This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add SolverConfigOverride support
- Added missing type info to Solver API Classes - Unlike Java, the SolverConfig in Python is Generic, since: - Python users do not need to specify types of variables, and no warnings are emitted for using a raw type - Allows a smart enough type checker to deduce the generic type of a SolverFactory, SolverManager, etc. from the generic type of the SolverConfig
- Loading branch information
1 parent
244e1d9
commit 196d76f
Showing
7 changed files
with
188 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from timefold.solver.api import * | ||
from timefold.solver.annotation import * | ||
from timefold.solver.config import * | ||
from timefold.solver.constraint import * | ||
from timefold.solver.score import * | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Annotated, List | ||
|
||
|
||
@planning_entity | ||
@dataclass | ||
class Entity: | ||
code: Annotated[str, PlanningId] | ||
value: Annotated[int, PlanningVariable] = field(default=None, compare=False) | ||
|
||
|
||
@constraint_provider | ||
def my_constraints(constraint_factory: ConstraintFactory): | ||
return [ | ||
constraint_factory.for_each(Entity) | ||
.reward(SimpleScore.ONE, lambda entity: entity.value) | ||
.as_constraint('Maximize value'), | ||
] | ||
|
||
|
||
@planning_solution | ||
@dataclass | ||
class Solution: | ||
entities: Annotated[List[Entity], PlanningEntityCollectionProperty] | ||
value_range: Annotated[List[int], ValueRangeProvider] | ||
score: Annotated[SimpleScore, PlanningScore] = field(default=None) | ||
|
||
def __str__(self) -> str: | ||
return str(self.entities) | ||
|
||
|
||
def test_solver_config_override(): | ||
solver_config = SolverConfig( | ||
solution_class=Solution, | ||
entity_class_list=[Entity], | ||
score_director_factory_config=ScoreDirectorFactoryConfig( | ||
constraint_provider_function=my_constraints, | ||
), | ||
termination_config=TerminationConfig( | ||
best_score_limit='9' | ||
) | ||
) | ||
solver_factory = SolverFactory.create(solver_config) | ||
solver = solver_factory.build_solver(SolverConfigOverride( | ||
termination_config=TerminationConfig( | ||
best_score_limit='3' | ||
) | ||
)) | ||
problem = Solution([Entity('A')], [1, 2, 3]) | ||
solution = solver.solve(problem) | ||
assert solution.score.score() == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.