-
Notifications
You must be signed in to change notification settings - Fork 0
/
pessimistic_combinator.go
217 lines (180 loc) · 6.1 KB
/
pessimistic_combinator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package solver
import (
"fmt"
"slices"
"github.com/caseymerrill/turingsolver/game"
"github.com/caseymerrill/turingsolver/set"
"github.com/caseymerrill/turingsolver/verifiers"
"gonum.org/v1/gonum/stat/combin"
)
func Combinator() *Solver {
return &Solver{
name: "Combinator",
codeStrategy: pessimisticCombinatorCodeStrategy,
verifierStrategy: pessimisticVerifierStrategy,
}
}
func Combinator1_1() *Solver {
return &Solver{
name: "Combinator1.1",
codeStrategy: combinator1_1CodeStrategy,
verifierStrategy: combinator1_1VerifierStrategy,
}
}
func Combinator2() *Solver {
panic("Not implemented")
return &Solver{
name: "Combinator2",
}
}
func pessimisticCombinatorCodeStrategy(s *Solver, code []int) int {
neededVerifiers := unsolvedVerifiers(s)
choose := min(3, len(neededVerifiers))
combinations := combin.Combinations(len(neededVerifiers), choose)
bestScore := 0
for _, combination := range combinations {
score := 0
for _, neededVerifierIndex := range combination {
score += s.verifierStrategy(s, neededVerifiers[neededVerifierIndex], code)
}
if score > bestScore {
bestScore = score
}
}
return bestScore
}
func combinator1_1CodeStrategy(s *Solver, code []int) int {
combinations := combin.Combinations(len(s.game.GetVerifierCards()), 3)
bestScore := 0
for _, combination := range combinations {
score := 0
for _, neededVerifierIndex := range combination {
score += s.verifierStrategy(s, neededVerifierIndex, code)
}
if score > bestScore {
bestScore = score
}
}
return bestScore
}
func scorer(solutions []game.Solution) int {
solutionCount := len(solutions)
codeCount := countCodes(solutions)
return codeCount*1_000_000 + solutionCount
}
func combinator1_1VerifierStrategy(s *Solver, verifierIndex int, code []int) int {
trueScore := scorer(s.adjustSolutions(code, verifierIndex, true))
falseScore := scorer(s.adjustSolutions(code, verifierIndex, false))
worstScore := max(trueScore, falseScore)
if worstScore == 0 {
return 0
}
return scorer(s.solutions) - worstScore
}
func combinator2(s *Solver, code []int) (int, []int) {
type possibility struct {
score int
possibleCodes set.Set[string]
verifiersIndexes []int
}
// currentSolutionCodeCount := countCodes(s.solutions)
currentCodeCount := countCodes(s.solutions)
neededVerifiers := unsolvedVerifiers(s)
choose := min(3, len(neededVerifiers))
combinations := combin.Permutations(len(neededVerifiers), choose)
var bestWorstCase possibility
for _, combination := range combinations {
resultsForThisCombination := make([]possibility, 0)
for _, neededVerifierIndex := range combination {
verifierIndex := neededVerifiers[neededVerifierIndex]
trueSolutions := s.adjustSolutions(code, verifierIndex, true)
trueCodes := solutions2CodeSet(trueSolutions)
falseSolutions := s.adjustSolutions(code, verifierIndex, false)
falseCodes := solutions2CodeSet(falseSolutions)
if len(resultsForThisCombination) == 0 {
if len(trueCodes) > 0 {
resultsForThisCombination = append(resultsForThisCombination, possibility{possibleCodes: trueCodes, verifiersIndexes: []int{verifierIndex}})
}
if len(falseCodes) > 0 {
resultsForThisCombination = append(resultsForThisCombination, possibility{possibleCodes: falseCodes, verifiersIndexes: []int{verifierIndex}})
}
} else {
newResultPermutations := make([]possibility, 0)
for _, resultPermutation := range resultsForThisCombination {
if len(resultPermutation.possibleCodes) == 1 {
continue
}
ifTrue := resultPermutation.possibleCodes.Intersection(trueCodes)
if len(ifTrue) > 0 {
newResultPermutations = append(newResultPermutations, possibility{possibleCodes: ifTrue, verifiersIndexes: append(resultPermutation.verifiersIndexes, verifierIndex)})
}
ifFalse := resultPermutation.possibleCodes.Intersection(falseCodes)
if len(ifFalse) > 0 {
newResultPermutations = append(newResultPermutations, possibility{possibleCodes: ifFalse, verifiersIndexes: append(resultPermutation.verifiersIndexes, verifierIndex)})
}
// Keep old possibility even if the next verifier isn't useful
if len(ifTrue) == 0 && len(ifFalse) == 0 {
newResultPermutations = append(newResultPermutations, resultPermutation)
}
}
resultsForThisCombination = newResultPermutations
}
}
if len(resultsForThisCombination) == 0 {
continue
}
worstCaseThisCombination := slices.MinFunc(resultsForThisCombination, func(s1, s2 possibility) int {
return len(s1.possibleCodes) - len(s2.possibleCodes)
})
if len(bestWorstCase.possibleCodes) == 0 || len(worstCaseThisCombination.possibleCodes) < len(bestWorstCase.possibleCodes) {
bestWorstCase = worstCaseThisCombination
}
}
if len(bestWorstCase.possibleCodes) == 0 {
return -1, nil
}
score := currentCodeCount - len(bestWorstCase.possibleCodes)
//fmt.Printf("%v %v %+v\n", code, score, bestWorstCase)
return score, bestWorstCase.verifiersIndexes
}
func unsolvedVerifiers(s *Solver) []int {
codes := set.Set[string]{}
unsolved := set.Set[int]{}
firstVerifier := make([]*verifiers.Verifier, len(s.solutions[0].Verifiers))
for _, solution := range s.solutions {
// Skip duplicate codes... don't care about solving the verifiers for the same code
solutionCode := code2str(solution.Code)
if codes.Contains(solutionCode) {
continue
} else {
codes.Add(solutionCode)
}
for verifierIndex, verifier := range solution.Verifiers {
if firstVerifier[verifierIndex] == nil {
firstVerifier[verifierIndex] = verifier
} else if verifier != firstVerifier[verifierIndex] {
unsolved.Add(verifierIndex)
}
}
}
asSlice := unsolved.ToSlice()
slices.Sort(asSlice)
return asSlice
}
func code2str(code []int) string {
codeStr := ""
for _, c := range code {
codeStr += fmt.Sprintf("%v", c)
}
return codeStr
}
func solutions2CodeSet(solutions []game.Solution) set.Set[string] {
codes := set.Set[string]{}
for _, solution := range solutions {
codes.Add(code2str(solution.Code))
}
return codes
}
func countCodes(solutions []game.Solution) int {
return len(solutions2CodeSet(solutions))
}