-
Notifications
You must be signed in to change notification settings - Fork 1
/
islandHopBrowser.ts
122 lines (102 loc) · 2.6 KB
/
islandHopBrowser.ts
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
import { island_list, newGa, Gene, Island } from './islandHopCore'
import { populate } from '../src'
const { abs } = Math
const canvas = document.getElementById('canvas') as HTMLCanvasElement
const madeit = document.getElementById('madeit') as HTMLParagraphElement
const context = canvas.getContext('2d')!
if (!context) {
const msg = '2D Canvas Context is not supported'
document.body.innerHTML = msg
throw new Error(msg)
}
const MAX_SIZE = 600
const ga = newGa()
const { fitness, doesABeatB } = ga.options
const seed: Gene = { ...ga.options.population[0] }
function basic_ga() {
ga.options.doesABeatB = (a, b) => {
return fitness(a) >= fitness(b)
}
}
function diversity_ga() {
ga.options.doesABeatB = doesABeatB
}
function reset_population() {
ga.options.population = [{ ...seed }]
populate(ga.options)
}
// ********************** UI STUFF *************************
function drawCircle(x: number, y: number, s: number, color: number) {
context.fillStyle = 'hsla(' + color + ',90%,40%,1)'
context.strokeStyle = 'hsla(' + color + ',100%,20%,1)'
context.beginPath()
context.arc(
(x * MAX_SIZE) / 10,
(y * MAX_SIZE) / 10,
(s * MAX_SIZE) / 10,
0,
Math.PI * 2,
true,
)
context.fill()
context.stroke()
}
function drawIsland({ x, y, score }: Island) {
drawCircle(x, y, 0.4, 90)
drawCircle(x, y, 0.1, 90)
context.fillStyle = 'hsla(0,0%,0%,1)'
context.strokeStyle = 'hsla(0,0%,0%,1)'
context.fillText(
score.toString(),
(x * MAX_SIZE) / 10 - 5,
(y * MAX_SIZE) / 10 - 10,
)
}
function drawFrog(frog: Gene) {
const score = fitness(frog)
drawCircle(frog.x, frog.y, 0.1, score > 0 ? 270 : 320)
}
function draw() {
// clear the screen
context.fillStyle = 'hsla(180,90%,40%,1)'
context.fillRect(0, 0, canvas.width, canvas.height)
context.fill()
if (ga) {
for (const island of island_list) {
drawIsland(island)
}
for (const frog of ga.options.population) {
drawFrog(frog)
}
}
window.requestAnimationFrame(draw)
}
window.onload = function () {
basic_ga()
window.requestAnimationFrame(draw)
}
let doSimulation = false
function startSimulation() {
doSimulation = true
}
function stopSimulation() {
doSimulation = false
}
Object.assign(window, {
basic_ga,
diversity_ga,
reset_population,
startSimulation,
stopSimulation,
})
setInterval(function () {
if (!doSimulation) return
for (let x = 0; x < 10; x++) ga.evolve()
let acc = 0
ga.options.population.forEach(p => {
if (abs(p.x - 8.5) < 1 && abs(p.y - 8.5) < 1) {
acc++
}
})
madeit.innerText = acc + ' of ' + ga.options.population.length
}, 50)