Skip to content

Commit 1befd75

Browse files
committed
Added fast average for optimized time savings
1 parent 471d385 commit 1befd75

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

controller.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@ class Controller {
5353
if (this.data.length == 0) {return "No data set";}
5454

5555
//Create initial task
56-
//this.setPivot();
56+
this.initPivots();
5757
this.query(this.data.length, 0, this.data.length - 1);
5858

5959
this.controllerStatus = "WORK";
6060
return true;
6161

6262
}
6363

64-
setPivot() {
64+
initPivots() {
6565

6666
let minValue = this.data[0];
6767
let minIndex = 0;
68+
let maxValue = this.data[0];
69+
let maxIndex = 0;
6870

6971
for (let i = 0; i < this.data.length; i++) {
7072

71-
if (abs(this.data[i] - this.pivAvg) < minValue) {
73+
if (this.data[i] <= minValue) {
7274

73-
minValue = abs(this.data[i] - this.pivAvg);
75+
minValue = this.data[i];
7476
minIndex = i;
7577

7678
}
@@ -79,6 +81,20 @@ class Controller {
7981

8082
this.swap(0, minIndex);
8183

84+
for (let i = 0; i < this.data.length; i++) {
85+
86+
if (this.data[i] >= maxValue) {
87+
88+
maxValue = this.data[i];
89+
maxIndex = i;
90+
91+
}
92+
93+
}
94+
95+
this.swap(this.data.length - 1, maxIndex);
96+
return true;
97+
8298
}
8399

84100
assignQuery() {
@@ -151,7 +167,7 @@ class Controller {
151167

152168
this.data.push(data);
153169

154-
this.pivAvg = Math.round(this.pivAvg * ((this.data.length-1)/this.data.length) + data/this.data.length);
170+
this.pivAvg = (this.pivAvg * ((this.data.length-1)/this.data.length) + data/this.data.length);
155171
return true;
156172

157173
}

interface.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function windowResized() {
99

1010
function mousePressed() {
1111

12-
//noLoop();
13-
//redraw();
12+
noLoop();
13+
redraw();
1414

1515
}
1616

@@ -46,7 +46,7 @@ function setup() {
4646
starts = [];
4747
ends = [];
4848

49-
let n = 500;
49+
let n = 300;
5050

5151
for (let i = 0; i < n; i++) {
5252

@@ -58,8 +58,8 @@ function setup() {
5858

5959
shuffleJRS();
6060

61-
jrs.setZoneLimit(15);
62-
jrs.setMachines(4);
61+
jrs.setZoneLimit(5);
62+
jrs.setMachines(10);
6363
jrs.go();
6464

6565
barWidth = wW/jrs.data.length;

machine.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,45 @@ class Machine {
2525

2626
}
2727

28+
setPivot() {
29+
30+
let avg = (this.controller.data[this.start] + this.controller.data[this.end])/2;
31+
let minDelta = abs(avg - this.controller.data[this.leftPivot]);
32+
let deltaIndex = this.leftPivot;
33+
34+
for (let i = this.leftPivot; i < this.end; i++) {
35+
36+
if (abs(avg - this.controller.data[i]) < minDelta) {
37+
38+
minDelta = abs(avg - this.controller.data[i]);
39+
deltaIndex = i;
40+
41+
}
42+
43+
}
44+
45+
this.controller.swap(this.leftPivot, deltaIndex);
46+
return true;
47+
48+
}
49+
2850
sendWork(queueTask) {
2951

3052
this.start = queueTask[1];
3153
this.end = queueTask[2];
3254

33-
this.leftPivot = this.start;
34-
this.rightPivot = this.end;
55+
this.leftPivot = this.start + 1;
56+
this.rightPivot = this.end - 1;
3557

36-
this.bestBubbleValue = this.controller.data[this.end];
37-
this.bestBubbleIndex = this.end;
58+
this.bestBubbleValue = this.controller.data[this.rightPivot];
59+
this.bestBubbleIndex = this.rightPivot;
3860

39-
if(this.end - this.start > this.controller.zoneLimitSize) {this.machineStatus = "WORK(JRS)";}
61+
if(this.end - this.start > this.controller.zoneLimitSize) {
62+
63+
this.setPivot();
64+
this.machineStatus = "WORK(JRS)";
65+
66+
}
4067
else {this.machineStatus = "WORK(BBL)";}
4168

4269
return true;
@@ -53,8 +80,8 @@ class Machine {
5380
if (this.leftPivot == this.rightPivot) {
5481

5582
//Create two queries and go DONE
56-
if(this.leftPivot - this.start > 1) {this.controller.query(this.leftPivot - this.start, this.start, this.leftPivot);}
57-
if(this.end - this.leftPivot + 1 > 1) {this.controller.query(this.end - this.leftPivot + 1, this.leftPivot +1, this.end);}
83+
if(this.leftPivot - this.start > 3) {this.controller.query(this.leftPivot - this.start, this.start, this.leftPivot);}
84+
if(this.end - this.leftPivot + 1 > 3) {this.controller.query(this.end - this.leftPivot, this.leftPivot, this.end);}
5885

5986
this.machineStatus = "DONE";
6087
return true;
@@ -69,8 +96,16 @@ class Machine {
6996

7097
if(this.machineStatus == "WORK(BBL)") {
7198

72-
//Walk downwards and record lowest value
99+
//Stop when done
100+
if (this.leftPivot >= this.end - 1) {
101+
102+
this.machineStatus = "DONE";
103+
return true;
104+
105+
}
106+
73107
this.rightPivot--;
108+
//record lowest value
74109
if (this.controller.data[this.rightPivot] < this.bestBubbleValue) {
75110

76111
this.bestBubbleValue = this.controller.data[this.rightPivot];
@@ -79,21 +114,13 @@ class Machine {
79114
}
80115

81116
//Swap when walker reaches the leftPivot
82-
if(this.rightPivot == this.leftPivot) {
117+
if(this.rightPivot <= this.leftPivot) {
83118

84-
this.controller.swap(this.rightPivot, this.bestBubbleIndex);
85-
this.bestBubbleValue = this.controller.data[this.end];
86-
this.bestBubbleIndex = this.end;
119+
this.controller.swap(this.leftPivot, this.bestBubbleIndex);
120+
this.bestBubbleValue = this.controller.data[this.end - 1];
121+
this.bestBubbleIndex = this.end - 1;
87122
this.leftPivot++;
88-
this.rightPivot = this.end;
89-
return true;
90-
91-
}
92-
93-
//Stop when done
94-
if (this.leftPivot == this.end) {
95-
96-
this.machineStatus = "DONE";
123+
this.rightPivot = this.end - 1;
97124
return true;
98125

99126
}

0 commit comments

Comments
 (0)