Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplification of ResultStorage api #246

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ def get_starting_solution(self) -> ResultStorage:
sol = self.problem.get_dummy_solution()
fit = self.aggreg_from_sol(sol)
return ResultStorage(
list_solution_fits=[(sol, fit)],
best_solution=sol,
mode_optim=self.params_objective_function.sense_function,
list_solution_fits=[(sol, fit)],
)
else:
solver = GreedyColoring(
Expand Down Expand Up @@ -238,5 +237,5 @@ def build_other_solution(self, result_storage: ResultStorage) -> ResultStorage:
colors=[new_color_dict[colors[i]] for i in range(len(colors))],
)
fit = self.aggreg_from_sol(new_solution)
result_storage.add_solution(new_solution, fit)
result_storage.append((new_solution, fit))
return result_storage
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def get_starting_solution(self) -> ResultStorage:
sol = self.problem.get_dummy_solution()
fit = self.aggreg_from_sol(sol)
return ResultStorage(
list_solution_fits=[(sol, fit)],
best_solution=sol,
mode_optim=self.params_objective_function.sense_function,
list_solution_fits=[(sol, fit)],
)
else:
solver = GreedyColoring(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,8 @@ def solve(self, **kwargs: Any) -> ResultStorage:
colors=solution[0][1 : 1 + self.problem.number_of_nodes],
)
fit = self.aggreg_from_sol(rcpsp_sol)
return ResultStorage(
list_solution_fits=[(rcpsp_sol, fit)],
mode_optim=self.params_objective_function.sense_function,
return self.create_result_storage(
[(rcpsp_sol, fit)],
)
else:
return ResultStorage(
list_solution_fits=[],
mode_optim=self.params_objective_function.sense_function,
)
return self.create_result_storage()
6 changes: 1 addition & 5 deletions discrete_optimization/coloring/solvers/greedy_coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,4 @@ def solve(self, **kwargs: Any) -> ResultStorage:
solution = solution.to_reformated_solution()
fit = self.aggreg_from_sol(solution)
logger.debug(f"Solution found : {solution, fit}")
return ResultStorage(
list_solution_fits=[(solution, fit)],
best_solution=solution,
mode_optim=self.params_objective_function.sense_function,
)
return self.create_result_storage([(solution, fit)])
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ def get_starting_solution(self) -> ResultStorage:
solution = self.problem.get_dummy_solution()
fit = self.aggreg_from_sol(solution)
return ResultStorage(
list_solution_fits=[(solution, fit)],
best_solution=solution,
mode_optim=self.params_objective_function.sense_function,
list_solution_fits=[(solution, fit)],
)


Expand Down
3 changes: 1 addition & 2 deletions discrete_optimization/facility/solvers/facility_lp_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,8 @@ def retrieve(self) -> ResultStorage:
solution[c] = f
facility_solution = FacilitySolution(self.problem, solution)
fit = self.aggreg_from_sol(facility_solution)
return ResultStorage(
return self.create_result_storage(
[(facility_solution, fit)],
mode_optim=self.params_objective_function.sense_function,
)

def solve(
Expand Down
12 changes: 4 additions & 8 deletions discrete_optimization/facility/solvers/greedy_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ def solve(self, **kwargs: Any) -> ResultStorage:
capacity_remaining[facility_index] -= customer.demand
sol = FacilitySolution(problem=self.problem, facility_for_customers=solution)
fit = self.aggreg_from_sol(sol)
return ResultStorage(
list_solution_fits=[(sol, fit)],
best_solution=sol,
mode_optim=self.params_objective_function.sense_function,
return self.create_result_storage(
[(sol, fit)],
)


Expand Down Expand Up @@ -93,8 +91,6 @@ def solve(self, **kwargs: Any) -> ResultStorage:
break
sol = FacilitySolution(problem=self.problem, facility_for_customers=solution)
fit = self.aggreg_from_sol(sol)
return ResultStorage(
list_solution_fits=[(sol, fit)],
best_solution=sol,
mode_optim=self.params_objective_function.sense_function,
return self.create_result_storage(
[(sol, fit)],
)
5 changes: 2 additions & 3 deletions discrete_optimization/generic_rcpsp_tools/gphh_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,8 @@ def solve(self, **kwargs):
solution = self.build_solution(
domain=self.problem, func_heuristic=self.func_heuristic
)
return ResultStorage(
list_solution_fits=[(solution, self.aggreg_from_sol(solution))],
mode_optim=self.params_objective_function.sense_function,
return self.create_result_storage(
[(solution, self.aggreg_from_sol(solution))],
)

def init_primitives(self, pset) -> PrimitiveSet:
Expand Down
56 changes: 24 additions & 32 deletions discrete_optimization/generic_rcpsp_tools/neighbor_tools_rcpsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1413,20 +1413,18 @@ def adding_constraint_from_results_store(
if last_result_store is not None:
current_solution, fit = next(
(
last_result_store.list_solution_fits[j]
for j in range(len(last_result_store.list_solution_fits))
if "opti_from_cp"
in last_result_store.list_solution_fits[j][0].__dict__.keys()
last_result_store[j]
for j in range(len(last_result_store))
if "opti_from_cp" in last_result_store[j][0].__dict__.keys()
),
(None, None),
)
else:
current_solution, fit = next(
(
result_storage.list_solution_fits[j]
for j in range(len(result_storage.list_solution_fits))
if "opti_from_cp"
in result_storage.list_solution_fits[j][0].__dict__.keys()
result_storage[j]
for j in range(len(result_storage))
if "opti_from_cp" in result_storage[j][0].__dict__.keys()
),
(None, None),
)
Expand Down Expand Up @@ -1554,20 +1552,18 @@ def adding_constraint_from_results_store(
if last_result_store is not None:
current_solution, fit = next(
(
last_result_store.list_solution_fits[j]
for j in range(len(last_result_store.list_solution_fits))
if "opti_from_cp"
in last_result_store.list_solution_fits[j][0].__dict__.keys()
last_result_store[j]
for j in range(len(last_result_store))
if "opti_from_cp" in last_result_store[j][0].__dict__.keys()
),
(None, None),
)
else:
current_solution, fit = next(
(
result_storage.list_solution_fits[j]
for j in range(len(result_storage.list_solution_fits))
if "opti_from_cp"
in result_storage.list_solution_fits[j][0].__dict__.keys()
result_storage[j]
for j in range(len(result_storage))
if "opti_from_cp" in result_storage[j][0].__dict__.keys()
),
(None, None),
)
Expand Down Expand Up @@ -1646,20 +1642,18 @@ def adding_constraint_from_results_store(
if last_result_store is not None:
current_solution, fit = next(
(
last_result_store.list_solution_fits[j]
for j in range(len(last_result_store.list_solution_fits))
if "opti_from_cp"
in last_result_store.list_solution_fits[j][0].__dict__.keys()
last_result_store[j]
for j in range(len(last_result_store))
if "opti_from_cp" in last_result_store[j][0].__dict__.keys()
),
(None, None),
)
else:
current_solution, fit = next(
(
result_storage.list_solution_fits[j]
for j in range(len(result_storage.list_solution_fits))
if "opti_from_cp"
in result_storage.list_solution_fits[j][0].__dict__.keys()
result_storage[j]
for j in range(len(result_storage))
if "opti_from_cp" in result_storage[j][0].__dict__.keys()
),
(None, None),
)
Expand Down Expand Up @@ -1742,20 +1736,18 @@ def adding_constraint_from_results_store(
if last_result_store is not None:
current_solution, fit = next(
(
last_result_store.list_solution_fits[j]
for j in range(len(last_result_store.list_solution_fits))
if "opti_from_cp"
in last_result_store.list_solution_fits[j][0].__dict__.keys()
last_result_store[j]
for j in range(len(last_result_store))
if "opti_from_cp" in last_result_store[j][0].__dict__.keys()
),
(None, None),
)
else:
current_solution, fit = next(
(
result_storage.list_solution_fits[j]
for j in range(len(result_storage.list_solution_fits))
if "opti_from_cp"
in result_storage.list_solution_fits[j][0].__dict__.keys()
result_storage[j]
for j in range(len(result_storage))
if "opti_from_cp" in result_storage[j][0].__dict__.keys()
),
(None, None),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ def build_other_solution(self, result_storage: ResultStorage) -> ResultStorage:
init_solution=s,
)
solution, f = result_store.get_last_best_solution()
result_storage.list_solution_fits += [
(solution, self.aggreg_from_sol(solution))
]
result_storage += [(solution, self.aggreg_from_sol(solution))]
return result_storage
14 changes: 6 additions & 8 deletions discrete_optimization/generic_rcpsp_tools/solution_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,18 @@ def adding_constraint_from_results_store(
if last_result_store is not None:
current_solution, fit = next(
(
last_result_store.list_solution_fits[j]
for j in range(len(last_result_store.list_solution_fits))
if "opti_from_cp"
in last_result_store.list_solution_fits[j][0].__dict__.keys()
last_result_store[j]
for j in range(len(last_result_store))
if "opti_from_cp" in last_result_store[j][0].__dict__.keys()
),
(None, None),
)
else:
current_solution, fit = next(
(
result_storage.list_solution_fits[j]
for j in range(len(result_storage.list_solution_fits))
if "opti_from_cp"
in result_storage.list_solution_fits[j][0].__dict__.keys()
result_storage[j]
for j in range(len(result_storage))
if "opti_from_cp" in result_storage[j][0].__dict__.keys()
),
(None, None),
)
Expand Down
8 changes: 2 additions & 6 deletions discrete_optimization/generic_tools/asp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ def __init__(
self.dump_model_in_folders = dump_model_in_folders
self.do_solver = do_solver
self.callback = callback
self.res = ResultStorage(
[],
mode_optim=self.do_solver.params_objective_function.sense_function,
limit_store=False,
)
self.res = do_solver.create_result_storage()
self.nb_solutions = 0

def on_model(self, model: clingo.Model) -> bool:
Expand All @@ -121,7 +117,7 @@ def on_model(self, model: clingo.Model) -> bool:
# translate into do solution
sol = self.do_solver.retrieve_solution(model=model)
fit = self.do_solver.aggreg_from_sol(sol)
self.res.add_solution(solution=sol, fitness=fit)
self.res.append((sol, fit))
self.nb_solutions += 1
# end of step callback: stopping?
try:
Expand Down
3 changes: 2 additions & 1 deletion discrete_optimization/generic_tools/callbacks/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ def on_step_end(
) -> Optional[bool]:
if step % self.save_nb_steps == 0:
logger.debug(f"Pickling best solution")
pickle.dump(res.best_solution, open(self.backup_path, "wb"))
sol, fit = res.get_best_solution_fit()
pickle.dump(sol, open(self.backup_path, "wb"))
10 changes: 3 additions & 7 deletions discrete_optimization/generic_tools/cp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def solve(
optimisation_level=parameters_cp.optimisation_level,
)
except Exception as e:
if len(output_type.res.list_solution_fits) > 0:
if len(output_type.res) > 0:
self.status_solver = StatusSolver.SATISFIED
else:
self.status_solver = StatusSolver.UNKNOWN
Expand Down Expand Up @@ -394,7 +394,7 @@ def __init__(self, _output_item: Optional[str] = None, **kwargs: Any):
fit = self.solver.aggreg_from_sol(self.solution)

# update class attributes to remember step number and global resultstorage
self.__class__.res.add_solution(solution=self.solution, fitness=fit)
self.__class__.res.append((self.solution, fit))
self.__class__.step += 1

# callback: step end
Expand Down Expand Up @@ -427,10 +427,6 @@ def generate_subclass_for_solve(
solver=solver,
callback=callback,
step=0,
res=ResultStorage(
[],
mode_optim=solver.params_objective_function.sense_function,
limit_store=False,
),
res=solver.create_result_storage(),
),
)
22 changes: 21 additions & 1 deletion discrete_optimization/generic_tools/do_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
from __future__ import annotations # see annotations as str

from abc import abstractmethod
from typing import Any, List, Optional
from typing import Any, List, Optional, Tuple

from discrete_optimization.generic_tools.callbacks.callback import Callback
from discrete_optimization.generic_tools.do_problem import (
ParamsObjectiveFunction,
Problem,
Solution,
build_aggreg_function_and_params_objective,
)
from discrete_optimization.generic_tools.hyperparameters.hyperparametrizable import (
Hyperparametrizable,
)
from discrete_optimization.generic_tools.result_storage.result_storage import (
ResultStorage,
fitness_class,
)


Expand Down Expand Up @@ -61,6 +63,24 @@ def solve(
"""
...

def create_result_storage(
self, list_solution_fits: Optional[List[Tuple[Solution, fitness_class]]] = None
) -> ResultStorage:
"""Create a result storage with the proper mode_optim.

Args:
list_solution_fits:

Returns:

"""
if list_solution_fits is None:
list_solution_fits = []
return ResultStorage(
list_solution_fits=list_solution_fits,
mode_optim=self.params_objective_function.sense_function,
)

def init_model(self, **kwargs: Any) -> None:
"""Initialize intern model used to solve.

Expand Down
6 changes: 2 additions & 4 deletions discrete_optimization/generic_tools/ea/alternating_ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def solve(self, **kwargs: Any) -> ResultStorage:
"ga_solver.solve().get_best_solution() should not be None!"
)
problem_sol = tmp_sol
result_storage = ResultStorage(
list_solution_fits=[(problem_sol, self.aggreg_from_sol(problem_sol))],
best_solution=problem_sol,
mode_optim=self.params_objective_function.sense_function,
result_storage = self.create_result_storage(
[(problem_sol, self.aggreg_from_sol(problem_sol))],
)
return result_storage
6 changes: 2 additions & 4 deletions discrete_optimization/generic_tools/ea/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,7 @@ def solve(self, **kwargs: Any) -> ResultStorage:
kwargs = {self._encoding_variable_name: s_pure_int, "problem": self.problem}
problem_sol = self.problem.get_solution_type()(**kwargs)

result_storage = ResultStorage(
list_solution_fits=[(problem_sol, self.aggreg_from_sol(problem_sol))],
best_solution=problem_sol,
mode_optim=self.params_objective_function.sense_function,
result_storage = self.create_result_storage(
[(problem_sol, self.aggreg_from_sol(problem_sol))],
)
return result_storage
7 changes: 1 addition & 6 deletions discrete_optimization/generic_tools/ea/nsga.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,4 @@ def solve(self, **kwargs: Any) -> ResultStorage:
problem_sol = self.problem.get_solution_type()(**kwargs)
fits = self.aggreg_from_sol(problem_sol)
sols.append((problem_sol, fits))
rs = ResultStorage(
list_solution_fits=sols,
best_solution=None,
mode_optim=self.params_objective_function.sense_function,
)
return rs
return self.create_result_storage(sols)
Loading