-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
200 lines (123 loc) · 3.94 KB
/
Copy pathcontroller.js
File metadata and controls
200 lines (123 loc) · 3.94 KB
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
class Controller {
constructor() {
this.machines = [];
this.busyMachines = [];
this.freeMachines = [];
this.data = [];
this.controllerStatus = "OFF";
this.pivAvg = 0;
this.queryQueue = []; //A queue entry defines a zone as:[length, starting index, end index, approximate average]
this.zoneLimitSize = 10;
/*The controller can be in states:
DONE: Sorting done
WORK: Waiting for step
OFF: Waiting for all parameters and machines to be set
*/
}
setZoneLimit(n) {
this.zoneLimitSize = n;
return true;
}
setMachines(n) {
if (this.controllerStatus == "WORK") {return "Controller is not OFF";}
this.machines = [];
this.freeMachines = [];
for (let i = 0; i < n; i++) {
this.machines[i] = new Machine(this);
this.freeMachines[i] = this.machines[i];
}
this.busyMachines = [];
this.controllerStatus = "OFF";
return true;
}
go() {
if (this.busyMachines.length + this.freeMachines.length != this.machines.length) {return "Machinecount, Busy and Free machines mismatch";}
if (this.machines.length < 1) {return "MachineCount is less that 1";}
if (this.data.length == 0) {return "No data set";}
//Create initial task
this.initPivots();
this.query(this.data.length, 0, this.data.length - 1);
this.controllerStatus = "WORK";
return true;
}
initPivots() {
let minValue = this.data[0];
let minIndex = 0;
let maxValue = this.data[0];
let maxIndex = 0;
for (let i = 0; i < this.data.length; i++) {
if (this.data[i] <= minValue) {
minValue = this.data[i];
minIndex = i;
}
}
this.swap(0, minIndex);
for (let i = 0; i < this.data.length; i++) {
if (this.data[i] >= maxValue) {
maxValue = this.data[i];
maxIndex = i;
}
}
this.swap(this.data.length - 1, maxIndex);
return true;
}
assignQuery() {
//Check for queue entries and assign free machines in order of preference
//Greater zones first
while ((this.queryQueue.length > 0) && (this.freeMachines.length > 0)) {
let maxZone = 0;
let maxZoneIndex = 0;
for (let i = 0; i < this.queryQueue.length; i++) {
if (this.queryQueue[i][0] > maxZone) {
maxZone = this.queryQueue[i][0];
maxZoneIndex = i;
}
}
this.freeMachines[0].sendWork(this.queryQueue[maxZoneIndex]);
this.queryQueue.splice(maxZoneIndex, 1);
//Add the machine to busyMachines
this.busyMachines.push(this.freeMachines[0]);
//Remove the machine from freeMachines
this.freeMachines.splice(0, 1);
}
return true;
}
//Move machines that finished its jobs to freeMachines
freeTheMachines() {
for(let i = this.busyMachines.length - 1; i >= 0; i--) {
if (this.busyMachines[i].machineStatus == "DONE") {
this.busyMachines[i].clear();
this.freeMachines.push(this.busyMachines[i]);
this.busyMachines.splice(i, 1);
}
}
}
//Function to be called only from machines or from shuffle
swap(a, b) {
let temp = this.data[a];
this.data[a] = this.data[b];
this.data[b] = temp;
return true;
}
addData(data) {
if (typeof data != "number") {return "data is not a Number";}
if (this.controllerStatus != "OFF") {return "Controller is not OFF";}
this.data.push(data);
this.pivAvg = (this.pivAvg * ((this.data.length-1)/this.data.length) + data/this.data.length);
return true;
}
step() {
if (this.controllerStatus == "OFF") {return "Controller is not set";}
if (this.controllerStatus == "DONE") {return "Controller is DONE";}
if(this.queryQueue.length == 0 && this.busyMachines.length == 0) {this.controllerStatus = "DONE"; return "Controller is DONE";}
this.freeTheMachines();
this.assignQuery();
for (let i = 0; i < this.busyMachines.length; i++) {
this.busyMachines[i].step();
}
return true;
}
query(n, start, end) {
this.queryQueue.push([n, start, end]);
}
}