@@ -11,35 +11,43 @@ public class Organism
11
11
private Problem problem ;
12
12
13
13
public SolutionInformation Solution = new SolutionInformation { CostTotal = int . MaxValue } ;
14
+ private EvaluationState lastEvaluation ;
14
15
15
16
public Organism ( Solver solver , Problem problem )
16
17
{
17
18
this . solver = solver ;
18
19
this . problem = problem ;
19
20
20
21
root = new InspirationalBranch ( solver ) { LeftNode = new InspirationalBranch ( solver ) , RightNode = new InspirationalBranch ( solver ) } ;
21
- Solution = Evaluate ( root , problem ) ;
22
+ lastEvaluation = new EvaluationState ( ) ;
23
+ Solution = Evaluate ( root , lastEvaluation , problem ) ;
22
24
}
23
25
26
+ public bool IsSupersetOf ( Organism organism )
27
+ {
28
+ if ( lastEvaluation == null || organism . lastEvaluation == null ) return false ;
29
+
30
+ return lastEvaluation . IsSupersetOf ( organism . lastEvaluation ) ;
31
+ }
32
+
24
33
public override string ToString ( )
25
34
{
26
35
return root . GetNames ( ) ;
27
36
}
28
37
29
- public static SolutionInformation Evaluate ( InspirationalBranch root , Problem problem )
38
+ private static SolutionInformation Evaluate ( InspirationalBranch root , EvaluationState state , Problem problem )
30
39
{
31
- EvaluationState engine = new EvaluationState ( ) ;
32
- root . Evaluate ( engine ) ;
40
+ root . Evaluate ( state ) ;
33
41
34
42
var cost = new SolutionInformation ( ) ;
35
43
36
44
// Add the total inspiration cost
37
- cost . InspirationTotal += engine . Inspiration ;
45
+ cost . InspirationTotal += state . Inspiration ;
38
46
39
47
// Add extra cost for incomplete solutions
40
48
foreach ( var needed in problem . Proficiencies )
41
49
{
42
- cost . IncompletenessPenalty += Math . Max ( 0d , needed . Value - engine . GetValue ( needed . Key ) ) * 1000 ;
50
+ cost . IncompletenessPenalty += Math . Max ( 0d , needed . Value - state . GetValue ( needed . Key ) ) * 1000 ;
43
51
}
44
52
45
53
cost . CostTotal = cost . InspirationTotal + cost . IncompletenessPenalty ;
@@ -54,16 +62,21 @@ public void Tick()
54
62
clone = ( InspirationalBranch ) root . Clone ( ) ;
55
63
clone . Mutate ( ) ;
56
64
57
- var engineNew = new EvaluationState ( ) ;
58
- var engineOld = new EvaluationState ( ) ;
65
+ var stateNew = new EvaluationState ( ) ;
66
+ var stateOld = new EvaluationState ( ) ;
59
67
60
- var newResult = Evaluate ( clone , problem ) ;
61
- var oldResult = Evaluate ( root , problem ) ;
68
+ var newResult = Evaluate ( clone , stateNew , problem ) ;
69
+ var oldResult = Evaluate ( root , stateOld , problem ) ;
62
70
63
71
if ( newResult . CostTotal < oldResult . CostTotal || Helper . Mutate ( 10 ) )
64
72
{
65
73
root = clone ;
66
74
Solution = newResult ;
75
+ lastEvaluation = stateNew ;
76
+ }
77
+ else
78
+ {
79
+ lastEvaluation = stateOld ;
67
80
}
68
81
}
69
82
@@ -72,6 +85,7 @@ public Organism Clone()
72
85
Organism clone = new Organism ( solver , problem ) ;
73
86
clone . root = this . root . Clone ( ) ;
74
87
clone . Solution = this . Solution ;
88
+ clone . lastEvaluation = lastEvaluation ;
75
89
return clone ;
76
90
}
77
91
@@ -83,7 +97,9 @@ public Organism MakeCrossOver(Organism father)
83
97
motherDna . LeftNode = fatherDna . LeftNode ;
84
98
motherDna . RightNode = fatherDna . RightNode ;
85
99
86
- child . Solution = Evaluate ( child . root , problem ) ;
100
+ var state = new EvaluationState ( ) ;
101
+ child . Solution = Evaluate ( child . root , state , problem ) ;
102
+ child . lastEvaluation = state ;
87
103
return child ;
88
104
}
89
105
0 commit comments