|
1 | 1 | import copy |
2 | 2 |
|
3 | | -from pymoo.util.dynamic_dict import DynamicDict |
4 | 3 |
|
| 4 | +class Individual: |
5 | 5 |
|
6 | | -class Individual(DynamicDict): |
| 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()) |
7 | 14 |
|
8 | 15 | def has(self, key): |
9 | | - return key in self |
| 16 | + return key in self.attr or key in self.data |
10 | 17 |
|
11 | | - def set(self, key, val): |
12 | | - self[key] = val |
| 18 | + def set(self, key, value): |
| 19 | + if key in self.attr: |
| 20 | + self.__dict__[key] = value |
| 21 | + else: |
| 22 | + self.data[key] = value |
| 23 | + return self |
13 | 24 |
|
14 | 25 | def copy(self, deep=False): |
15 | | - if not deep: |
16 | | - d = dict(**self.__dict__) |
| 26 | + ind = copy.copy(self) |
| 27 | + ind.data = copy.copy(self.data) if not deep else copy.deepcopy(self.data) |
| 28 | + return ind |
| 29 | + |
| 30 | + 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] |
17 | 47 | else: |
18 | | - d = copy.deepcopy(**self.__dict__) |
| 48 | + return tuple(ret) |
19 | 49 |
|
20 | | - ind = Individual(**d) |
21 | | - return ind |
| 50 | + |
| 51 | +class eIndividual: |
| 52 | + |
| 53 | + def __init__(self, **kwargs) -> None: |
| 54 | + kwargs = {**kwargs, **dict(X=None, F=None, CV=None, G=None, feasible=None)} |
| 55 | + for k, v in kwargs.items(): |
| 56 | + self.__dict__[k] = v |
| 57 | + |
| 58 | + def has(self, key): |
| 59 | + return key in self.__dict__ |
| 60 | + |
| 61 | + def set(self, key, val): |
| 62 | + self.__dict__[key] = val |
22 | 63 |
|
23 | 64 | def get(self, *keys): |
24 | 65 | if len(keys) == 1: |
25 | | - key, = keys |
26 | | - return super().get(key) |
| 66 | + return self.__dict__.get(keys[0]) |
| 67 | + else: |
| 68 | + return tuple([self.__dict__.get(key) for key in keys]) |
| 69 | + |
| 70 | + def copy(self, deep=False): |
| 71 | + ind = copy.copy(self) |
| 72 | + ind.data = copy.copy(self.__dict__) if not deep else copy.deepcopy(self.__dict__) |
| 73 | + return ind |
| 74 | + |
| 75 | + |
| 76 | + if not deep: |
| 77 | + d = dict(self.__dict__) |
27 | 78 | else: |
28 | | - return tuple([super().get(key) for key in keys]) |
| 79 | + d = copy.deepcopy(self.__dict__) |
| 80 | + ind = Individual(**d) |
| 81 | + return ind |
| 82 | + |
| 83 | + # @property |
| 84 | + # def F(self): |
| 85 | + # attr = "F" |
| 86 | + # if attr in self.__dict__: |
| 87 | + # return self.__dict__[attr] |
| 88 | + # else: |
| 89 | + # return None |
| 90 | + |
| 91 | + # Gets called when the item is not found via __getattribute__ |
| 92 | + # def __getattr__(self, item): |
| 93 | + # return super(Individual, self).__setattr__(item, 'orphan') |
| 94 | + |
| 95 | + def __getattr__(self, val): |
| 96 | + return self.__dict__.get(val) |
| 97 | + |
| 98 | + # def __setitem__(self, key, value): |
| 99 | + # self.__dict__[key] = value |
| 100 | + # |
| 101 | + # def __getitem__(self, key): |
| 102 | + # return self.__dict__.get(key) |
| 103 | + |
| 104 | + # def __getattr__(self, attr): |
| 105 | + # |
| 106 | + # if attr == "F": |
| 107 | + # if attr in self.__dict__: |
| 108 | + # return self.__dict__[attr] |
| 109 | + # else: |
| 110 | + # return None |
| 111 | + # |
| 112 | + # if attr in self.__dict__: |
| 113 | + # return self.__dict__[attr] |
| 114 | + # |
| 115 | + # |
| 116 | + # |
| 117 | + def __setattr__(self, key, value): |
| 118 | + self.__dict__[key] = value |
0 commit comments