|
25 | 25 | import random |
26 | 26 | import logging |
27 | 27 | import functools |
| 28 | +import numpy |
28 | 29 |
|
29 | 30 | import deap |
30 | 31 | import deap.base |
|
44 | 45 | # settings of the algorithm can be stored in objects of these classes |
45 | 46 |
|
46 | 47 |
|
| 48 | +class WeightedSumFitness(deap.base.Fitness): |
| 49 | + |
| 50 | + """Fitness that compares by weighted sum""" |
| 51 | + |
| 52 | + def __init__(self, values=(), obj_size=None): |
| 53 | + self.weights = [-1.0] * obj_size if obj_size is not None else [-1] |
| 54 | + |
| 55 | + super(WeightedSumFitness, self).__init__(values) |
| 56 | + |
| 57 | + @property |
| 58 | + def weighted_sum(self): |
| 59 | + """Weighted sum of wvalues""" |
| 60 | + return sum(self.wvalues) |
| 61 | + |
| 62 | + @property |
| 63 | + def sum(self): |
| 64 | + """Weighted sum of values""" |
| 65 | + return sum(self.values) |
| 66 | + |
| 67 | + def __le__(self, other): |
| 68 | + return self.weighted_sum <= other.weighted_sum |
| 69 | + |
| 70 | + def __lt__(self, other): |
| 71 | + return self.weighted_sum < other.weighted_sum |
| 72 | + |
| 73 | + def __deepcopy__(self, _): |
| 74 | + """Override deepcopy""" |
| 75 | + |
| 76 | + cls = self.__class__ |
| 77 | + result = cls.__new__(cls) |
| 78 | + result.__dict__.update(self.__dict__) |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +class WSListIndividual(list): |
| 83 | + |
| 84 | + """Individual consisting of list with weighted sum field""" |
| 85 | + |
| 86 | + def __init__(self, *args, **kwargs): |
| 87 | + """Constructor""" |
| 88 | + self.fitness = WeightedSumFitness(obj_size=kwargs['obj_size']) |
| 89 | + del kwargs['obj_size'] |
| 90 | + super(WSListIndividual, self).__init__(*args, **kwargs) |
| 91 | + |
| 92 | + |
47 | 93 | class DEAPOptimisation(bluepyopt.optimisations.Optimisation): |
48 | 94 |
|
49 | 95 | """DEAP Optimisation class""" |
@@ -135,7 +181,7 @@ def setup_deap(self): |
135 | 181 | self.toolbox.register( |
136 | 182 | "Individual", |
137 | 183 | deap.tools.initIterate, |
138 | | - functools.partial(utils.WSListIndividual, obj_size=OBJ_SIZE), |
| 184 | + functools.partial(WSListIndividual, obj_size=OBJ_SIZE), |
139 | 185 | self.toolbox.uniformparams) |
140 | 186 |
|
141 | 187 | # Register the population format. It is a list of individuals |
|
0 commit comments