-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
140 lines (120 loc) · 3.47 KB
/
sketch.js
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
// populationSize, change this to experiment around
var populationSize = 1000;
// set this to true if you want to implement your own database
var enableDatabase = false;
// number of columns
// this also applies to row
var cols=8;
// variable initialization
var scl;
var population;
var table;
var stats;
var chromosomes;
var tourslog;
var generations = [];
var knightmoves = [];
var toursdata;
// setup is a p5js function that
// will run only once
function setup() {
// create a 800x800 pixel canvas
createCanvas(800,800);
// set the background to black
background(0);
// set fill color to white
fill(255);
// set scale to be width of canvas divided by number of columns
scl=width/cols;
// initialize new table
table = new Table();
// initialize new population with the population size
population = new Population(populationSize);
// display the table
table.show();
// create a paragraph element
stats = createP("Stats");
// place the paragraph on 850, 10
stats.position(850,10);
// add class "stats" to the paragraph
stats.class("stats");
// rename paragraph element to say GENERATION 0
stats.html("GENERATION 0");
// create a new paragraph element
chromosomes = createP("Chromosomes");
chromosomes.position(850,40);
chromosomes.class("chromosomes");
chromosomes.html("Chromosomes");
if(enableDatabase) {
// create a new paragraph element
toursdata = createP("");
toursdata.position(850,70);
toursdata.class("toursdata");
// create config key to connect with the firebase database
// to store existing tours
// change the config according to your apiKey (in constants.js)
// NOTE: DO NOT STORE API KEYS IN YOUR REPOSITORY
var config = {
apiKey: API_KEY,
authDomain: AUTH_DOMAIN,
databaseURL: DATABASE_URL,
storageBucket: STORAGE_BUCKET,
messagingSenderId: MESSAGING_SENDER_ID
};
// initialize firebase
firebase.initializeApp(config);
var database = firebase.database();
tourslog = database.ref('tourslog');
var ref = database.ref('tourslog');
// fetch data from firebase to show how many valid tours that we have covered
ref.on('value',gotData,errData);
}
}
// gotData is the function to process the fetched tours from the database
function gotData(data) {
generations = [];
knightmoves = [];
var average=0;
var addtoCount;
var tourscount = 0;
var tours = data.val();
var keys = Object.keys(tours);
for(var i=0; i<keys.length; i++) {
var k=keys[i];
generations.push(tours[k].knightgen);
knightmoves.push(tours[k].moves);
}
for(var i=0; i<knightmoves.length; i++) {
addtoCount=true;
for(var j=0; j<i; j++) {
if(knightmoves[i]===knightmoves[j]) {
addtoCount=false;
}
}
if(addtoCount) {
tourscount++;
}
}
for(var i=0; i<generations.length; i++) {
average+=generations[i];
}
average/=generations.length;
toursdata.html("We have found " + tourscount + " different tours with an average of "+ average + " generations.");
}
function errData(err) {
console.log("Error!");
console.log(err);
}
// draw function is the p5js function
// that will constantly loop
function draw() {
// run a generation of the genetic algorithm
population.run();
// refresh the table
table.show();
// evaluate the GA's generation
// display the best candidate on the board
population.evaluate();
// select the candidates for the next GA generation
population.selection();
}