Skip to content

Commit

Permalink
✅ Update tests to the new interface. Skips Pisinger tests because is …
Browse files Browse the repository at this point in the history
…not building the cpp files.
  • Loading branch information
amarrerod committed Feb 7, 2025
1 parent 27ec2a4 commit 6a95dcb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions digneapy/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ def log(self) -> tools.Logbook:
def __str__(self):
port_names = [s.__name__ for s in self.portfolio]
domain_name = self.domain.name if self.domain is not None else "None"
return f"EAGenerator(pop_size={self.pop_size},gen={self.generations},domain={domain_name},portfolio={port_names!r},{super().__str__()})"
return f"EAGenerator(pop_size={self.pop_size},gen={self.generations},domain={domain_name},portfolio={port_names!r},{self._ns_approach.__str__()})"

def __repr__(self) -> str:
port_names = [s.__name__ for s in self.portfolio]
domain_name = self.domain.name if self.domain is not None else "None"
return f"EAGenerator<pop_size={self.pop_size},gen={self.generations},domain={domain_name},portfolio={port_names!r},{super().__repr__()}>"
return f"EAGenerator<pop_size={self.pop_size},gen={self.generations},domain={domain_name},portfolio={port_names!r},{self._ns_approach.__repr__()}>"

def __call__(self, verbose: bool = False):
return self._run(verbose)
Expand Down Expand Up @@ -227,15 +227,15 @@ def _run(self, verbose: bool = False):
offspring.append(off)

self._evaluate_population(offspring)
self.sparseness(offspring)
self._ns_approach.sparseness(offspring)
self._compute_fitness(population=offspring) # Computes Fitness (p and s)

# Only the feasible instances are considered to be included
# in the archive and the solution set.
feasible_off_archive = list(filter(lambda i: i.p >= 0, offspring))
self._ns_approach.archive.extend(feasible_off_archive)

self.sparseness_solution_set(offspring)
self._ns_approach.sparseness_solution_set(offspring)
feasible_off_sset = list(filter(lambda i: i.p >= 0, offspring))
self._ns_approach.solution_set.extend(feasible_off_sset)

Expand Down
10 changes: 5 additions & 5 deletions tests/domains/test_knapsack_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ def test_default_kp_domain():
domain = kp.KnapsackDomain(dimension, capacity_approach="evolved")
assert len(domain) == dimension
assert domain.capacity_approach == "evolved"
assert domain.max_capacity == 1e4
assert domain.max_capacity == 1e7
assert domain.capacity_ratio == 0.8
assert domain.min_p == 1
assert domain.min_w == 1
assert domain.max_p == 1000
assert domain.max_w == 1000
assert domain.bounds == [(1.0, 1e4)] + [
assert domain.bounds == [(1.0, 1e7)] + [
(1, 1000) if i % 2 == 0 else (1, 1000) for i in range(2 * dimension)
]

Expand All @@ -96,7 +96,7 @@ def test_kp_domain_to_features():
features = domain.extract_features(instance)

assert isinstance(features, tuple)
assert features[0] == 1e4
assert features[0] == 1e7
assert features[1] <= 1000
assert features[2] <= 1000
assert features[3] >= 1
Expand All @@ -121,7 +121,7 @@ def test_kp_domain_to_features_dict():
instance = domain.generate_instance()
features = domain.extract_features_as_dict(instance)
assert isinstance(features, dict)
assert features["capacity"] == 1e4
assert features["capacity"] == 1e7
assert features["max_p"] <= 1000
assert features["max_w"] <= 1000
assert features["min_w"] >= 1
Expand All @@ -140,7 +140,7 @@ def test_kp_domain_to_instance():
kp_instance = domain.from_instance(instance)
assert len(kp_instance.weights) == dimension
assert len(kp_instance.profits) == dimension
assert kp_instance.capacity == 1e4
assert kp_instance.capacity == 1e7

domain.capacity_approach = "evolved"
kp_instance = domain.from_instance(instance)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_equal_archives(default_archive):

def test_append_instance(empty_archive):
assert 0 == len(empty_archive)
instance = Instance(variables=list(range(100)))
instance = Instance(variables=list(range(100)), s=1.0)
empty_archive.append(instance)
assert 1 == len(empty_archive)
assert [instance] == empty_archive.instances
Expand Down
9 changes: 5 additions & 4 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
runtime_score,
)
from digneapy.domains import BPPDomain, KnapsackDomain
from digneapy.generators import EAGenerator, MapElitesGenerator
from digneapy.generators import EAGenerator, MapElitesGenerator, DEAGenerator
from digneapy.operators import (
binary_tournament_selection,
generational_replacement,
Expand All @@ -48,9 +48,9 @@ def test_default_generator():
eig = EAGenerator(domain=None, portfolio=[])
assert eig.pop_size == 100
assert eig.generations == 1000
assert eig.k == 15
assert eig._describe_by == "features"
assert eig._transformer is None
assert eig._ns_approach.k == 15
assert eig._ns_approach._describe_by == "features"
assert eig._ns_approach._transformer is None
assert eig.domain is None
assert eig.portfolio == tuple()
assert eig.repetitions == 1
Expand Down Expand Up @@ -233,6 +233,7 @@ def test_eig_gen_kp_inst_descriptor():
assert all(max(p_scores[i]) == p_scores[i][0] for i in range(len(p_scores)))


@pytest.mark.skip(reason="No way of currently testing this")
def test_eig_gen_kp_perf_descriptor_with_pisinger():
portfolio = deque([combo, minknap, expknap])
kp_domain = KnapsackDomain(dimension=50, capacity_approach="evolved")
Expand Down
5 changes: 3 additions & 2 deletions tests/transformers/test_knapsack_autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from digneapy.domains import kp
from digneapy.transformers.autoencoders import KPEncoder

encoders = (50, 100, 250, 500, 100, "var_2d", "var_8d", "var_best")
encoders = ("50", "100", "500", "1000", "2000", "5000", "variable")



@pytest.mark.parametrize("encoder", encoders)
def test_autoencoder(encoder):
dimension = encoder if isinstance(encoder, int) else 1000
dimension = int(encoder) if encoder.isdigit() else 1000
n_instances = 100
autoencoder = KPEncoder(encoder=encoder)
domain = kp.KnapsackDomain(dimension=dimension)
Expand Down

0 comments on commit 6a95dcb

Please sign in to comment.