71 def __init__(self, archive, dimension, *, population_size=16, neighbors=5, memory_size=256,
100 def optimise(self, evaluate, budget, *, repair=None):
101 """Measure evaluate(bits) returning (scalar fitness, descriptors).
103 An optional repair(bits) supplies a feasible bit vector for
104 evaluation. Charge any additional work inside that callback
105 separately. Run with an empty reporting archive.
108 raise ValueError(
"Use an empty reporting archive for each swarm run.")
109 if not callable(evaluate)
or (repair
is not None and not callable(repair)):
110 raise TypeError(
"Supply a callable evaluator and optional repair.")
111 budget = _positive_int(budget,
"budget")
112 rng = np.random.default_rng(self.
seed)
114 moves = (budget - 1) // width
116 positions = rng.integers(0, 2, size=shape, dtype=np.int8)
117 personal = positions.copy()
119 memory, seen = deque(), set()
120 calls = updates = outside = 0
121 while calls < budget:
122 count = min(budget - calls, width)
124 for row
in range(count):
125 genome = tuple(map(int, positions[row]))
126 if repair
is not None:
127 fixed = np.asarray(repair(genome))
128 if fixed.shape != (self.
dimension,)
or not np.isin(fixed, (0, 1)).all():
129 raise ValueError(
"repair must return a binary vector of the configured length.")
130 positions[row] = fixed
131 genome = tuple(map(int, positions[row]))
132 fitness, features = evaluate(genome)
133 fitness = float(fitness)
134 features = tuple(map(float, features))
136 if (
not isfinite(fitness)
or len(features) != len(self.
archive.features_domain)
137 or not all(map(isfinite, features))):
138 raise ValueError(
"Return one finite objective and finite archive descriptors.")
139 if (self.
archive.in_bounds((fitness,), self.
archive.fitness_domain)
140 and self.
archive.in_bounds(features, self.
archive.features_domain)):
142 previous = self.
archive.nb_added
145 updates += self.
archive.nb_added - previous
148 measured.append(genome)
150 snapshot = np.asarray(memory, dtype=np.int8)
151 candidates = _hamming_novelty(positions[:count], snapshot, self.
neighbors)
152 leaders = _hamming_novelty(personal, snapshot, self.
neighbors)
153 better = np.ones(count, dtype=bool)
if not memory
else candidates > leaders[:count]
154 np.copyto(personal[:count], positions[:count], where=better[:,
None])
155 leaders[:count] = np.where(better, candidates, leaders[:count])
156 for genome
in measured:
157 if genome
not in seen:
159 seen.remove(memory.popleft())
160 memory.append(genome)
163 leader = personal[int(np.argmax(leaders))]
164 velocity = np.clip(self.
inertia * velocity
165 + self.
cognitive * rng.random(shape) * (personal - positions)
166 + self.
social * rng.random(shape) * (leader - positions),
169 progress = (calls // width - 1) / max(1, moves - 1)
170 temperature = start + progress * (end - start)
172 active = min(width, budget - calls)
173 positions[:active] = rng.random((active, self.
dimension)) < expit(velocity[:active] / temperature)
175 tuple(memory), calls, updates, outside)