This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (130 loc) · 4.15 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/BranLwyd/acnh_flowers/breedgraph"
"github.com/BranLwyd/acnh_flowers/flower"
)
const (
expandSteps = 4
)
func main() {
// Initial flowers.
roses := flower.Roses()
seedWhite := must(roses.ParseGenotype("rryyWwss")).ToGeneticDistribution()
seedYellow := must(roses.ParseGenotype("rrYYWWss")).ToGeneticDistribution()
seedRed := must(roses.ParseGenotype("RRyyWWSs")).ToGeneticDistribution()
blueRoseGeno := must(roses.ParseGenotype("RRYYwwss"))
blueRose := blueRoseGeno.ToGeneticDistribution()
candidatePredicate := func(gd flower.GeneticDistribution) bool {
isSuitable := true
gd.Visit(func(g flower.Genotype, _ uint64) bool {
if g != blueRoseGeno {
isSuitable = false
}
return isSuitable
})
return isSuitable
}
// Breeding tests.
tests := []*breedgraph.Test{breedgraph.NoTest}
tests = append(tests, breedgraph.PhenotypeTestsUpToSize(roses, 1)...)
g := breedgraph.NewGraph(tests, []flower.GeneticDistribution{seedWhite, seedYellow, seedRed})
for i := 0; i < expandSteps; i++ {
fmt.Fprintf(os.Stderr, "Beginning graph expansion step %d...\n", i+1)
keepPred := func(flower.GeneticDistribution) bool { return true }
if i == expandSteps-1 {
// On the last step, keep only if it's a solution
// candidate, since we won't be expanding any more from
// it.
keepPred = candidatePredicate
}
g.Expand(keepPred)
}
// Find candidate distribution, or fail out if this is impossible.
candidate, ok := g.Search(candidatePredicate)
if !ok {
fmt.Fprintf(os.Stderr, "No solution possible.\n")
os.Exit(1)
}
// Print result.
names := map[flower.GeneticDistribution]string{}
names[seedWhite] = "Seed White (rryyWwss)"
names[seedYellow] = "Seed Yellow (rrYYWWss)"
names[seedRed] = "Seed Red (RRyyWWSs)"
names[blueRose] = "Blue Roses (RRYYwwss)"
printDotGraphPathTo(roses, candidate, names)
}
func printGraph(s flower.Species, g *breedgraph.Graph, names map[flower.GeneticDistribution]string) {
name := func(gd flower.GeneticDistribution) string {
if name, ok := names[gd]; ok {
return name
}
name := s.RenderGeneticDistribution(gd)
names[gd] = name
return name
}
fmt.Println("All flowers:")
g.VisitVertices(func(v breedgraph.Vertex) {
fmt.Printf(" %s\n", name(v.Value()))
})
fmt.Println("Lineage:")
g.VisitEdges(func(e breedgraph.Edge) {
fmt.Printf(" %s and %s make %s [test = %q, cost = %.02f]\n", name(e.FirstParent().Value()), name(e.SecondParent().Value()), name(e.Child().Value()), e.Test().Name(), e.EdgeCost())
})
}
func printDotGraph(s flower.Species, g *breedgraph.Graph, names map[flower.GeneticDistribution]string) {
name := func(gd flower.GeneticDistribution) string {
if name, ok := names[gd]; ok {
return name
}
name := s.RenderGeneticDistribution(gd)
names[gd] = name
return name
}
// Print vertices.
fmt.Println("digraph {")
g.VisitVertices(func(v breedgraph.Vertex) {
fmt.Printf(` "%s"`, name(v.Value()))
fmt.Println()
})
fmt.Println()
// Print edges.
g.VisitEdges(func(e breedgraph.Edge) {
fmt.Printf(` {"%s" "%s"} -> "%s" [label="%s"]`, name(e.FirstParent().Value()), name(e.SecondParent().Value()), name(e.Child().Value()), edgeLabel(e.Test().Name(), e.EdgeCost()))
fmt.Println()
})
fmt.Println("}")
}
func printDotGraphPathTo(s flower.Species, v breedgraph.Vertex, names map[flower.GeneticDistribution]string) {
name := func(gd flower.GeneticDistribution) string {
if name, ok := names[gd]; ok {
return name
}
name := s.RenderGeneticDistribution(gd)
names[gd] = name
return name
}
// Print vertices.
fmt.Println("digraph {")
v.VisitPathTo(func(v breedgraph.Vertex) {
fmt.Printf(` "%s"`, name(v.Value()))
fmt.Println()
}, func(e breedgraph.Edge) {
fmt.Printf(` {"%s" "%s"} -> "%s" [label="%s"]`, name(e.FirstParent().Value()), name(e.SecondParent().Value()), name(e.Child().Value()), edgeLabel(e.Test().Name(), e.EdgeCost()))
fmt.Println()
})
fmt.Println("}")
}
func edgeLabel(test string, cost float64) string {
if test != "" {
return fmt.Sprintf("%s (%.2f)", test, cost)
}
return fmt.Sprintf("%.02f", cost)
}
func must(g flower.Genotype, err error) flower.Genotype {
if err != nil {
panic(err)
}
return g
}