xxxxxxxxxx
124
/*
Here we assume that we have two populations of people going to work. One travels by train and the
other by car. Both populations have a certain baseline wellness, but each population has a specific
time after which point their wellness starts to suffer.
This program graphs the results of the wellness of these two populations for a random set of travel
times.
*/
//++++++++----------++++++++++----------++++++++++----------++++++++++----------++++++++++----------
// We create a list of reported wellbeings. Each element represents the average rank of wellbeing
// that people of a given commute time report (first element is the average of people commuting for
// 0-9 minutes, the second 10-19 etc.)
wellness_list = [];
// To calculate the averages we sum up all the scores from the people in a certain bracket and then
// divide by the number of people in that bracket.
trip_wellness_sum = [];
trip_number_of_people = [];
// Let's simulate pnum private car passengers and tnum transit passengers.
total = 1000;
pnum = total*0.9;
tnum = total - pnum;
noise_level = 12;
function populate() {
// Private car traffic:
median = 0;
std_dev = 23/0.675;
//std_dev_high = 30;
for (i=0; i<pnum; i++) {
trip_length = abs(randomGaussian(median, std_dev));
// Now that we have this passenger's trip length, let's assign him a wellness score.
// If the private car passenger drives for less than 15 minutes, the score doesn't drop but
// any longer reduces the wellness score by one for each ten minutes. We then add perlin
// noise to that wellbeing score to simulate noise in the population.
wellness_baseline = 69;
wellness = wellness_baseline;
if (trip_length > 15) {
wellness = wellness_baseline - (trip_length - 15)/10 + (noise(i)-0.5)*noise_level;
}
// Finally we add the score to the wellness list (the average comes once outside of the loop).
bracket = floor(trip_length/10);
print("wellness " + wellness);
print("trip length: " + trip_length + ", bracket " + bracket);
// If that bracket hasn't been entered yet:
if (!trip_wellness_sum[bracket]) {
trip_wellness_sum[bracket] = 0;
trip_number_of_people[bracket] = 0;
wellness_list[bracket] = 0;
}
trip_wellness_sum[bracket] += wellness;
print("sum " + trip_wellness_sum[bracket]);
trip_number_of_people[bracket] += 1;
}
// Public transit
median = 0;
std_dev = 33/0.675;
for (i=0; i<tnum; i++) {
trip_length = abs(randomGaussian(median, std_dev));
// Now that we have this passenger's trip length, let's assign him a wellness score.
// If the private car passenger drives for less than 15 minutes, the score doesn't drop but
// any longer reduces the wellness score by one for each ten minutes. We then add perlin
// noise to that wellbeing score to simulate noise in the population.
wellness_baseline = 69;
wellness = wellness_baseline;
if (trip_length > 30) {
wellness = wellness_baseline - (trip_length - 30)/10 + (noise(i)-0.5)*noise_level;
}
// Finally we add the score to the wellness list (the average comes once outside of the loop).
bracket = floor(trip_length/10);
print("wellness " + wellness);
print("trip length: " + trip_length + ", bracket " + bracket);
// If that bracket hasn't been entered yet:
if (!trip_wellness_sum[bracket]) {
trip_wellness_sum[bracket] = 0;
trip_number_of_people[bracket] = 0;
wellness_list[bracket] = 0;
}
trip_wellness_sum[bracket] += wellness;
print("sum " + trip_wellness_sum[bracket]);
trip_number_of_people[bracket] += 1;
}
// Now let's calculate the averages:
for (j=0; j<trip_wellness_sum.length; j++) {
wellness_list[j] = trip_wellness_sum[j]/trip_number_of_people[j];
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
populate();
}
function draw() {
/* running = false;
if (!running) {
populate();
running = true;
}*/
noStroke();
bar_width = 40;
bar_separation = 10;
for (j=0; j<wellness_list.length; j++) {
fill(255,34,163);
rect((bar_width+bar_separation)*j, (100-wellness_list[j])*10, bar_width, wellness_list[j]*10 - 400);
fill(0);
text(trip_number_of_people[j], (bar_width+bar_separation)*j + bar_width/2 -10, 590);
text(int(wellness_list[j]*10)/10, (bar_width+bar_separation)*j + bar_width/2 -10, (100 - wellness_list[j])*10);
}
}