File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -209,7 +209,9 @@ def default_score(
209
209
try :
210
210
return max (0 , min (1 , sol_score / gen_score ))
211
211
except ZeroDivisionError :
212
- return float (sol_score < 0 )
212
+ # if generator scored 0 then the solver will have achieved an equal or better score
213
+ # i.e. the Fight's score is simply 1 regardless of its solution score.
214
+ return 1
213
215
else :
214
216
return max (0 , min (1 , solution .score (instance , Role .solver )))
215
217
Original file line number Diff line number Diff line change 1
1
"""Tests for all util functions."""
2
+ from math import inf
2
3
import unittest
3
4
4
5
from algobattle .battle import Battle , Iterated , Averaged
6
+ from algobattle .problem import InstanceModel , SolutionModel , default_score
7
+ from algobattle .util import Role
8
+
9
+
10
+ class DummyInstance (InstanceModel ): # noqa: D101
11
+ @property
12
+ def size (self ) -> int :
13
+ return 1
14
+
15
+
16
+ class DummySolution (SolutionModel [DummyInstance ]): # noqa: D101
17
+ val : float
18
+
19
+ def score (self , instance : DummyInstance , role : Role ) -> float :
20
+ return self .val
5
21
6
22
7
23
class Utiltests (unittest .TestCase ):
@@ -12,6 +28,35 @@ def test_default_battle_types(self):
12
28
self .assertEqual (Battle .all ()["Iterated" ], Iterated )
13
29
self .assertEqual (Battle .all ()["Averaged" ], Averaged )
14
30
31
+ def test_default_fight_score (self ):
32
+ """Tests the default fight scoring function."""
33
+ instance = DummyInstance ()
34
+ scores = [
35
+ (0 , 0 , 1 ),
36
+ (0 , 2 , 1 ),
37
+ (0 , 4 , 1 ),
38
+ (0 , inf , 1 ),
39
+ (2 , 0 , 0 ),
40
+ (2 , 2 , 1 ),
41
+ (2 , 4 , 1 ),
42
+ (2 , inf , 1 ),
43
+ (4 , 0 , 0 ),
44
+ (4 , 2 , 0.5 ),
45
+ (4 , 4 , 1 ),
46
+ (4 , inf , 1 ),
47
+ (inf , 0 , 0 ),
48
+ (inf , 2 , 0 ),
49
+ (inf , 4 , 0 ),
50
+ (inf , inf , 1 ),
51
+ ]
52
+ for gen , sol , score in scores :
53
+ self .assertEqual (
54
+ default_score (
55
+ instance , generator_solution = DummySolution (val = gen ), solver_solution = DummySolution (val = sol )
56
+ ),
57
+ score ,
58
+ )
59
+
15
60
16
61
if __name__ == "__main__" :
17
62
unittest .main ()
You can’t perform that action at this time.
0 commit comments