summaryrefslogtreecommitdiff
path: root/SA_POP/experiments/EU_Performance/Exp_Core/Exp_EU_SchedStrategy.cpp
blob: ed166b953149ae9d63d03ae1112077a29cc422fc (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// -*- C++ -*-
// $Id$

//=============================================================================
/**
 * @file  Exp_EU_SchedStrategy.cpp
 *
 * This file contains the Exp_EU_SchedStrategy concrete class implementation, which
 * implements a SchedStrategy for "roadblock" scheduling that only updates the
 * precedence graph without any other scheduling and which can randomly choose a
 * "roadblock" of a set of tasks in the working plan.  It considers any working
 * plan with the "roadblock" set of tasks to be unschedulable and any other
 * working plan to be schedulable.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#include "SA_POP_Types.h"

//#include "SA_SchedStrategy.h"
#include "Exp_EU_SchedStrategy.h"

#include "SA_WorkingPlan.h"
#include "Planner.h"
#include <list>
#include <set>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace SA_POP;

// Constructor.
Exp_EU_SchedStrategy::Exp_EU_SchedStrategy (SA_POP::Planner *planner)
: SA_SchedStrategy (planner)
{
  // Nothing to do.
};

// Destructor.
Exp_EU_SchedStrategy::~Exp_EU_SchedStrategy (void)
{
  // Nothing to do.
};


// Recursively satisfy all scheduling constraints (and continue
// satisfaction of open conditions by recursive call back to planning).
bool Exp_EU_SchedStrategy::satisfy_sched (TaskInstID task_inst)
{



  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  //return this->planner_->recurse_plan ();
  // Adjust schedule.
	double THRESHOLD_CRIT = 0.8;
	// Get the current command id to backtrack to.
	CommandID cur_cmd_id = this->planner_->cur_command_id();
	this->cur_seq_num_=1;

  SA_POP_DEBUG (SA_POP_DEBUG_MINIMAL, "NO SCHEDULING with ROADBLOCK SCHEDULER");
  // Don't do any propagation for roadblock scheduler.
/*
  // Do the energy propogation for this task instance
	// This function automatically does this for the task instances before and after it.
  if(!this->energy_prop(task_inst))
  {


	  this->planner_->undo_through(cur_cmd_id);
	  return false;
  }

  // Do the balance propogation related to time windows for this task instance 
  // and those unranked with respect to it
  if(!this->time_balance_prop(task_inst))
  {
	  this->planner_->undo_through(cur_cmd_id);
	  return false;
  }
	const TaskInstSet *unranked = this->planner_->get_prec_insts(task_inst,UNRANKED);
	for(TaskInstSet::const_iterator iter = unranked->begin();iter!=unranked->end();iter++)
	{
		if(!this->time_balance_prop(*iter))
		{
			this->planner_->undo_through(cur_cmd_id);
			return false;
		}
	}
  // Do the balance propogation related to precedence graph links for this task instance 
  // and those unranked with respect to it
 // if(!this->prec_balance_prop(task_inst))
 // {
	//  this->planner_->undo_through(cur_cmd_id);
	//  return false;
 // }
 // unranked = this->planner_->get_prec_insts(task_inst,UNRANKED);
	//for(TaskInstSet::const_iterator iter = unranked->begin();iter!=unranked->end();iter++)
	//{
	//	if(!this->prec_balance_prop(*iter))
	//	{
	//		this->planner_->undo_through(cur_cmd_id);
	//		return false;
	//	}
	//}

	// Recalculate all the levels
	TaskInstSet all = this->planner_->get_all_insts();
	for(TaskInstSet::iterator iter=all.begin();iter!=all.end();iter++)
		this->calculate_levels(*iter);

	// start the search
  if(!search(THRESHOLD_CRIT))
  {
	  this->planner_->undo_through(cur_cmd_id);
	  return false;
  }
*/
  

  if(!this->planner_->recurse_plan ()){
	  this->planner_->undo_through(cur_cmd_id);
	  return false;
  }else{
	  return true;
  }
};

// Satisfy fully instantiated plan.
bool Exp_EU_SchedStrategy::satisfy_full_sched ()
{
  // Just use normal satisfy schedule for "roadblock" scheduler.
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  return true;
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP

  // Don't do normal scheduling for "roadblock" scheduler.
/*

  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  //return true;
  // Adjust schedule.
	CommandID cur_cmd_id = this->planner_->cur_command_id();
	this->cur_seq_num_=1;
  if(!search(0))
  {
	  this->planner_->undo_through(cur_cmd_id);
	  return false;
  }
	
  SA_WorkingPlan* working_plan_tmp = (SA_WorkingPlan*)this->planner_->get_working_plan();

 // working_plan_tmp->get_precedence_graph()
  PrecedenceSet befores = (working_plan_tmp->get_precedence_graph().find(BEFORE)->second);
  PrecedenceSet afters = (working_plan_tmp->get_precedence_graph().find(AFTER)->second);
  PrecedenceSet simuls = (working_plan_tmp->get_precedence_graph().find(SIMUL)->second);
  PrecedenceSet unrankeds = (working_plan_tmp->get_precedence_graph().find(UNRANKED)->second);

  //double current_execution_time = 0
  //vector currently executing actions


  std::vector<TaskInstEndTimeSet> executing_tasks;

  double current_execution_time = 0;

  int total_tasks = befores.size();
  int completed_tasks = 0;

  while(completed_tasks < total_tasks){

	  for(PrecedenceSet::iterator it = befores.begin(); it != befores.end();){
		
		  PrecedenceSet::iterator prev_it = it++;
		  if((befores.find(prev_it->first))->second.empty()){
			
			  TaskInstEndTimeSet new_execute;
			  new_execute.inst = prev_it->first;

			
			  if(prev_it->first == INIT_TASK_INST_ID){
				  new_execute.end_time = current_execution_time + 0;
			  }else{
				  new_execute.end_time = current_execution_time + this->planner_->get_impl(working_plan_tmp->get_impl_id(prev_it->first))
					  ->get_duration();
			  }

			  executing_tasks.push_back(new_execute);
			  befores.erase(prev_it);
		  }
	  }

	  std::sort(executing_tasks.begin(), executing_tasks.end());

	  TaskInstEndTimeSet next_done = *executing_tasks.begin();
	  current_execution_time = next_done.end_time;

	  std::list<TaskInstEndTimeSet> to_remove;

	  for(std::vector<TaskInstEndTimeSet>::iterator it3 = executing_tasks.begin(); 
		  it3 != executing_tasks.end();){

			if(it3->end_time != current_execution_time){	 
			  break;
			 }
		  std::vector<TaskInstEndTimeSet>::iterator prev_it = it3++;

		  std::cout<<"Task "<<prev_it->inst<<" finishes at time "<<prev_it->end_time<<std::endl;
		  completed_tasks++;

		  for(PrecedenceSet::iterator it2 = befores.begin(); it2 != befores.end(); it2++){
			  it2->second.erase(prev_it->inst);
		  }
		  befores.erase(prev_it->inst);
		  to_remove.push_front(*prev_it);
	//	  executing_tasks.erase(prev_it);
	  }

	  for( std::list<TaskInstEndTimeSet>::iterator it3 = to_remove.begin(); it3 != to_remove.end(); it3++){
		  std::vector<TaskInstEndTimeSet>::iterator it4;
		  for(it4 = executing_tasks.begin(); it4 != executing_tasks.end(); it4++){
			  if(it4->inst == it3->inst){
				break;
			  }
		  }
		  executing_tasks.erase(it4);
	  }
  }

  //while #actions finished != total actions
  //find all actions with no more befores
  //add to currently executing actions sorted by current_time + their duration
  //take action(s if there are multiple that end at the same time) off currently executing actions
  //
  //remove them from the befores of all other actions
  //remove them from befores
  //current execution time = when they finish
*/





  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  // DON'T KNOW WHO'S CODE THIS IS OR WHY IT WAS COMMENTED OUT...
  // PLEASE REMOVE CODE OR ADD COMMENTS TO EXPLAIN.
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  /*
  std::list<TaskInstID> execute_this_time;

  int time = 0;
  while(!befores.empty()){

	  execute_this_time.clear();

	  for(PrecedenceSet::iterator it = befores.begin(); it != befores.end();){
		
		  PrecedenceSet::iterator prev_it = it++;

		  if((befores.find(prev_it->first))->second.empty()){
			
			  execute_this_time.push_front(prev_it->first);

			  befores.erase(prev_it);
		  }
	  }

	
	  for(std::list<TaskInstID>::iterator it = execute_this_time.begin(); it != execute_this_time.end(); it++){
	
		  std::cout<<"Task "<<(*it)<<" can execute at time "<<time<<std::endl;

		  for(PrecedenceSet::iterator it2 = befores.begin(); it2 != befores.end(); it2++){
				it2->second.erase(*it);
		  }
	  }
	  time++;
  }
  */

  return true;
};