Source code for macop.solutions.discrete

"""Discrete solution classes implementations
"""
import numpy as np

# modules imports
from .base import Solution


[docs]class BinarySolution(Solution): """ Binary integer solution class Attributes: data: {ndarray} -- array of binary values size: {int} -- size of binary array values score: {float} -- fitness score value """ def __init__(self, data, size): """ Initialize binary solution using specific data Args: data: {ndarray} -- array of binary values size: {int} -- size of binary array values """ super().__init__(data, size)
[docs] def random(self, validator): """ Intialize binary array with use of validator to generate valid random solution Args: validator: {function} -- specific function which validates or not a solution Returns: {BinarySolution} -- new generated binary solution """ self._data = np.random.randint(2, size=self._size) while not self.isValid(validator): self._data = np.random.randint(2, size=self._size) return self
def __str__(self): return "Binary solution %s" % (self._data)
[docs]class CombinatoryIntegerSolution(Solution): """ Combinatory integer solution class Attributes: data: {ndarray} -- array of binary values size: {int} -- size of binary array values score: {float} -- fitness score value """ def __init__(self, data, size): """ Initialize binary solution using specific data Args: data: {ndarray} -- array of binary values size: {int} -- size of binary array values """ super().__init__(data, size)
[docs] def random(self, validator): """ Intialize combinatory integer array with use of validator to generate valid random solution Args: validator: {function} -- specific function which validates or not a solution Returns: {CombinatoryIntegerSolution} -- new generated combinatory integer solution """ self._data = np.random.shuffle(np.arange(self._size)) while not self.isValid(validator): self._data = np.random.shuffle(np.arange(self._size)) return self
def __str__(self): return "Combinatory integer solution %s" % (self._data)
[docs]class IntegerSolution(Solution): """ Integer solution class Attributes: data: {ndarray} -- array of binary values size: {int} -- size of binary array values score: {float} -- fitness score value """ def __init__(self, data, size): """ Initialize integer solution using specific data Args: data: {ndarray} -- array of binary values size: {int} -- size of binary array values """ super().__init__(data, size)
[docs] def random(self, validator): """ Intialize integer array with use of validator to generate valid random solution Args: validator: {function} -- specific function which validates or not a solution Returns: {IntegerSolution} -- new generated integer solution """ self._data = np.random.randint(self._size, size=self._size) while not self.isValid(validator): self._data = np.random.randint(self._size, size=self._size) return self
def __str__(self): return "Integer solution %s" % (self._data)