-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
378 lines (282 loc) · 12.4 KB
/
index.js
File metadata and controls
378 lines (282 loc) · 12.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* global require, _ */
require.config({
paths: {
'underscore': 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min'
}
});
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
};
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
require(['underscore'], function (_) {
/* Spring Retreat 2015 Data */
var p, people;
people = [];
//p = new Person(); p.id = 492; p.wantsToDrive = 5; p.seats = 4; p.sex = 0; p.origin = 1; people.push(p);
//p = new Person(); p.id = 1; p.wantsToDrive = 5; p.seats = 2; p.sex = 0; p.origin = 12; people.push(p);
p = new Person(); p.id = 268; p.wantsToDrive = 5; p.seats = 5; p.sex = 0; p.origin = 4; people.push(p);
p = new Person(); p.id = 73; p.wantsToDrive = 5; p.seats = 4; p.sex = 0; p.origin = 5; people.push(p);
p = new Person(); p.id = 51; p.wantsToDrive = 5; p.seats = 4; p.sex = 0; p.origin = 5; people.push(p);
p = new Person(); p.id = 213; p.wantsToDrive = 5; p.seats = 4; p.sex = 0; p.origin = 5; people.push(p);
p = new Person(); p.id = 229; p.wantsToDrive = 5; p.seats = 7; p.sex = 0; p.origin = 4; people.push(p);
p = new Person(); p.id = 45; p.wantsToDrive = 3; p.seats = 5; p.sex = 0; p.origin = 1; people.push(p);
p = new Person(); p.id = 170; p.wantsToDrive = 3; p.seats = 5; p.sex = 0; p.origin = 1; people.push(p);
p = new Person(); p.id = 144; p.wantsToDrive = 3; p.seats = 5; p.sex = 1; p.origin = 4; people.push(p);
p = new Person(); p.id = 94; p.wantsToDrive = 1; p.seats = 5; p.sex = 0; p.origin = 5; people.push(p);
//p = new Person(); p.id = 90; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 5; people.push(p);
p = new Person(); p.id = 87; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 57; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 4; people.push(p);
p = new Person(); p.id = 270; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 1; people.push(p);
p = new Person(); p.id = 710; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 1; people.push(p);
//p = new Person(); p.id = 1067; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 1; people.push(p);
//p = new Person(); p.id = 92; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 4; people.push(p);
p = new Person(); p.id = 692; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 604; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 263; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 1; people.push(p);
p = new Person(); p.id = 589; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 637; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
//p = new Person(); p.id = 673; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 4; people.push(p);
p = new Person(); p.id = 112; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 5; people.push(p);
p = new Person(); p.id = 22; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 4; people.push(p);
p = new Person(); p.id = 7; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 4; people.push(p);
p = new Person(); p.id = 600; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 1066; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 1; people.push(p);
p = new Person(); p.id = 98; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 4; people.push(p);
p = new Person(); p.id = 476; p.wantsToDrive = 0; p.seats = 0; p.sex = 1; p.origin = 4; people.push(p);
p = new Person(); p.id = 227; p.wantsToDrive = 0; p.seats = 0; p.sex = 0; p.origin = 1; people.push(p);
/* COST EVALUATIONS */
var evaluations = [
// Base. Currently, primarily number of different origins.
function (car, passengers) {
var origins = [];
passengers.forEach(function (passenger) {
if (!origins.contains(passenger.origin)) {
origins.push(passenger.origin);
}
});
return origins.length + 2;
},
// Over seatbelt limit.
function (car, passengers) {
if (passengers.length > car.seats) {
return 10000000000000000;
}
return 1;
},
// Car Fullness. Ideally 80%.
function (car, passengers) {
return Math.cos((passengers.length/car.seats * Math.PI+0.8)+1)*0.2+1;
},
// Car Gender Composition. Single-gender cars are ideal, but a perfectly even split is the next-best.
function (car, passengers) {
var numerator = 0,
denominator = 0;
passengers.forEach(function (person) {
numerator += person.sex;
denominator += 1;
});
var sexRatio = numerator/denominator;
return 1 + 0.2 * (Math.sin(sexRatio * 2.8) + Math.abs(Math.sin(sexRatio * 2 * Math.PI)));
},
// Desire to Drive. This is important.
function (car, passengers) {
return 2.25 - (0.25 * car.driver.wantsToDrive); // more efficient form of (5 - x) * 0.25 + 1
}
];
/**
* The evalCost function. Evaluates the cost of a car, with a separate given passenger list. Iterates through each
* of the 'evaluations', and multiplies those results together.
*
* @param car
* @param passengers
* @returns {number}
*/
function evalCost(car, passengers) {
var cost = 1;
evaluations.forEach(function(test) {
cost *= test(car, passengers);
});
return cost;
}
/**
* The evalTotalCost function returns the total cost for all car assignments.
*
*/
function evalTotalCost() {
var cost = 0;
cars.forEach(function (car) {
cost += evalCost(car, car.passengers);
});
return cost;
}
/**
* The remainingSeats function returns the total number of seats unclaimed in all cars.
*
*/
function remainingSeats() {
var seats = 0;
cars.forEach(function (car) {
seats += car.emptySeats;
});
return seats;
}
/**
* The totalSeats function returns the total number of seats in all cars.
*
*/
function totalSeats() {
var seats = 0;
cars.forEach(function (car) {
seats += car.seats;
});
return seats;
}
/**
* The Person class. Creates a Person object. Initial generation inserts random data.
*
* @class Person
* @constructor
*/
function Person() {
this.id = people.length;
this.wantsToDrive = _.random(0, 1) * _.random(0, 5); // 0 = have no car and can't drive; 1 = has car, doesn't wanna drive; 5 = has car, really wants to drive
this.seats = this.wantsToDrive < 0 ? 0 : _.random(2, 8);
this.sex = _.random(0, 1); // Single-Sex cars are preferable; there tends to be less drama that way (at least from my perspective as a male)
this.origin = -1; //KURTZ TESTING _.random(1, locations.length - 1); // initial location, references "locations" var
this.inCar = -1; // default of -1, meaning unassigned.
}
var cars = [];
/**
* The Car class. Creates a car object, which has passengers.
*
* @class Car
* @param {Person} driver the driver of the car. Number of seats available is taken from this person's information.
* @constructor
*/
function Car(driver) {
this.id = cars.length;
this.passengers = [];
this.seats = driver.seats;
this.origin = driver.origin;
this.travelLegs = [];
this.addPassenger(driver);
}
Car.prototype.getDriver = function () {
return this.passengers[0];
};
Car.prototype.__defineGetter__('driver', Car.prototype.getDriver);
Car.prototype.getCost = function () {
return evalCost(this, this.passengers);
};
Car.prototype.__defineGetter__('cost', Car.prototype.getCost);
Car.prototype.getEmptySeats = function () {
return this.seats - this.passengers.length;
};
Car.prototype.__defineGetter__('emptySeats', Car.prototype.getEmptySeats);
/**
* The addPassenger method adds a passenger to a given car.
*
* @method addPassenger
* @param {Person} passenger The passenger being added to
*/
Car.prototype.addPassenger = function (passenger) {
if (passenger.inCar !== -1) {
passenger.inCar.passengers.remove(passenger);
}
passenger.inCar = this;
this.passengers.push(passenger);
if (passenger.origin !== this.origin) {
if (!this.travelLegs.contains(passenger.origin)) {
this.travelLegs.push(passenger.origin); // KURTZ change travel legs to have intelligent ordering.
}
}
};
/* THE ACTUAL PROCEDURE: */
///* Initializes the test people */
//var people = [];
//for (var i = 0; i < numPeople; i++) {
// people.push(new Person());
//}
/* 1. Put all potential drivers in their cars. */
var canDrive = "has car";
var cantDrive = "no car";
people = _.groupBy(people, function (person) {
return (person.wantsToDrive > 0) ? canDrive : cantDrive
}); // separates those who could drive from those who can't.
people[canDrive].forEach(function (driver) {
cars.push(new Car(driver))
});
/* 2. For each non-driver, assign to best car */
people[cantDrive].forEach(function (passenger) { // KURTZ make into a "assign to some car" function
cars = _.sortBy(cars, function(car) { // sort to get least expensive. // KURTZ do something more efficient
var passengers = [passenger].concat(car.passengers);
return evalCost(car, passengers);
});
cars[0].addPassenger(passenger);
});
people = _.union(people[cantDrive], people[canDrive]);
/* 3. The Most Expensive cars are eliminated */
var removedCar;
while (totalSeats()*.80 > people.length) { // KURTZ develop the exit condition
cars = _.sortBy(cars, function (car) { // sort to get most expensive. // KURTZ do something more efficient
return car.cost;
});
removedCar = cars.pop();
while (removedCar.passengers.length > 0) {
var passenger = removedCar.passengers.pop();
cars = _.sortBy(cars, function (car) { // sort to get least expensive. // KURTZ do something more efficient
var passengers = [passenger].concat(car.passengers);
return evalCost(car, passengers);
});
cars[0].addPassenger(passenger);
}
}
/* preparing the output... */
cars = _.sortBy(cars, function (car) {
return evalCost(car, car.passengers)
});
window.console.log("people", people);
window.console.log("cars", cars);
window.console.info("Total Cost: ", evalTotalCost().toString());
window.console.info("Empty Seats: ", remainingSeats().toString());
window.console.info("Total Seats: ", totalSeats().toString());
people = _.sortBy(people, function (person) {
return person.id;
});
var div = document.createElement('div');
div.innerHTML = "id";
div.innerHTML += ",";
div.innerHTML += "car";
div.innerHTML += ",";
div.innerHTML += "peopleInCar";
div.innerHTML += ",";
div.innerHTML += "personIsDriver";
document.body.appendChild(div);
people.forEach(function(passenger) {
div = document.createElement('div');
div.id = passenger.id;
div.innerHTML = passenger.id;
div.innerHTML += ",";
div.innerHTML += passenger.inCar.id.toString();
div.innerHTML += ",";
div.innerHTML += passenger.inCar.passengers.length.toString();
div.innerHTML += ",";
div.innerHTML += (passenger.inCar.passengers[0].id === passenger.id).toString().toUpperCase();
document.body.appendChild(div);
});
});