Home > src > simulationLoop.m

simulationLoop

PURPOSE ^

SIMULATIONLOOP

SYNOPSIS ^

function [ALL_Family_plRec, great_data_struct] = simulationLoop( varargin )

DESCRIPTION ^

 SIMULATIONLOOP
 [ALL_Family_plRec, great_data_struct] = SIMULATIONLOOP( VARARGIN )
 
 param: 'start', value - start time - default is 1
 param: 'stop', value - stop time - default is 100
 param: 'diary', filename - use diary with filename 
 param: 'video', filename - make a video with filename
 param: 'sensordata', filename - sensordata filename (if not supplied default value 'sensorData.mat' is used)
 param: 'outputstyle', 'verbose' - display lots of information (typically for debug purposes)
 param: 'graphics', 'on' or 'off' - default is 'on'
 param: 'preemption', 'on' or 'off' - default is 'off'
 return: ALL_Family_plRec - plan distribution data
 return: great_data_struct - all other data

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ALL_Family_plRec, great_data_struct] = simulationLoop( varargin )
0002 % SIMULATIONLOOP
0003 % [ALL_Family_plRec, great_data_struct] = SIMULATIONLOOP( VARARGIN )
0004 %
0005 % param: 'start', value - start time - default is 1
0006 % param: 'stop', value - stop time - default is 100
0007 % param: 'diary', filename - use diary with filename
0008 % param: 'video', filename - make a video with filename
0009 % param: 'sensordata', filename - sensordata filename (if not supplied default value 'sensorData.mat' is used)
0010 % param: 'outputstyle', 'verbose' - display lots of information (typically for debug purposes)
0011 % param: 'graphics', 'on' or 'off' - default is 'on'
0012 % param: 'preemption', 'on' or 'off' - default is 'off'
0013 % return: ALL_Family_plRec - plan distribution data
0014 % return: great_data_struct - all other data
0015 %
0016 
0017 load namesOfNodes;
0018 load roads_struct;
0019 load image_struct;
0020 % Following graph will be implemented
0021 
0022 % changed: 040517 - rj - new calls to allocation_scheme
0023 % changed: 040518 - rj - new return values from allocation_scheme
0024 % changed: 040519 - rj - minor changes to function calls
0025 % changed: 040521 - rj - additional changes
0026 % changed: 040528 - rj - important changes
0027 % changed: 040529 - rj - made simulationLoop a function with arguments
0028 % changed: 040604 - rj - 'brief' 'outputstyle'-mode added
0029 % changed: 040607 - rj - changed check of input arguments to a
0030 %                        switch statement, added preemption input argument
0031 % changed: 041021 - rj - changed name to simulationLoop
0032 % changed: 041209 - rj - minor changes, e.g., added global variable noOfParticles
0033 % changed: 041212 - rs - initializeImageStruct bortaget ersatt av .mat filer
0034 % changed: 041221 - rj - minor change
0035 % changed: 050105 - rj - new call to task management
0036 % changed: 050126 - rj - changed call to updateSituationPicture
0037 % changed: 050202 - rj - added preferenceRelation function pointer
0038 % changed: 050209 - rj - some minor changes
0039 
0040 % constants
0041 preemption_allowed = 1;
0042 preemption_not_allowed = 0;
0043 
0044 % check input arguments
0045 
0046 % start and stop timesteps
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   % for odd arguments
0061   if mod(n,2) == 1
0062     switch varargin{n}
0063       
0064       % start and stop arguments
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       % video argument
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       % diary argument
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       % sensor data file
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       % output style
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       % graphics
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       % preemption
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 % warnings
0151 warning off MATLAB:nonIntegerTruncatedInConversionToChar;
0152 
0153 %clear, clf
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; % initialized in initializeEnemy
0168 
0169 simulationTime = start;
0170 
0171 % interesting variables
0172 great_data_struct = struct('enemy', {}, 'own', {}, 'sensors', {}, 'threat', {}, 'tasks', {}, 'services', {}, 'allocs', {});   
0173 % stores almost all interesting data (it will be a struct array with one element for each timestep)
0174 
0175 % Init begin %
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 % function pointers
0193 task_management_func = @task_management_robust;
0194 
0195 %get_prior_func = @getRandomPrior;
0196 get_prior_func = @getCGPrior;
0197 %get_prior_func = @getMaxEntropyPrior;
0198 
0199 preferenceRelation = @priorityPreferredMu;
0200 %preferenceRelation = @priorityPreferredSigma;
0201 
0202 noOfPart = 15; % samples to draw from particle cloud
0203 % Init end %
0204 
0205 if verbose
0206   task_management_func
0207   get_prior_func
0208   preferenceRelation
0209   noOfPart
0210 end
0211 
0212 %--------------------------------------------------------------------
0213 %                     Loop
0214 %--------------------------------------------------------------------
0215 
0216 for i = start:stop  
0217     simulationTime = i;
0218     if verbose == 1
0219       disp(' ');
0220       %disp(['timestep = ' num2str(i)]);
0221       simulationTime
0222     end
0223 
0224     %% Visualization and timestep
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     % intialize current policy structure
0236     curr_policy_stuct = cell(length(own_force_ids), length(enemy_ids));
0237 
0238     % update actual simulation states of enemies, own forces and sensors
0239     updateWorld; 
0240 
0241     if verbose == 1
0242       % DEBUG BEGIN: show sensors
0243       showSensors( struct_sensors );
0244       % DEBUG END
0245     end
0246 
0247     % updates services and checks for observations
0248     [services, struct_sensors, struct_enemy_comps, allocations, observations] =  service_management( services, struct_sensors, struct_enemy_comps, ...
0249                 allocations, tasks );    
0250 
0251     % update the view of the world
0252     struct_enemy_comps = updateSituationPicture( struct_enemy_comps, ...
0253                          observations, ...
0254                          services, struct_sensors, struct_own_forces );
0255 
0256     if verbose == 1
0257       % DEBUG BEGIN: show enemies
0258       showEnemies( struct_enemy_comps );
0259       % DEBUG END
0260     end
0261 
0262     if verbose == 1
0263       % DEBUG BEGIN: show services
0264       %disp('Services:')
0265       %for s=1:length(services)
0266       %  services(s)
0267       %end
0268       showServices( services );
0269       % DEBUG END
0270     end
0271 
0272     for own_force = 1:length(own_force_ids)
0273         for currEnemy = 1:length(enemy_ids) 
0274             %[struct_currentData] = getCurrentEsimatedData(own_force, currEnemy);
0275             %[vi_pos, pos_new, fi_vec] = getRandomParticleData(own_force, currEnemy);
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     %Save infered results in this simulation step
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       % DEBUG BEGIN: show tasks
0291       %disp('Tasks: ')
0292       %for t=1:length(tasks)
0293       %  tasks(t)
0294       %end
0295       showTasks( tasks );
0296       % DEBUG END
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       % DEBUG BEGIN: show allocations
0307       showAllocs( allocations );
0308       % DEBUG END
0309     end
0310 
0311     % DEBUG BEGIN: show sensors
0312     % DEBUG END
0313 
0314     % store all data
0315 
0316      %struct_sensors
0317      %struct('enemy', struct_enemy_comps, 'own', struct_own_forces, 'sensors', struct_sensors, 'threat', {threat_data}, 'tasks', tasks, 'services', services, 'allocs', allocations)
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     % DEBUG BEGIN
0324 %     disp(' ');
0325 %     disp(['----- Pause after timestep = ' num2str(simulationTime) ' -----']);
0326 %     pause;
0327     % DEBUG END
0328 end
0329 
0330 %% finish up
0331 
0332 if diary_on == 1
0333   diary off
0334 end
0335 
0336 if video_on == 1
0337   mov = close(mov);
0338 end
0339 %% finished finishing up
0340 
0341 
0342

Generated on Wed 16-Mar-2005 09:17:47 by m2html © 2003