0001 function [ALL_Family_plRec, great_data_struct] = simulationLoop( varargin )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 load namesOfNodes;
0018 load roads_struct;
0019 load image_struct;
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 preemption_allowed = 1;
0042 preemption_not_allowed = 0;
0043
0044
0045
0046
0047 start = 1;
0048 stop = 100;
0049
0050 diary_on = 0;
0051 video_on = 0;
0052 alt_sensordata = 0;
0053 global verbose;
0054 verbose = 1;
0055 graphics_on = 1;
0056 preemption = preemption_not_allowed;
0057
0058 for n=1:nargin
0059
0060
0061 if mod(n,2) == 1
0062 switch varargin{n}
0063
0064
0065 case 'start'
0066 if (nargin > n) & (isa(varargin{n+1}, 'numeric') == 1)
0067 start = varargin{n+1}
0068 else
0069 error('Please supply a start time!');
0070 end
0071
0072 case 'stop'
0073 if (nargin > n) & (isa(varargin{n+1}, 'numeric') == 1)
0074 stop = varargin{n+1}
0075 else
0076 error('Please supply a stop time!');
0077 end
0078
0079
0080 case 'video'
0081 video_on = 1;
0082 if (nargin > n) & (isa(varargin{n+1}, 'char') == 1)
0083 video_filename = varargin{n+1}
0084 mov = avifile( video_filename, 'quality', 100, 'fps', 2);
0085 else
0086 error('Please supply a video filename');
0087 end
0088
0089
0090 case 'diary'
0091 diary_on = 1;
0092 if (nargin > n) & (isa(varargin{n+1}, 'char') == 1)
0093 diary_filename = varargin{n+1}
0094 diary( diary_filename );
0095 diary on;
0096 else
0097 error('Please supply a diary filename');
0098 end
0099
0100
0101 case 'sensordata'
0102 alt_sensordata = 1;
0103 if (nargin > n) && (isa(varargin{n+1}, 'char') == 1)
0104 sensor_data_filename = varargin{n+1}
0105 else
0106 error('Please supply a sensor datafile filename');
0107 end
0108 if (exist(sensor_data_filename) ~= 2)
0109 error(['Data file suggested does not exist: ' sensor_data_filename]);
0110 end
0111
0112
0113 case 'outputstyle'
0114 if (isa(varargin{n+1}, 'char') == 1)
0115 if strcmp(varargin{n+1},'verbose')
0116 verbose = 1
0117 elseif strcmp(varargin{n+1},'brief')
0118 verbose = 0
0119 end
0120 end
0121
0122
0123 case 'graphics'
0124 if (isa(varargin{n+1}, 'char') == 1)
0125 if strcmp(varargin{n+1},'on')
0126 graphics_on = 1
0127 elseif (strcmp(varargin{n+1}, 'off') == 1)
0128 graphics_on = 0
0129 end
0130 end
0131
0132
0133 case 'preemption'
0134 if (isa(varargin{n+1}, 'char') == 1)
0135 if strcmp(varargin{n+1},'on')
0136 preemption = preemption_allowed
0137 elseif (strcmp(varargin{n+1}, 'off') == 1)
0138 preemption = preemption_not_allowed
0139 end
0140 end
0141
0142 otherwise
0143 error(['Unkonwn input flag: ' varargin{n}])
0144 end
0145 end
0146 end
0147
0148
0149
0150
0151 warning off MATLAB:nonIntegerTruncatedInConversionToChar;
0152
0153
0154 global image_struct;
0155 global roads_struct;
0156 global simulationTime;
0157 global struct_own_forces;
0158 global struct_sensors;
0159 global struct_enemy_comps;
0160 global noOfOwn_forces;
0161 global noOfEnemyCompanies;
0162 global noOfSensors;
0163 global south_enemy;
0164 global north_enemy1;
0165 global north_enemy2;
0166
0167 global noOfParticles;
0168
0169 simulationTime = start;
0170
0171
0172 great_data_struct = struct('enemy', {}, 'own', {}, 'sensors', {}, 'threat', {}, 'tasks', {}, 'services', {}, 'allocs', {});
0173
0174
0175
0176 load enemy_positions;
0177 [struct_own_forces,noOfOwn_forces] = initializeOwn_forces;
0178 [struct_enemy_comps, noOfEnemyCompanies] = initializeEnemy;
0179 [tasks] = initializeTasks;
0180 [allocations] = initializeAllocations;
0181 if alt_sensordata == 0
0182 [struct_sensors, noOfSensors] = initializeSensors;
0183 else
0184 [struct_sensors, noOfSensors] = initializeSensors(sensor_data_filename);
0185 end
0186 [services] = initializeServices( struct_sensors );
0187 enemy_ids = [1 2 3];
0188 own_force_ids = [1 2];
0189 [DBN_Mx, soft_ev_Mx_robust, oldData_Mx_robust] = initDBNMx(own_force_ids, ...
0190 enemy_ids);
0191
0192
0193 task_management_func = @task_management_robust;
0194
0195
0196 get_prior_func = @getCGPrior;
0197
0198
0199 preferenceRelation = @priorityPreferredMu;
0200
0201
0202 noOfPart = 15;
0203
0204
0205 if verbose
0206 task_management_func
0207 get_prior_func
0208 preferenceRelation
0209 noOfPart
0210 end
0211
0212
0213
0214
0215
0216 for i = start:stop
0217 simulationTime = i;
0218 if verbose == 1
0219 disp(' ');
0220
0221 simulationTime
0222 end
0223
0224
0225 if graphics_on == 1
0226 visualize_current_timestep;
0227 end
0228 if video_on == 1
0229 frame = getframe(gcf);
0230 mov = addframe(mov, frame);
0231 end
0232
0233
0234
0235
0236 curr_policy_stuct = cell(length(own_force_ids), length(enemy_ids));
0237
0238
0239 updateWorld;
0240
0241 if verbose == 1
0242
0243 showSensors( struct_sensors );
0244
0245 end
0246
0247
0248 [services, struct_sensors, struct_enemy_comps, allocations, observations] = service_management( services, struct_sensors, struct_enemy_comps, ...
0249 allocations, tasks );
0250
0251
0252 struct_enemy_comps = updateSituationPicture( struct_enemy_comps, ...
0253 observations, ...
0254 services, struct_sensors, struct_own_forces );
0255
0256 if verbose == 1
0257
0258 showEnemies( struct_enemy_comps );
0259
0260 end
0261
0262 if verbose == 1
0263
0264
0265
0266
0267
0268 showServices( services );
0269
0270 end
0271
0272 for own_force = 1:length(own_force_ids)
0273 for currEnemy = 1:length(enemy_ids)
0274
0275
0276 [Family_plRec, Cmp_tti, soft_ev_elem_robust, oldData_elem_robust] = robust_getPolicy(DBN_Mx{own_force, currEnemy}, soft_ev_Mx_robust{own_force, currEnemy}, oldData_Mx_robust{own_force, currEnemy}, noOfPart, own_force, currEnemy, get_prior_func);
0277 curr_Family_plRec{own_force, currEnemy} = Family_plRec;
0278 particles_tti{own_force, currEnemy} = Cmp_tti;
0279 soft_ev_Mx_robust{own_force, currEnemy} = soft_ev_elem_robust;
0280 oldData_Mx_robust{own_force, currEnemy} = oldData_elem_robust;
0281 end;
0282 end;
0283
0284 ALL_Family_plRec{simulationTime} = curr_Family_plRec;
0285 [tasks, threat_data] = feval( task_management_func, tasks, ...
0286 struct_enemy_comps, struct_own_forces, ...
0287 curr_Family_plRec, particles_tti, preferenceRelation );
0288
0289 if verbose == 1
0290
0291
0292
0293
0294
0295 showTasks( tasks );
0296
0297 end
0298
0299 services = update_priority_levels( tasks, services, allocations );
0300
0301 [allocations, services, struct_sensors] = allocation_scheme( allocations, tasks, services, struct_enemy_comps, struct_sensors, preemption, preferenceRelation );
0302
0303 [allocations, services, struct_sensors] = resource_deployment( allocations, tasks, services, struct_enemy_comps, struct_sensors );
0304
0305 if verbose == 1
0306
0307 showAllocs( allocations );
0308
0309 end
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 great_data_struct(simulationTime-start+1) = struct('enemy', struct_enemy_comps, 'own', struct_own_forces, 'sensors', struct_sensors, 'threat', {threat_data}, 'tasks', tasks, 'services', services, 'allocs', allocations);
0320
0321
0322
0323
0324
0325
0326
0327
0328 end
0329
0330
0331
0332 if diary_on == 1
0333 diary off
0334 end
0335
0336 if video_on == 1
0337 mov = close(mov);
0338 end
0339
0340
0341
0342