20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 | class Solution:
"""
Class representing a solution in a genetic algorithm.
It contains the variables, objectives, constraints, and fitness of the solution.
"""
__slots__ = (
"_variables",
"_objectives",
"_constraints",
"_fitness",
"_dtype",
"_otype",
)
def __init__(
self,
variables: Optional[Iterable] = [],
objectives: Optional[Iterable] = [],
constraints: Optional[Iterable] = [],
fitness: float | np.float64 = 0.0,
dtype=np.uint32,
otype=np.float64,
):
"""Creates a new solution object.
The variables is a numpy array of the solution's genes.
The objectives and constraints are numpy arrays of the solution's objectives and constraints.
The fitness is a float representing the solution's fitness value.
Args:
variables (Optional[Iterable], optional): Tuple or any other iterable with the variables/variables. Defaults to None.
objectives (Optional[Iterable], optional): Tuple or any other iterable with the objectives values. Defaults to None.
constraints (Optional[Iterable], optional): Tuple or any other iterable with the constraint values. Defaults to None.
fitness (float, optional): Fitness of the solution. Defaults to 0.0.
"""
self._otype = otype
self._dtype = dtype
self._variables = np.asarray(variables, dtype=self.dtype)
self._objectives = np.asarray(objectives, dtype=self._otype)
self._constraints = np.asarray(constraints, dtype=self._otype)
try:
if fitness is None:
self._fitness = np.float64(0)
else:
self._fitness = np.float64(fitness)
except (TypeError, ValueError) as exc:
raise ValueError(
"Fitness must be convertible to float in Solution"
) from exc
@property
def dtype(self):
return self._dtype
@property
def variables(self):
return self._variables
@variables.setter
def variables(self, new_variables: np.ndarray):
if len(new_variables) != len(self._variables):
raise ValueError(
"Updating the variables of an Solution object with a different number of values. "
f"Solution have {len(self._variables)} "
f"variables and the new_variables sequence have {len(new_variables)}"
)
self._variables = np.asarray(new_variables)
@property
def fitness(self) -> np.float64 | float:
return self._fitness
@fitness.setter
def fitness(self, f: np.float64 | float):
try:
self._fitness = float(f)
except (TypeError, ValueError) as exc:
raise ValueError(
f"The fitness value {f} is not a float in fitness setter of class Solution."
) from exc
finally:
self._fitness = np.float64(self._fitness)
@property
def objectives(self):
return self._objectives
@objectives.setter
def objectives(self, new_objectives: np.ndarray):
if len(new_objectives) != len(self._objectives):
raise ValueError(
"Updating the objectives of an Solution object with a different number of values. "
f"Solution have {len(self._objectives)} "
f"objectives and the new_objectives sequence have {len(new_objectives)}"
)
self._objectives = np.asarray(new_objectives)
@property
def constraints(self):
return self._constraints
@constraints.setter
def constraints(self, new_constraints: np.ndarray):
if len(new_constraints) != len(self._constraints):
raise ValueError(
"Updating the constraints of an Solution object with a different number of values. "
f"Solution have {len(self._constraints)} "
f"constraints and the new_constraints sequence have {len(new_constraints)}"
)
self._constraints = np.asarray(new_constraints)
def clone(self) -> Self:
"""Returns a deep copy of the solution. It is more efficient than using the copy module.
Returns:
Self: Solution object
"""
return type(self)(
variables=list(self._variables),
objectives=list(self._objectives),
constraints=list(self._constraints),
fitness=self._fitness,
otype=self._otype,
)
def clone_with(self, **overrides):
"""Clones an Instance with overriden attributes
Returns:
Instance
"""
new_object = self.clone()
for key, value in overrides.items():
setattr(new_object, key, value)
return new_object
def __str__(self) -> str:
return f"Solution(dim={len(self.variables)},f={self.fitness},objs={self.objectives},const={self.constraints})"
def __repr__(self) -> str:
return f"Solution<dim={len(self.variables)},f={self.fitness},objs={self.objectives},const={self.constraints}>"
def __len__(self) -> int:
return len(self.variables)
def __iter__(self):
return iter(self.variables)
def __eq__(self, other: Self) -> bool:
if not isinstance(other, Solution):
raise TypeError(
"Other of type {other.__class__.__name__} can not be compared with a Solution."
)
else:
try:
return all(a == b for a, b in zip(self, other, strict=True))
except ValueError:
return False
def __gt__(self, other: Self) -> np.bool:
if not isinstance(other, Solution):
raise TypeError(
f"Other of type {other.__class__.__name__} can not be compared with a Solution."
)
return self.fitness > other.fitness
def __getitem__(self, key: int | slice) -> Sequence | np.ndarray:
"""Accessor to variables of the Solution
Args:
key (int | slice): index or slice to access a subset of variables
Returns:
Sequence or np.ndarray: If accessed with a slice the subset of variables
are returned. Otherwise, it returns a single scalar at variables[key].
"""
if not isinstance(key, (int, slice)):
raise TypeError(
f"Solution cannot be indexed with type: {type(key)}. Use slice or int."
)
if isinstance(key, slice):
return self._variables[key]
else:
index = operator.index(key)
return self.variables[index]
def __setitem__(self, key: int | slice, value):
"""Setter to variables of the Solution
Args:
key (int | slice): index or slice to access a subset of variables
value: Value to set in the variables
"""
if not isinstance(key, (int, np.integer, np.unsignedinteger, slice)):
raise TypeError(
f"Solution cannot be update via __setitem__ with type: {type(key)}. Use slice or int."
)
if isinstance(key, slice):
# Compute how many positions this slice actually targets
start, stop, step = key.indices(len(self.variables))
target_count = len(range(start, stop, step))
# Accept any sequence; reject bare scalars for slice assignment
try:
expected_len = len(value)
except TypeError:
raise TypeError(
f"[Solution] slice assignment requires a sequence, got scalar {value!r}. "
f"Expected {target_count} value(s)."
)
if expected_len != target_count:
raise ValueError(
f"[Solution] slice targets {target_count} element(s) but value has "
f"{expected_len} element(s)."
)
else:
index = operator.index(key)
try:
if len(value) != 1:
raise ValueError(
f"[Solution] index {index!r} targets 1 element but value has "
f"{len(value)} element(s). Use a slice to assign multiple values."
)
except TypeError:
pass # len() failed which means that we have a scalar value
self._variables[key] = value
|