我正在用matlab做一个简单的仿真。
我可以将机器人分为三个阶段。它们分别是Search
,Assess
或Recruit
。如果机器人处于上述任何阶段,则需要执行某些操作。如果机器人完成了Search
,则应该转到Assess
。如果完成了Assess
,则应该转到Recruit
。当机器人到达阶段Recruit
时,机器人的初始数量应减少1。我正在保存current_time
和从number_of_robots_in_nest_initial
开始的数量减少。我发现从while循环中断并保存current_time
和number_of_robots_in_nest_initial
的问题。解决该问题的任何帮助将不胜感激。
for_distribution = @(mean_time) log(1/(1-rand))/mean_time;
number_of_robots_in_nest_initial = 20
current_time = 0;
counter = 1;
state ='search';
mean_search_duration = 1998.849773195878;
mean_assessment_duration = [93.63,99.050];
mean_recruitment_duration = 136.756;
mean_threshold_time_to_pick_nest = [1309.01,1905.4];
while true
switch state
case 'search'
t = for_distribution(mean_search_duration);
current_time(counter+1) = current_time(counter) + t; %#ok<SAGROW>
state = 'assessing';
case 'assessing'
t = for_distribution(mean_assessment_duration(1));
current_time(counter+1) = current_time(counter) + t; %#ok<SAGROW>
if current_time > mean_time_to_pick_nest(1)
state = 'recruiting';
else
state = 'search';
end
case 'recruiting'
t = for_distribution(mean_recruitment_duration);
current_time(counter+1) = current_time(counter) + t; %#ok<SAGROW>
number_of_robots_in_nest_initial(current_time(counter+1)) = number_of_robots_in_nest_initial(current_time(counter)) - 1;
if number_of_robots_in_nest_initial(current_time) == 0
break
end
end
counter = counter+1;
end