-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.m
145 lines (118 loc) · 5.41 KB
/
world.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
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
classdef world < dynamicprops % dynamicprops is subclass of handle
% world object - tracks actual state of simulation
properties (SetObservable = true)
time; % keep track of world time
end
properties
N=5; %size of grid
r; %rate constant for - events per unit time(world wide)
s; %size constant for exponential distribution of sizes
v; %rate constant for RAIN events - events per unit time (world wide)
cloudsize; %average size of rain event
A; %array of values for dirt levels
Moisture; %array of values for moisture level
vacuumArray; %array of object handles
sensor; % data as recorded on sensor
planner; % handle to planning processor
g_handle; %handle to worlddraw graph
expenditure; %cummulative funds expended since last reset
end
methods % methods
function a=world(r,s,v,cloudsize) % constructor (input rate and size constants)
a.time=0;
a.r=r;
a.s=s;
a.v=v;
a.cloudsize=cloudsize;
a.A=zeros(a.N);
a.Moisture=zeros(a.N);
end
function clean(a,x,y) % reset location x,y dirt level to 0
a.A(x,y)=0;
end
function inc(a) % single time step of simulated world
% dustfall procedure -----
t=a.time; % start time
T=t+1; % final time
tau=-log(rand(1))/a.r ; %time until first event
t=t+tau;
while t<T % accumulate dirt until next event falls past final time
dustball=-log(rand(1))*a.s; % dustball size
I=randi(a.N^2); %select site
a.A(I)=a.A(I)+dustball;
tau=-log(rand(1))/a.r ; %time until next event
t=t+tau;
end
% end dustfall
% drying
a.Moisture(a.Moisture>0)=a.Moisture(a.Moisture>0)-1;
% rainfall procedure -----
t=a.time; % start time
tau=-log(rand(1))/a.v ; %time until first event
t=t+tau;
while t<T % accumulate dirt until next event falls past final time
I=randi(a.N^2); %select site
a.Moisture(I)=a.Moisture(I)+ceil(2*rand(1)*a.cloudsize); %uniform 0% to 200% of average
tau=-log(rand(1))/a.v ; %time until next event
t=t+tau;
end
% end rainfall
a.time=T;
end
function draw(world) % produce standard three frame graphic
if isempty(world.g_handle) || ~ishandle(world.g_handle);
subplot(1,3,1)
imagesc(world.A')
vacs=world.vacuumArray;
for i=1:length(vacs);
vacs(i).draw
end
set(gca,'Xtick',1:world.N,'Ytick',1:world.N,'color',[0 0 1]);
gridxy((0:world.N)+.5,(0:world.N)+.5);
caxis([0 max(max(world.A(:)),max(world.sensor.array(:)))]);
colorbar
title(['real t=',num2str(world.time)]);
subplot(1,3,2)
imagesc(world.sensor.array')
vacs=world.vacuumArray;
for i=1:length(vacs);
vacs(i).draw
end
set(gca,'Xtick',1:world.N,'Ytick',1:world.N,'color',[0 0 1]);
gridxy((0:world.N)+.5,(0:world.N)+.5);
caxis([0 max(max(world.A(:)),max(world.sensor.array(:)))]);
colorbar
title('sensor')
subplot(1,3,3)
imagesc(world.planner.worldview')
vacs=world.vacuumArray;
for i=1:length(vacs);
vacs(i).draw
end
set(gca,'Xtick',1:world.N,'Ytick',1:world.N,'color',[0 0 1]);
gridxy((0:world.N)+.5,(0:world.N)+.5);
caxis([0 max(max(world.A(:)),max(world.sensor.array(:)))]);
colorbar
title('planner')
colormap('bone');c=colormap;colormap(flipud(c));
drawnow;
world.g_handle=gcf;
else
for i=1:length(world.vacuumArray);
v=findobj(world.g_handle,'string',num2str(i)); %handle to vacuum text
set(v,'position',[world.vacuumArray(i).xPos world.vacuumArray(i).yPos]);
set(v,'backgroundcolor',[.7 .7 .7]+[.3 -.5 -.5]*(world.vacuumArray(i).status==4));
end
C_des=[0 max(max(world.A(:)),max(world.sensor.array(:)))];
I=findobj(world.g_handle,'type','image'); %Image components
set(I(4),'CData',world.planner.worldview');
set(I(5),'CData',world.sensor.array','AlphaData',(world.sensor.Wet'==0));
set(I(6),'CData',world.A','AlphaData',(world.Moisture'==0));
ax=findobj(world.g_handle,'type','axes');
for i=4:6;caxis(ax(i),C_des);end
title(ax(6),['real t=',num2str(world.time)]);
drawnow;
end
end
end
end