Skip to content

Commit ae5a191

Browse files
author
Julian Blank
committed
Individual as dictionary implementation
1 parent ac5ae79 commit ae5a191

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

pymoo/model/individual.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,28 @@
11
import copy
22

3+
from pymoo.util.dynamic_dict import DynamicDict
34

4-
class Individual:
55

6-
def __init__(self, X=None, F=None, CV=None, G=None, feasible=None, **kwargs) -> None:
7-
self.X = X
8-
self.F = F
9-
self.CV = CV
10-
self.G = G
11-
self.feasible = feasible
12-
self.data = kwargs
13-
self.attr = set(self.__dict__.keys())
6+
class Individual(DynamicDict):
147

158
def has(self, key):
16-
return key in self.attr or key in self.data
9+
return key in self
1710

18-
def set(self, key, value):
19-
if key in self.attr:
20-
self.__dict__[key] = value
11+
def set(self, key, val):
12+
self[key] = val
13+
14+
def copy(self, deep=False):
15+
if not deep:
16+
d = dict(**self.__dict__)
2117
else:
22-
self.data[key] = value
23-
return self
18+
d = copy.deepcopy(**self.__dict__)
2419

25-
def copy(self):
26-
ind = copy.copy(self)
27-
ind.data = self.data.copy()
20+
ind = Individual(**d)
2821
return ind
2922

3023
def get(self, *keys):
31-
32-
def _get(key):
33-
if key in self.data:
34-
return self.data[key]
35-
elif key in self.attr:
36-
return self.__dict__[key]
37-
else:
38-
return None
39-
40-
ret = []
41-
42-
for key in keys:
43-
ret.append(_get(key))
44-
45-
if len(ret) == 1:
46-
return ret[0]
24+
if len(keys) == 1:
25+
key, = keys
26+
return super().get(key)
4727
else:
48-
return tuple(ret)
28+
return tuple([super().get(key) for key in keys])

pymoo/model/plot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def show(self, **kwargs):
172172
plt.show(**kwargs)
173173
plt.close()
174174

175+
return self
176+
175177
def save(self, fname, **kwargs):
176178
self.plot_if_not_done_yet()
177179
set_if_none(kwargs, "bbox_inches", "tight")

pymoo/model/population.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
class Population(np.ndarray):
99

10-
individual = None
11-
1210
def __new__(cls, n_individuals=0, individual=None):
1311

1412
# the default individual if not specific otherwise

pymoo/util/dynamic_dict.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
class DynamicDict(dict):
3+
4+
def __init__(self, *args, **kwargs) -> None:
5+
super().__init__(*args, **kwargs)
6+
7+
def __setitem__(self, key, value):
8+
super(DynamicDict, self).__setitem__(key, value)
9+
self.__dict__.update({key: value})
10+
11+
def __delitem__(self, key):
12+
super(DynamicDict, self).__delitem__(key)
13+
del self.__dict__[key]
14+
15+
def __getattr__(self, attr):
16+
return self.get(attr)
17+
18+
def __setattr__(self, key, value):
19+
self.__setitem__(key, value)
20+
21+
def __delattr__(self, item):
22+
self.__delitem__(item)
23+
24+
def __getstate__(self):
25+
return self
26+
27+
def __setstate__(self, state):
28+
self.update(state)
29+
self.__dict__ = self

pymoo/util/nds/non_dominated_sorting.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class NonDominatedSorting:
88

9-
def __init__(self, epsilon=0.0, method="fast_non_dominated_sort") -> None:
9+
def __init__(self, epsilon=None, method="fast_non_dominated_sort") -> None:
1010
super().__init__()
11-
self.epsilon = float(epsilon)
11+
self.epsilon = epsilon
1212
self.method = method
1313

1414
def do(self, F, return_rank=False, only_non_dominated_front=False, n_stop_if_ranked=None, **kwargs):
@@ -18,7 +18,12 @@ def do(self, F, return_rank=False, only_non_dominated_front=False, n_stop_if_ran
1818
if n_stop_if_ranked is None:
1919
n_stop_if_ranked = int(1e8)
2020
func = load_function(self.method)
21-
fronts = func(F, epsilon=self.epsilon, **kwargs)
21+
22+
# set the epsilon if it should be set
23+
if self.epsilon is not None:
24+
kwargs["epsilon"] = float(self.epsilon)
25+
26+
fronts = func(F, **kwargs)
2227

2328
# convert to numpy array for each front and filter by n_stop_if_ranked if desired
2429
_fronts = []

pymoo/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.2.dev"
1+
__version__ = "0.4.2.rc1"

0 commit comments

Comments
 (0)