From 1eab762a1ead1fbc5af1b3a42933e7de7fcc9036 Mon Sep 17 00:00:00 2001 From: Christopher Chianelli Date: Thu, 25 Apr 2024 11:31:27 -0400 Subject: [PATCH] chore: Use properties to define score properties --- tests/test_anchors.py | 2 +- tests/test_collectors.py | 6 +- tests/test_constraint_streams.py | 96 +++++++++---------- tests/test_custom_shadow_variables.py | 4 +- tests/test_domain.py | 42 ++++---- tests/test_easy_score_calculator.py | 2 +- tests/test_incremental_score_calculator.py | 18 ++-- tests/test_inverse_relation.py | 2 +- tests/test_pinning.py | 4 +- tests/test_solution_manager.py | 12 +-- tests/test_solver_configuration.py | 2 +- tests/test_solver_events.py | 6 +- tests/test_solver_factory.py | 2 +- tests/test_solver_manager.py | 6 +- tests/test_solver_problem_change.py | 4 +- .../src/main/python/score/__init__.py | 88 +++++++++++++---- 16 files changed, 177 insertions(+), 119 deletions(-) diff --git a/tests/test_anchors.py b/tests/test_anchors.py index fac8923..671ea54 100644 --- a/tests/test_anchors.py +++ b/tests/test_anchors.py @@ -99,7 +99,7 @@ # ChainedEntity('3'), # ] # )) -# assert solution.score.score() == 9 +# assert solution.score.score == 9 # anchor = solution.entities[0].anchor # assert anchor is not None # anchor_value_count = 0 diff --git a/tests/test_collectors.py b/tests/test_collectors.py index e566b6b..98d1a77 100644 --- a/tests/test_collectors.py +++ b/tests/test_collectors.py @@ -271,7 +271,7 @@ def define_constraints(constraint_factory: ConstraintFactory): entity_d.value = value_7 entity_e.value = value_9 - assert score_manager.explain(problem).score.score() == 5 + assert score_manager.explain(problem).score.score == 5 entity_a.value = value_1 entity_b.value = value_2 @@ -279,7 +279,7 @@ def define_constraints(constraint_factory: ConstraintFactory): entity_d.value = value_4 entity_e.value = value_5 - assert score_manager.explain(problem).score.score() == 25 + assert score_manager.explain(problem).score.score == 25 entity_a.value = value_1 entity_b.value = value_2 @@ -287,7 +287,7 @@ def define_constraints(constraint_factory: ConstraintFactory): entity_d.value = value_5 entity_e.value = value_6 - assert score_manager.explain(problem).score.score() == 13 + assert score_manager.explain(problem).score.score == 13 def test_to_list(): diff --git a/tests/test_constraint_streams.py b/tests/test_constraint_streams.py index 1fb9564..3237f86 100644 --- a/tests/test_constraint_streams.py +++ b/tests/test_constraint_streams.py @@ -67,15 +67,15 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 2 + assert score_manager.explain(problem).score.score == 2 def test_filter_uni(): @@ -97,16 +97,16 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 2 + assert score_manager.explain(problem).score.score == 2 def test_filter_bi(): @@ -129,16 +129,16 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 def test_filter_tri(): @@ -164,19 +164,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b, entity_c], [value_1, value_2, value_3]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_c.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_c.value = value_3 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 def test_filter_quad(): @@ -206,22 +206,22 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b, entity_c, entity_d], [value_1, value_2, value_3, value_4]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_c.value = value_3 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_d.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_d.value = value_4 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 def test_join_uni(): @@ -248,7 +248,7 @@ def define_constraints(constraint_factory: ConstraintFactory): entity_a1.value = value_1 - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a1.value = value_1 entity_a2.value = value_1 @@ -257,13 +257,13 @@ def define_constraints(constraint_factory: ConstraintFactory): entity_b2.value = value_2 # 1 * 1 + 1 * 1 + 2 * 2 + 2 * 2 - assert score_manager.explain(problem).score.score() == 10 + assert score_manager.explain(problem).score.score == 10 entity_a1.value = value_2 entity_b1.value = value_1 # 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 - assert score_manager.explain(problem).score.score() == 8 + assert score_manager.explain(problem).score.score == 8 def test_map(): @@ -285,19 +285,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 2 + assert score_manager.explain(problem).score.score == 2 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 3 + assert score_manager.explain(problem).score.score == 3 def test_multi_map(): @@ -319,19 +319,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 11 + assert score_manager.explain(problem).score.score == 11 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 23 + assert score_manager.explain(problem).score.score == 23 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 33 + assert score_manager.explain(problem).score.score == 33 def test_expand(): @@ -353,19 +353,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 2 + assert score_manager.explain(problem).score.score == 2 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 3 + assert score_manager.explain(problem).score.score == 3 def test_multi_expand(): @@ -387,19 +387,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 11 + assert score_manager.explain(problem).score.score == 11 entity_b.value = value_1 - assert score_manager.explain(problem).score.score() == 23 + assert score_manager.explain(problem).score.score == 23 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 33 + assert score_manager.explain(problem).score.score == 33 def test_concat(): @@ -423,19 +423,19 @@ def define_constraints(constraint_factory: ConstraintFactory): problem = Solution([entity_a, entity_b], [value_1, value_2, value_3]) - assert score_manager.explain(problem).score.score() == 0 + assert score_manager.explain(problem).score.score == 0 entity_a.value = value_1 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 entity_b.value = value_2 - assert score_manager.explain(problem).score.score() == 2 + assert score_manager.explain(problem).score.score == 2 entity_b.value = value_3 - assert score_manager.explain(problem).score.score() == 1 + assert score_manager.explain(problem).score.score == 1 def test_custom_indictments(): @@ -471,7 +471,7 @@ def define_constraints(constraint_factory: ConstraintFactory): value_indictment = indictments[1] assert a_indictment.indicted_object == MyIndictment('A') - assert a_indictment.score.score() == 1 + assert a_indictment.score.score == 1 assert a_indictment.constraint_match_count == 1 assert a_indictment.constraint_match_set == { ConstraintMatch(constraint_ref=ConstraintRef(package_name='my_package', constraint_name='Maximize value'), @@ -484,7 +484,7 @@ def define_constraints(constraint_factory: ConstraintFactory): } assert b_indictment.indicted_object == MyIndictment('B') - assert b_indictment.score.score() == 1 + assert b_indictment.score.score == 1 assert b_indictment.constraint_match_count == 1 assert b_indictment.constraint_match_set == { ConstraintMatch(constraint_ref=ConstraintRef(package_name='my_package', constraint_name='Maximize value'), @@ -497,7 +497,7 @@ def define_constraints(constraint_factory: ConstraintFactory): } assert value_indictment.indicted_object == 1 - assert value_indictment.score.score() == 2 + assert value_indictment.score.score == 2 assert value_indictment.constraint_match_count == 2 assert value_indictment.constraint_match_set == { ConstraintMatch(constraint_ref=ConstraintRef(package_name='my_package', constraint_name='Maximize value'), diff --git a/tests/test_custom_shadow_variables.py b/tests/test_custom_shadow_variables.py index 952bf43..2570c4f 100644 --- a/tests/test_custom_shadow_variables.py +++ b/tests/test_custom_shadow_variables.py @@ -57,7 +57,7 @@ class MySolution: solver = solver_factory.build_solver() problem = MySolution([MyPlanningEntity()], [1, 2, 3]) solution: MySolution = solver.solve(problem) - assert solution.score.score() == 1 + assert solution.score.score == 1 assert solution.entity_list[0].value == 2 assert solution.entity_list[0].value_squared == 4 @@ -117,6 +117,6 @@ class MySolution: solver = solver_factory.build_solver() problem = MySolution([MyPlanningEntity()], [1, 2, 3]) solution: MySolution = solver.solve(problem) - assert solution.score.score() == 1 + assert solution.score.score == 1 assert solution.entity_list[0].value == 2 assert solution.entity_list[0].value_squared == 4 diff --git a/tests/test_domain.py b/tests/test_domain.py index 72e1963..f8802a6 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -71,7 +71,7 @@ class Solution: solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 3 + assert solution.score.score == 3 assert solution.entities[0].value == v1 assert solution.entities[1].value == v1 assert solution.entities[2].value == v1 @@ -147,8 +147,10 @@ class Solution: solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.hard_score() == 1 - assert solution.score.soft_score() == 1 + assert solution.score.is_feasible + assert solution.score.init_score == 0 + assert solution.score.hard_score == 1 + assert solution.score.soft_score == 1 assert solution.entities[0].value == v1 or solution.entities[0].value is None assert solution.entities[1].value == v1 or solution.entities[1].value is None @@ -209,7 +211,7 @@ class Solution: solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 2 + assert solution.score.score == 2 assert solution.entities[0].value == v1 assert solution.entities[1].value == v1 @@ -391,7 +393,7 @@ class Solution: problem: Solution = Solution(Entity('A'), Value('1'), ['1', '2', '3']) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 1 + assert solution.score.score == 1 assert solution.entity.value == '1' @@ -446,26 +448,26 @@ class Solution: entity_4.value = value_1 entity_5.value = value_1 - assert score_manager.update(problem).score() == 4 + assert score_manager.update(problem).score == 4 entity_5.value = value_2 - assert score_manager.update(problem).score() == 4 + assert score_manager.update(problem).score == 4 entity_1.value = value_2 - assert score_manager.update(problem).score() == 5 + assert score_manager.update(problem).score == 5 entity_2.value = value_2 - assert score_manager.update(problem).score() == 6 + assert score_manager.update(problem).score == 6 entity_3.value = value_2 - assert score_manager.update(problem).score() == 7 + assert score_manager.update(problem).score == 7 entity_4.value = value_2 - assert score_manager.update(problem).score() == 8 + assert score_manager.update(problem).score == 8 entity_1.value = value_3 - assert score_manager.update(problem).score() == 9 + assert score_manager.update(problem).score == 9 def test_tuple_group_by_key(): @@ -526,7 +528,7 @@ class Solution: ['1', '2', '3']) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == len(entity_list) + assert solution.score.score == len(entity_list) for entity in solution.entity_list: assert entity.value == '1' @@ -583,7 +585,7 @@ class Solution: problem: Solution = Solution(Entity('A'), [pointer1, pointer2, pointer3]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 3 + assert solution.score.score == 3 assert solution.entity.value is pointer1 @@ -648,7 +650,7 @@ class Solution: ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 encountered = set() for entity in solution.entities: @@ -714,7 +716,7 @@ class Solution: ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 encountered = set() for entity in solution.entities: @@ -782,7 +784,7 @@ class Solution: ]) score_manager = SolutionManager.create(SolverFactory.create(solver_config)) score = score_manager.update(problem) - assert score.score() == -1 + assert score.score == -1 def test_entity_value_range_provider(): @@ -842,7 +844,7 @@ class Solution: ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 encountered = set() for entity in solution.entities: @@ -901,7 +903,7 @@ class Solution: ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 encountered = set() for entity in solution.entities: @@ -952,5 +954,5 @@ class Solution: problem: Solution = Solution(Entity('A'), [1, 2, 3]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 assert solution.entity.value == [1, 2, 3] diff --git a/tests/test_easy_score_calculator.py b/tests/test_easy_score_calculator.py index a0a149a..b14214f 100644 --- a/tests/test_easy_score_calculator.py +++ b/tests/test_easy_score_calculator.py @@ -41,7 +41,7 @@ def my_score_calculator(solution: Solution) -> SimpleScore: problem: Solution = Solution([Entity('A'), Entity('B'), Entity('C')], [1, 2, 3]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 9 + assert solution.score.score == 9 assert solution.entity_list[0].value == 3 assert solution.entity_list[1].value == 3 assert solution.entity_list[2].value == 3 diff --git a/tests/test_incremental_score_calculator.py b/tests/test_incremental_score_calculator.py index cd610ce..b65591b 100644 --- a/tests/test_incremental_score_calculator.py +++ b/tests/test_incremental_score_calculator.py @@ -133,7 +133,7 @@ def calculate_score(self) -> HardSoftScore: [0, 1, 2, 3]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 for i in range(4): for j in range(i + 1, 4): left_queen = solution.queen_list[i] @@ -274,7 +274,7 @@ def get_indictment_map(self): solver_factory = SolverFactory.create(solver_config) solver = solver_factory.build_solver() solution = solver.solve(problem) - assert solution.score.score() == 0 + assert solution.score.score == 0 for i in range(4): for j in range(i + 1, 4): left_queen = solution.queen_list[i] @@ -289,23 +289,23 @@ def get_indictment_map(self): row_conflict = constraint_match_total_map.get('NQueens/Row Conflict') ascending_diagonal_conflict = constraint_match_total_map.get('NQueens/Ascending Diagonal Conflict') descending_diagonal_conflict = constraint_match_total_map.get('NQueens/Descending Diagonal Conflict') - assert row_conflict.score.score() == 0 - assert ascending_diagonal_conflict.score.score() == 0 - assert descending_diagonal_conflict.score.score() == 0 + assert row_conflict.score.score == 0 + assert ascending_diagonal_conflict.score.score == 0 + assert descending_diagonal_conflict.score.score == 0 bad_solution = Solution(4, [Queen('A', 0, 0), Queen('B', 1, 1), Queen('C', 2, 0), Queen('D', 3, 1)], [0, 1, 2, 3], [0, 1, 2, 3]) score_explanation = score_manager.explain(bad_solution) - assert score_explanation.score.score() == -5 + assert score_explanation.score.score == -5 constraint_match_total_map = score_explanation.constraint_match_total_map row_conflict = constraint_match_total_map.get('NQueens/Row Conflict') ascending_diagonal_conflict = constraint_match_total_map.get('NQueens/Ascending Diagonal Conflict') descending_diagonal_conflict = constraint_match_total_map.get('NQueens/Descending Diagonal Conflict') - assert row_conflict.score.score() == -2 # (A, C), (B, D) - assert ascending_diagonal_conflict.score.score() == -1 # (B, C) - assert descending_diagonal_conflict.score.score() == -2 # (A, B), (C, D) + assert row_conflict.score.score == -2 # (A, C), (B, D) + assert ascending_diagonal_conflict.score.score == -1 # (B, C) + assert descending_diagonal_conflict.score.score == -2 # (A, B), (C, D) indictment_map = score_explanation.indictment_map assert indictment_map.get(bad_solution.queen_list[0]).constraint_match_count == 2 assert indictment_map.get(bad_solution.queen_list[1]).constraint_match_count == 3 diff --git a/tests/test_inverse_relation.py b/tests/test_inverse_relation.py index 495ea77..6722444 100644 --- a/tests/test_inverse_relation.py +++ b/tests/test_inverse_relation.py @@ -77,7 +77,7 @@ def test_inverse_relation(): InverseRelationEntity('3'), ] )) - assert solution.score.score() == 0 + assert solution.score.score == 0 visited_set = set() for value in solution.values: assert len(value.entities) == 1 diff --git a/tests/test_pinning.py b/tests/test_pinning.py index 99199ef..dbdbc32 100644 --- a/tests/test_pinning.py +++ b/tests/test_pinning.py @@ -51,7 +51,7 @@ def my_constraints(constraint_factory: ConstraintFactory): ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == -2 + assert solution.score.score == -2 def test_planning_pin(): @@ -94,4 +94,4 @@ def my_constraints(constraint_factory: ConstraintFactory): ]) solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == -2 + assert solution.score.score == -2 diff --git a/tests/test_solution_manager.py b/tests/test_solution_manager.py index fe3605c..0670c5e 100644 --- a/tests/test_solution_manager.py +++ b/tests/test_solution_manager.py @@ -44,7 +44,7 @@ class Solution: def assert_score_explanation(problem: Solution, score_explanation: ScoreExplanation[Solution]): assert score_explanation.solution is problem - assert score_explanation.score.score() == 3 + assert score_explanation.score.score == 3 constraint_ref = ConstraintRef(package_name='package', constraint_name='Maximize Value') constraint_match_total_map = score_explanation.constraint_match_total_map @@ -92,8 +92,8 @@ def assert_score_explanation(problem: Solution, def assert_constraint_analysis(problem: Solution, constraint_analysis: ConstraintAnalysis): constraint_ref = ConstraintRef(package_name='package', constraint_name='Maximize Value') - assert constraint_analysis.score.score() == 3 - assert constraint_analysis.weight.score() == 1 + assert constraint_analysis.score.score == 3 + assert constraint_analysis.weight.score == 1 assert constraint_analysis.constraint_name == constraint_ref.constraint_name assert constraint_analysis.constraint_package == constraint_ref.package_name assert constraint_analysis.constraint_ref == constraint_ref @@ -114,7 +114,7 @@ def assert_constraint_analysis(problem: Solution, constraint_analysis: Constrain def assert_score_analysis(problem: Solution, score_analysis: ScoreAnalysis): constraint_ref = ConstraintRef(package_name='package', constraint_name='Maximize Value') - assert score_analysis.score.score() == 3 + assert score_analysis.score.score == 3 constraint_map = score_analysis.constraint_map assert len(constraint_map) == 1 @@ -132,8 +132,8 @@ def assert_solution_manager(solution_manager: SolutionManager[Solution]): problem: Solution = Solution([Entity('A', 1), Entity('B', 1), Entity('C', 1)], [1, 2, 3]) assert problem.score is None score = solution_manager.update(problem) - assert score.score() == 3 - assert problem.score.score() == 3 + assert score.score == 3 + assert problem.score.score == 3 score_explanation = solution_manager.explain(problem) assert_score_explanation(problem, score_explanation) diff --git a/tests/test_solver_configuration.py b/tests/test_solver_configuration.py index f2cd3cf..abc47d9 100644 --- a/tests/test_solver_configuration.py +++ b/tests/test_solver_configuration.py @@ -54,5 +54,5 @@ class Solution: solver = SolverFactory.create(solver_config).build_solver() solution = solver.solve(problem) - assert solution.score.score() == 6 + assert solution.score.score == 6 assert solution.entities[0].value == 3 diff --git a/tests/test_solver_events.py b/tests/test_solver_events.py index b2f3fb7..02214bb 100644 --- a/tests/test_solver_events.py +++ b/tests/test_solver_events.py @@ -53,12 +53,12 @@ def on_best_solution_changed(event): solver.add_event_listener(on_best_solution_changed) solution = solver.solve(problem) - assert solution.score.score() == 6 + assert solution.score.score == 6 assert solution.entities[0].value == 3 assert solution.entities[1].value == 3 assert len(score_list) == len(solution_list) assert len(solution_list) == 1 - assert score_list[0].score() == 6 - assert solution_list[0].score.score() == 6 + assert score_list[0].score == 6 + assert solution_list[0].score.score == 6 assert solution_list[0].entities[0].value == 3 assert solution_list[0].entities[1].value == 3 diff --git a/tests/test_solver_factory.py b/tests/test_solver_factory.py index b10b9a0..8cd6dbc 100644 --- a/tests/test_solver_factory.py +++ b/tests/test_solver_factory.py @@ -54,4 +54,4 @@ def test_solver_config_override(): )) problem = Solution([Entity('A')], [1, 2, 3]) solution = solver.solve(problem) - assert solution.score.score() == 3 + assert solution.score.score == 3 diff --git a/tests/test_solver_manager.py b/tests/test_solver_manager.py index dda057b..b92d761 100644 --- a/tests/test_solver_manager.py +++ b/tests/test_solver_manager.py @@ -88,7 +88,7 @@ def assert_solver_run(solver_manager, solver_job): assert solver_manager.get_solver_status(1) != SolverStatus.NOT_SOLVING lock.release() solution = solver_job.get_final_best_solution() - assert solution.score.score() == 6 + assert solution.score.score == 6 value_list = [entity.value.value for entity in solution.entity_list] assert 1 in value_list assert 2 in value_list @@ -100,7 +100,7 @@ def assert_problem_change_solver_run(solver_manager, solver_job): solver_manager.add_problem_change(1, UseOnlyEntityAndValueProblemChange(Entity('D'), Value(6))) lock.release() solution = solver_job.get_final_best_solution() - assert solution.score.score() == 6 + assert solution.score.score == 6 assert len(solution.entity_list) == 1 assert len(solution.value_list) == 1 assert solution.entity_list[0].code == 'D' @@ -327,4 +327,4 @@ class Solution: .run()) solution = solver_job.get_final_best_solution() - assert solution.score.score() == 3 + assert solution.score.score == 3 diff --git a/tests/test_solver_problem_change.py b/tests/test_solver_problem_change.py index 347be02..b2c7832 100644 --- a/tests/test_solver_problem_change.py +++ b/tests/test_solver_problem_change.py @@ -98,7 +98,7 @@ def do_solve(problem: Solution): assert result is not None assert len(result.entities) == 2 - assert result.score.score() == 6 + assert result.score.score == 6 def test_remove_entity(): @@ -132,4 +132,4 @@ def do_solve(problem: Solution): assert result is not None assert len(result.entities) == 1 - assert result.score.score() == -1 + assert result.score.score == -1 diff --git a/timefold-solver-python-core/src/main/python/score/__init__.py b/timefold-solver-python-core/src/main/python/score/__init__.py index e06d466..6c01e97 100644 --- a/timefold-solver-python-core/src/main/python/score/__init__.py +++ b/timefold-solver-python-core/src/main/python/score/__init__.py @@ -1,6 +1,6 @@ from .._timefold_java_interop import ensure_init -from typing import TYPE_CHECKING, List -from jpype import JImplementationFor +from typing import TYPE_CHECKING +from jpype import JImplementationFor, JOverride import jpype.imports # noqa if TYPE_CHECKING: @@ -9,49 +9,84 @@ class SimpleScore(Score): ZERO: 'SimpleScore' = None ONE: 'SimpleScore' = None + init_score: int + is_feasible: bool + score: int - def score(self) -> int: + @staticmethod + def of(score: int, /) -> 'SimpleScore': ... class HardSoftScore(Score): ZERO: 'HardSoftScore' = None ONE_HARD: 'HardSoftScore' = None ONE_SOFT: 'HardSoftScore' = None + init_score: int + is_feasible: bool + hard_score: int + soft_score: int - def hard_score(self) -> int: + @staticmethod + def of(hard_score: int, soft_score: int, /) -> 'HardSoftScore': ... - def soft_score(self) -> int: - ... class HardMediumSoftScore(Score): ZERO: 'HardMediumSoftScore' = None ONE_HARD: 'HardMediumSoftScore' = None ONE_MEDIUM: 'HardMediumSoftScore' = None ONE_SOFT: 'HardMediumSoftScore' = None - - def hard_score(self) -> int: + init_score: int + is_feasible: bool + hard_score: int + medium_score: int + soft_score: int + + @staticmethod + def of(self, hard_score: int, medium_score: int, soft_score: int, /) -> 'HardMediumSoftScore': ... - def medium_score(self) -> int: - ... + class BendableScore(Score): + init_score: int + is_feasible: bool + hard_scores: list[int] + soft_scores: list[int] - def soft_score(self) -> int: + @staticmethod + def of(hard_scores: list[int], soft_scores: list[int], /) -> 'BendableScore': ... - class BendableScore(Score): - def hard_scores(self) -> List[int]: - ... - def soft_scores(self) -> List[int]: - ... +@JImplementationFor('ai.timefold.solver.core.api.score.buildin.simple.SimpleScore') +class _SimpleScoreImpl: + @property + def init_score(self) -> int: + return self.initScore() + + @property + def is_feasible(self) -> bool: + return self.isFeasible() + + @property + def score(self): + return self.toLevelNumbers()[0] # noqa @JImplementationFor('ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore') class _HardSoftScoreImpl: + @property + def init_score(self) -> int: + return self.initScore() + + @property + def is_feasible(self) -> bool: + return self.isFeasible() + + @property def hard_score(self): return self.hardScore() # noqa + @property def soft_score(self): return self.softScore() # noqa @@ -59,21 +94,42 @@ def soft_score(self): @JImplementationFor('ai.timefold.solver.core.api.score.buildin.hardmediumsoft.' 'HardMediumSoftScore') class _HardMediumSoftScoreImpl: + @property + def init_score(self) -> int: + return self.initScore() + + @property + def is_feasible(self) -> bool: + return self.isFeasible() + + @property def hard_score(self): return self.hardScore() # noqa + @property def medium_score(self): return self.mediumScore() # noqa + @property def soft_score(self): return self.softScore() # noqa @JImplementationFor('ai.timefold.solver.core.api.score.buildin.bendable.BendableScore') class _BendableScoreImpl: + @property + def init_score(self) -> int: + return self.initScore() + + @property + def is_feasible(self) -> bool: + return self.isFeasible() + + @property def hard_scores(self): return self.hardScores() # noqa + @property def soft_scores(self): return self.softScores() # noqa