forked from Brandon123b/NemaNodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNematodeTrainer.js
79 lines (61 loc) · 2.51 KB
/
NematodeTrainer.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
/* This class is used to train the nematodes by storing the smartest nematodes in a priority queue. This is also
* used to load the smart nematodes from a file and to download the smart nematodes to a file.
*
* Author: Brandon Hall
*/
class NematodeTrainer {
// Only store new nematodes if we are training
static isTraining = true;
// The maximum number of nematodes to store
static maxNematodes = 2000;
// The priority queue of smart nematodes json objects
static priorityQueue;
/* Load the nematode data from a file and creates the priority queue */
static async Initialize() {
// Create a compare function for the priority queue (Based on age)
var compare = (a, b) => { return a.age - b.age };
// Create the priority queue
NematodeTrainer.priorityQueue = new PriorityQueue(compare, NematodeTrainer.maxNematodes);
// Load all data from the stored smart nematode data
await NematodeTrainer.LoadData();
}
/* Loads the nematode data from a JSON file */
static async LoadData() {
// The name of the file to load
const fileName = "SmartNematodeData.json";
// Load the data using "await" to ensure the data is loaded before continuing
try {
const response = await fetch(fileName);
const nematodes = await response.json();
for (const nematode of nematodes) {
NematodeTrainer.priorityQueue.Add(nematode);
}
} catch (error) {
console.error(error);
}
}
/* Adds a nematode to the priority queue
* This specifically takes a Json object from nematode.toJson()
* nematode: The nematode to add to the queue
*/
static AddNematode(nematode) {
// Add the nematode to the queue
NematodeTrainer.priorityQueue.Add(nematode);
}
/* Downloads the nematode data as a JSON file */
static Download() {
// May be null if UI calls this before the queue is initialized
if (NematodeTrainer.priorityQueue != null)
downloadJSON(NematodeTrainer.priorityQueue.GetAllValues(), "SmartNematodeData.json");
}
/* Returns a random nematode from the queue
* Returns: A random nematode from the queue (As a json object)
*/
static GetRandomNematode() {
return NematodeTrainer.priorityQueue.GetRandomElement();
}
/* Prints the nematode data to the console */
static Print(){
NematodeTrainer.priorityQueue.Print();
}
}