-
Notifications
You must be signed in to change notification settings - Fork 1
/
biasedwalker_mod.m
107 lines (97 loc) · 3.88 KB
/
biasedwalker_mod.m
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
function [totalcrosstime, dif, wlist, llist]=biasedwalker_mod(in_bias, num_widths, num_lengths, wt, lt, light_time)
% calculates total time waiting at crosslights wit bias when
% you have more widths than lengths or vise versa. For example
% if you have a bias of 1 and more lengths than widths left to
% travel, you would wait up to 1 second to cross horizontally
% versus vertically
%
% PARAMETERS:
% in_bias = bias for willingness to wait at light
% num_widths = number of width segments in the grid
% num_lengths = number of length segments in the grid
% wt = time to walk a width
% lt = time to walk a length
% light_time = maximum length of a light
wlist = [];
llist = [];
total = num_widths + num_lengths;
% set the dimensions of the grid
widths = num_widths;
lengths = num_lengths;
% set the time needed to cross a lenght or a width
widthtime = wt;
lengthtime = lt;
totalwidthtime = widths * widthtime;
totallengthtime = lengths * lengthtime;
% set the bias (seconds a person would be willing to wait at a light)
bias = in_bias;
% maximum time (seconds) one could possibly wait a light
maxwait = light_time;
% set the time counter to 0
totalcrosstime = 0;
while (lengths > 0) || (widths > 0) %while there are still block lengths and widths to travel
llist(total-lengths-widths+1) = lengths;
wlist(total-lengths-widths+1) = widths;
% ONLY WIDTHS REMAIN
if (lengths == 0)
% red light wait
if rand(1) < .5
totalcrosstime = totalcrosstime + rand(1)*maxwait;
end
widths = widths - 1;
totalcrosstime = totalcrosstime + widthtime;
% ONLY LENGTHS REMAIN
elseif (widths == 0)
% red light wait
if rand(1) < .5
totalcrosstime = totalcrosstime + rand(1)*maxwait;
end
lengths = lengths - 1;
totalcrosstime = totalcrosstime + lengthtime;
% MORE LENGTHS THAN WIDTHS, prefer length crossing
elseif lengths > widths
randwait = rand(1);
% green light to cross for a length
if randwait < .5
lengths = lengths - 1;
totalcrosstime = totalcrosstime + lengthtime;
% small wait for green light for a length
elseif randwait < (.5 + bias/(2*maxwait))
lengths = lengths - 1;
totalcrosstime = totalcrosstime + rand(1)*bias;
totalcrosstime = totalcrosstime + lengthtime;
else
widths = widths - 1;
totalcrosstime = totalcrosstime + widthtime;
end
% MORE WIDTHS THAN LENGTHS, width crossing preferred
elseif lengths < widths
randwait = rand(1);
% chance of green light to cross for a width
if randwait <.5
widths = widths - 1;
totalcrosstime = totalcrosstime + widthtime;
% small wait for green light for a width
elseif randwait < (.5 + bias/(2*maxwait))
widths = widths - 1;
totalcrosstime = totalcrosstime + rand(1)*bias;
totalcrosstime = totalcrosstime + widthtime;
else
lengths = lengths - 1;
totalcrosstime = totalcrosstime + lengthtime;
end
% SAME WIDTHS AS LENGTHS
else
if rand(1) < .5
widths = widths - 1;
totalcrosstime = totalcrosstime + widthtime;
else
lengths = lengths - 1;
totalcrosstime = totalcrosstime + lengthtime;
end
end
end
% final variable with total time spent traversing
%totaltime = totalcrosstime + totallengthtime + totalwidthtime;
dif = totalcrosstime - totallengthtime - totalwidthtime;
end