Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit ef41208

Browse files
author
Tanguy Damart
committed
Added WeightedSumFitness for retro-compatibility
1 parent d3cab08 commit ef41208

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

bluepyopt/deapext/optimisations.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import random
2626
import logging
2727
import functools
28+
import numpy
2829

2930
import deap
3031
import deap.base
@@ -44,6 +45,51 @@
4445
# settings of the algorithm can be stored in objects of these classes
4546

4647

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+
4793
class DEAPOptimisation(bluepyopt.optimisations.Optimisation):
4894

4995
"""DEAP Optimisation class"""
@@ -135,7 +181,7 @@ def setup_deap(self):
135181
self.toolbox.register(
136182
"Individual",
137183
deap.tools.initIterate,
138-
functools.partial(utils.WSListIndividual, obj_size=OBJ_SIZE),
184+
functools.partial(WSListIndividual, obj_size=OBJ_SIZE),
139185
self.toolbox.uniformparams)
140186

141187
# Register the population format. It is a list of individuals

0 commit comments

Comments
 (0)