Shortcuts

macop.solutions.continuous

Continuous solution classes implementation

class macop.solutions.continuous.ContinuousSolution(data, size)[source]

Bases: macop.solutions.base.Solution

Continuous solution class

  • store solution as a float array (example: [0.5, 0.2, 0.17, 0.68, 0.42])

  • associated size is the size of the array

  • mainly use for selecting or not an element in a list of valuable objects

data

{ndarray} – array of float values

size

{int} – size of float array values

score

{float} – fitness score value

clone()

Clone the current solution and its data, but without keeping evaluated _score

Returns

clone of current solution

Return type

{Solution}

evaluate(evaluator)

Evaluate solution using specific evaluator

Parameters

_evaluator – {function} – specific function which computes fitness of solution

Returns

fitness score value

Return type

{float}

isValid(validator)

Use of custom function which checks if a solution is valid or not

Parameters

validator – {function} – specific function which validates or not a solution

Returns

True is solution is valid

Return type

{bool}

static random(size, interval, validator=None)[source]

Intialize float array with use of validator to generate valid random solution

Parameters
  • size – {int} – expected solution size to generate

  • interval – {(float, float)} – tuple with min and max expected interval value for current solution

  • validator – {function} – specific function which validates or not a solution (if None, not validation is applied)

Returns

new generated continuous solution

Return type

{Continuous}

Example:

>>> from macop.solutions.continuous import ContinuousSolution
>>>
>>> # generate random solution using specific validator
>>> validator = lambda solution: True if sum(solution.data) > 5 else False
>>> solution = ContinuousSolution.random(10, (-2, 2), validator)
>>> sum(solution.data) > 5
True
property data

Returns solution data (by default data private attribute)

Returns

data values

Return type

{object}

property fitness

Returns fitness score (by default score private attribute)

Returns

fitness score value

Return type

{float}

property size

Returns solution size (by default size private attribute)

Returns

data size

Return type

{object}