Skip to content

Commit

Permalink
🚧 Renaming attributes to maintain coherence.
Browse files Browse the repository at this point in the history
  • Loading branch information
amarrerod committed Jul 23, 2024
1 parent f85d83d commit 6f78ca5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
14 changes: 7 additions & 7 deletions digneapy/archives/_grid_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
self,
dimensions: Sequence[int],
ranges: Sequence[Tuple[float, float]],
attribute: str = "features",
descriptor: str = "features",
instances: Optional[Iterable[Instance]] = None,
eps: float = 1e-6,
dtype=np.float64,
Expand All @@ -57,7 +57,7 @@ def __init__(
:math:`[-2,2]` (inclusive). ``ranges`` should be the same length as
``dims``.
instances (Optional[Iterable[Instance]], optional): Instances to pre-initialise the archive. Defaults to None.
attribute: str = Attribute of the Instances to compute the diversity.
descriptor: str = Descriptor of the Instances to compute the diversity.
eps (float, optional): Due to floating point precision errors, we add a small
epsilon when computing the archive indices in the :meth:`index_of`
method -- refer to the implementation `here. Defaults to 1e-6.
Expand All @@ -76,15 +76,15 @@ def __init__(
)

self._dimensions = np.asarray(dimensions)
self._inst_attrs = attribute
if attribute not in descriptor_strategies:
msg = f"describe_by {attribute} not available in {self.__class__.__name__}.__init__. Set to features by default"
self._inst_attrs = descriptor
if descriptor not in descriptor_strategies:
msg = f"describe_by {descriptor} not available in {self.__class__.__name__}.__init__. Set to features by default"
print(msg)
self._inst_attrs = "features"
self._descriptor_strategy = descriptor_strategies["features"]
else:
self._inst_attrs = attribute
self._descriptor_strategy = descriptor_strategies[attribute]
self._inst_attrs = descriptor
self._descriptor_strategy = descriptor_strategies[descriptor]

ranges = list(zip(*ranges))
self._lower_bounds = np.array(ranges[0], dtype=dtype)
Expand Down
14 changes: 7 additions & 7 deletions digneapy/generators/_map_elites_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
archive: GridArchive,
mutation: mutation.Mutation,
repetitions: int,
strategy: str,
descriptor: str,
performance_function: PerformanceFn = default_performance_metric,
):
self._domain = domain
Expand All @@ -49,17 +49,17 @@ def __init__(
self._repetitions = repetitions
self._performance_fn = performance_function

if strategy not in descriptor_strategies:
msg = f"strategy {strategy} not available in {self.__class__.__name__}.__init__. Set to features by default"
if descriptor not in descriptor_strategies:
msg = f"descriptor {descriptor} not available in {self.__class__.__name__}.__init__. Set to features by default"
print(msg)
strategy = "features"
descriptor = "features"

self._descriptor = strategy
match strategy:
self._descriptor = descriptor
match descriptor:
case "features":
self._descriptor_strategy = self._domain.extract_features
case _:
self._descriptor_strategy = descriptor_strategies[strategy]
self._descriptor_strategy = descriptor_strategies[descriptor]

self._stats_fitness = tools.Statistics(key=attrgetter("fitness"))
self._stats_fitness.register("avg", np.mean)
Expand Down
32 changes: 21 additions & 11 deletions examples/evolve_nn_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NSEval:
This must be called for each transformed at every generation of the CMA-ES algorithm.
"""

def __init__(self, features_info, resolution: int = 20):
def __init__(self, features_info, resolution: int = 10):
self.resolution = resolution
self.features_info = features_info
self.kp_domain = KPDomain(dimension=50, capacity_approach="percentage")
Expand Down Expand Up @@ -106,7 +106,7 @@ def __call__(self, transformer: TorchNN):


def main():
R = 20 # Resolution/Number of bins for each of the 8 features
R = 10 # Resolution/Number of bins for each of the 8 features
dimension = 46 # Number of weights of the NN for KP
nn = TorchNN(
name="NN_transformer_kp_domain.keras",
Expand All @@ -115,16 +115,26 @@ def main():
output_size=2,
)

# # KP Features information extracted from previously generated instances
# KP Features information extracted from previously generated instances
# features_info = [
# (711, 30000),
# (890, 1000),
# (860, 1000.0),
# (1.0, 200),
# (1.0, 230.0),
# (0.10, 12.0),
# (400, 610),
# (240, 330),
# ]
features_info = [
(711, 30000),
(890, 1000),
(860, 1000.0),
(1.0, 200),
(1.0, 230.0),
(0.10, 12.0),
(400, 610),
(240, 330),
(1, 30000), # Capacity
(1, 1000), # Max Profit
(1, 1000), # Max Weight
(1, 1000), # Min Profit
(1, 1000), # Min Weight
(0.0, 100.0), # Average Efficiency
(0.0, 1000), # Mean of vars
(0.0, 500.0), # Std
]
# NSEval is the evaluation/fitness function used to measure the NNs in CMA-Es
ns_eval = NSEval(features_info, resolution=R)
Expand Down
2 changes: 1 addition & 1 deletion examples/knapsack_domain_map_elites.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def map_elites_knapsack():
initial_pop_size=10,
mutation=uniform_one_mutation,
generations=1000,
strategy="features",
descriptor="features",
repetitions=1,
)
assert archive.filled_cells == len(archive.instances)
Expand Down
2 changes: 1 addition & 1 deletion tests/generators/test_map_elites.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_map_elites_domain(
initial_pop_size=5,
mutation=uniform_one_mutation,
generations=1000,
strategy="features",
descriptor="features",
repetitions=1,
)
archive = map_elites()
Expand Down

0 comments on commit 6f78ca5

Please sign in to comment.