summaryrefslogtreecommitdiff
path: root/SA_POP/Planner.h
blob: 7a2661bd6c4cf33c19e86e111fee0ded35183976 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// -*- C++ -*-
// $Id$

//=============================================================================
/**
 * @file  Planner.h
 *
 * This file contains the Planner abstract base class definition for planners,
 * which direct planning and mediate communication between other planning
 * objects.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#ifndef SA_POP_PLANNER_H_
#define SA_POP_PLANNER_H_


#include <string>
#include <set>
#include <map>
#include "SA_POP_Types.h"
#include "PlanStrategy.h"
#include "SchedStrategy.h"
#include "WorkingPlan.h"
#include "TaskMap.h"
#include "OutAdapter.h"
#include "PlanCommands.h"
#include "SANet/SANet.h"

namespace SA_POP {

  /**
   * @class Planner
   *
   * @brief Abstract base class for planners, which direct planning and
   *        mediate communication between other planning objects.
   */
  class Planner {
  public:


    /// Constructor.
    Planner (void);

    /// Destructor.
    virtual ~Planner (void);



    // ************************************************************************
    // Initialization methods.
    // ************************************************************************

    /// Add planning objects.
    /**
     * @param sanet  Spreading activation network.
     *
     * @param plan_strat  PlanStrategy object for planning.
     *
     * @param sched_strat  SchedStrategy object for scheduling.
     *
     * @param working_plan  WorkingPlan object for holding plan in progress.
     *
     * @param task_map  TaskMap object for associating tasks with
     *                  implementations and resources.
     */
    virtual void set_objects (SANet::Network *sanet, PlanStrategy *plan_strat,
      SchedStrategy *sched_strat, WorkingPlan *working_plan,
      TaskMap *task_map);

    /// Add output adapter.
    /**
     * @param out  OutAdapter to add.
     */
    virtual void add_out_adapter (OutAdapter *out);


    virtual void generate_all_threats(void);


    // ************************************************************************
    // Planning and changed planning info accessor methods.
    // ************************************************************************

    /// Run planning.
    /**
     * @param sa_max_steps  Maximum steps to run spreading activation.
     *
     * @param goal  Goal for which to plan.
     *
     * @return  True if planning succeeded, false otherwise.
     */
    virtual bool plan (size_t sa_max_steps, SA_POP::Goal goal);
    
    /// Replan with new goal.
    /**
     * @param sa_max_steps  Maximum steps to run spreading activation.
     *
     * @param goal  Goal for which to plan.
     *
     * @return  True if planning succeeded, false otherwise.
     */
    virtual bool replan (size_t sa_max_steps, SA_POP::Goal goal);

    /// Replan with current goal.
    /**
     * @param sa_max_steps  Maximum steps to run spreading activation.
     *
     * @return  True if planning succeeded, false otherwise.
     */
    virtual bool replan (size_t sa_max_steps);

    /// Get current plan.
    /**
     * @return  Reference to current plan.
     */
    virtual const Plan& get_plan (void);

    /// Get last set of expected utility changes.
    /**
     * @return  Reference to last set of expected utility changes.
     */
    virtual const TaskEUMap& get_eu_changes (void);

    /// Print network.
    /**
     * @param strm  Output stream on which to print network representation.
     *
     * @param verbose  Whether to print verbose representation.
     */
    virtual void print_sanet (std::basic_ostream<char, std::char_traits<char> >& strm
      = std::cout, bool verbose = false);



    // ************************************************************************
    // Recursive planning/scheduling methods.
    // ************************************************************************

    /// Recursively plan (satisfy all open conditions & schedule constraints).
    /**
     * @return  True if fully satisfied plan found, false otherwise.
     */
    virtual bool recurse_plan (void);

    /// Recursively schedule (satisfy schedule constraints and continue
    /// recursive planning).
    /**
     * @param task_inst  Current task instance being tried in the plan.
     *
     * @return  True if fully satisfied plan found, false otherwise.
     */
    virtual bool recurse_sched (TaskInstID task_inst);

    /// Satisfy scheduling constraints in fully instantiated plan (no
    /// recursive call backs).
    /**
     * 
     *
     * @return  True if fully satisfied plan found, false otherwise.
     */
    virtual bool full_sched ();



    // ************************************************************************
    // Planning/scheduling command execution and undo methods.
    // ************************************************************************

    /// Execute a command (adding it as the current command).
    /**
     * @param command  The command to add and execute.
     */
    virtual void execute_command (PlanCommand *command);

    /// Undo and remove command.
    /**
     * @param id  The id of the command to undo and remove.
     *
     * @exception InvalidCommand  The id provided does not correspond to top
     *                            command.
     */
    virtual void undo_command (CommandID id);

    /// Add a command to be executed later with execute_next().
    /**
     * @param command  The command to add for execution.
     */
    virtual void add_command (PlanCommand *command);

    /// On current command, undo last execution (if any) & execute next option.
    /**
     * @param id  The id of the command to undo and remove.
     *
     * @exception InvalidCommand  The id provided does not correspond to top
     *                            command.
     *
     * @return True if command had an option to execute, false otherwise.
     */
    virtual bool try_next (CommandID id);

    /// Undo and remove all commands back to specified point.
    /**
     * @param id  The id of the command to undo and remove through.
     */
    virtual void undo_through (CommandID id);

    /// Get the current command id.
    /**
     * @return  The id of the current command
     */
    virtual CommandID cur_command_id();


    // ************************************************************************
    // Condition update methods (environment/system state or goal changes).
    // ************************************************************************

    /// Update a condition's current value (probability of being true).
    /**
     * @param cond_id  The condition id.
     *
     * @param true_prob  New probability that condition is true.
     */
    virtual void update_cond_val (CondID cond_id, double true_prob);

    /// Update a condition's (goal) utility.
    /**
     * @param cond_id  The condition id.
     *
     * @param utility  New goal utility of condition.
     */
    virtual void update_cond_util (CondID cond_id, double utility);

    /// Update all condition utilities based on new goal set.
    /**
     * @param goals  Set of goal condition ids and associated utilities.
     */
    virtual void update_goals (GoalMap goals);



    // ************************************************************************
    // General task/condition accessor methods.
    // ************************************************************************

    /// Get a condition's current value (probability of being true).
    /**
     * @param cond_id  The condition id.
     *
     * @return  Probability that condition is true.
     */
    virtual double get_cond_val (CondID cond_id);

    /// Get all goals.
    /**
     * @return  Set of condition ids and associated utilities.
     */
    virtual const GoalMap& get_goals (void);

    /// Get a task's name.
    /**
     * @param task_id  The task id.
     *
     * @return  Task name.
     */
    virtual std::string get_task_name (TaskID task_id);

    /// Get a condition's name.
    /**
     * @param cond_id  The condition id.
     *
     * @return  Condition name.
     */
    virtual std::string get_cond_name (CondID cond_id);

    /// Get a condition's type/kind.
    /**
     * @param cond_id  The condition id.
     *
     * @return  Condition type.
     */
    virtual CondKind get_cond_type (CondID cond_id);

    /// Get a task's current expected utility.
    /**
     * @param task_id  The task id.
     *
     * @return  Current task expected utility.
     */
    virtual double get_task_current_eu (TaskID task_id);

    /// Get a task's future expected utility.
    /**
     * @param task_id  The task id.
     *
     * @return  Future task expected utility.
     */
    virtual double get_task_future_eu (TaskID task_id);

    /// Get all preconditions of a task.
    /**
     * @param task_id  The task id.
     *
     * @return  Set of all preconditions with associated values.
     */
    virtual CondSet get_preconds (TaskID task_id);

    /// Get currently unsatisfied preconditions of a task.
    /**
     * @param task_id  The task id.
     *
     * @return  Set of all unsatisfied preconditions with associated values.
     */
    virtual CondSet get_unsat_preconds (TaskID task_id);

    /// Get all effects of a task.
    /**
     * @param task_id  The task id.
     *
     * @return  Set of all effects with associated values.
     */
    virtual CondSet get_effects (TaskID task_id);

    SANet::LinkWeight get_link(SANet::TaskID id, SANet::CondID cond_ID);

    /// Get all tasks that satisfy a condition.
    /**
     * @param cond_id  The condition id.
     *
     * @return  Set of all tasks that satisfy the given condition.
     */
    virtual TaskSet get_satisfying_tasks (Condition cond);

    /// Get ports for a causal link.
    /**
     * @param task1_id  ID of start task node in causal link.
     *
     * @param cond_id  ID of condition node in both precondition and effect
     *                 links.
     *
     * @param task2_id  ID of end task node in causal link.
     */
    virtual LinkPorts get_clink_ports (TaskID task1_id, CondID cond_id,
      TaskID task2_id);



    // ************************************************************************
    // Planning task/condition info accessor methods.
    // ************************************************************************

    /// Get task id of a task instance.
    /**
     * @param inst_id  The task instance id.
     *
     * @return  The task id of this task instance.
     */
    virtual TaskID get_task_from_inst (TaskInstID inst_id);

    /// Get all current causal link threats.
    /**
     * @return  Set of all current causal link threats.
     */
    virtual CLThreatSet get_all_threats (void);



    // ************************************************************************
    // TaskMap accessor methods (resources, task->implementations,
    // and implementation->resources).
    // ************************************************************************

    /// Get all implementations of a task.
    /**
     * @param task_id  The task id.
     *
     * @return  The set of all implementations (ids) for the given task.
     */
    virtual TaskImplSet get_all_impls (TaskID task_id);

    /// Get task implementation.
    /**
     * @param impl_id  The task implementation id.
     *
     * @return  Reference to the task implementation.
     */
    virtual TaskImpl *get_impl (TaskImplID impl_id);

    /// Get utilization info of a task implementation for a resource.
    /**
     * @param impl_id  The task implementation id.
     *
     * @param resource_id  The resource id.
     *
     * @return  The quantity of resource used.
     */
    virtual ResourceValue get_resource_usage (TaskImplID impl_id,
      ResourceID resource_id);

    /// Get all resources used by a task implementation.
    /**
     * @param impl_id  The task implementation id.
     *
     * @return  The set of all resources used (with associated usage values).
     */
    virtual ResourceMap get_all_resources (TaskImplID impl_id);

    // ************************************************************************
    // Scheduling Precedence Graph/Time Windows/Resources accessor methods.
    // ************************************************************************


	/// Get the Task instances in a particular set of the specified task instance
	/**
	 * @param task_inst The Task Instance whose Precedence Set has been queried
	 *
	 * @param prec_rel The Precedence relation to the task_inst
	 *
	 * @return A pointer to the Task Instance Set that has the relation prec_rel to task_inst
	 */
	virtual const TaskInstSet* get_prec_insts (TaskInstID task_inst, PrecedenceRelation prec_rel);

	/// Get the Start Window of the Task instance
	/**
	 * @param task_inst The Task Instance whose Start Window is required
	 *
	 * @return The Start Window of task_inst
	 */
	virtual TimeWindow get_start_window (TaskInstID task_inst);

	/// Get the End Window of the Task instance
	/**
	 * @param task_inst The Task Instance whose End Window is required
	 *
	 * @return The End Window of task_inst
	 */
	virtual TimeWindow get_end_window (TaskInstID task_inst);

    //Get the duration of a task instance
    /**
     * @param task_inst The task instance of which the duration is returned
     *
     * @return The duration of the task instance
     */
    virtual TimeValue get_duration(TaskInstID task_inst);

    /// Get task implementation id of a task instance.
    /**
     * @param inst_id  The task instance id.
     *
     * @return  The task implementation id of this task instance.
     */
    /// Get task implementation id of a task instance.
	virtual TaskImplID get_task_impl_from_inst (TaskInstID inst_id);

    /// Get the capacity of a resource.
    /**
     * @param res_id The resource id whose capacity that we want to get.
     *
     * @return The capacity of the resource
     */
  virtual ResourceValue get_capacity (ResourceID res_id);

    /// Get the Causal and Scheduling orderings to this task instance
    /**
     * @param inst_id The task instance to which all orderings are required
     */
  virtual TaskInstSet before_orderings (TaskInstID inst_id);

    /// Get the Causal and Scheduling orderings from this task instance
    /**
     * @param inst_id The task instance from which all orderings are required
     */
  virtual TaskInstSet after_orderings (TaskInstID inst_id);

  /// Get all the task instances
  virtual TaskInstSet get_all_insts();
  
    /// Check if the instance id already exists and is being reused.
    /**
     * @param task_inst The task instance being checked
     *
     * @return  True If this task instance already exists.
     */
    virtual bool inst_exists (TaskInstID task_inst);

    /// Get task implementation for a task instance.
    /**
     * @param task_inst  The task instance.
     *
     * @return The task implementation id.
     */
    virtual TaskImplID get_impl_id (TaskInstID task_inst);


   /// (re) Set Effect Link
   /**
   * @param task_inst  The task id
	 * @param task_inst  The Cond ID
	 * @param task_inst  The LinkWeight
	 * 
   *
   * @return nothing
   */
	virtual void update_effect (SANet::TaskID tsk, SANet::CondID cnd, SANet::LinkWeight weight);
  
  /// print the graphviz network of the SANet
  /**
   * @param strm  Output stream on which to print network representation.
   *
   * @return nothing
   */
  virtual void print_graph (std::basic_ostream<char, std::char_traits<char> >& strm, std::map<std::string, std::string>& graphmap);

  virtual WorkingPlan*  get_working_plan(void){return this->working_plan_;};

  virtual void set_backtrack_cmd_id(CommandID cmd){backtrack_cmd = cmd;};

  virtual double calculate_plan_utility(size_t sa_max_steps);

  protected:
    /// Threshold for current probability of a condition to be satisfied.
    const Probability cond_prob_thresh_;

    /// Flag for whether planning objects have been set.
    bool has_objs_;

    /// Spreading activation network.
    SANet::Network *sanet_;

    /// PlanStrategy object for planning.
    PlanStrategy *plan_strat_;

    /// SchedStrategy object for scheduling.
    SchedStrategy *sched_strat_;

    /// WorkingPlan object for holding plan in progress.
    WorkingPlan *working_plan_;

    /// TaskMap object for associating tasks with implementations and resources.
    TaskMap *task_map_;

    /// OutAdapter objects.
    std::set <OutAdapter *> out_adapters_;

    /// Current complete plan.
    Plan plan_;

    /// Last set of expected utility changes.
    TaskEUMap eu_changes_;

    /// Current command.
    PlanCommand *cur_cmd_;

    /// Notify all output adapters that plans have changed.
    virtual void notify_plan_changed (void);

	CommandID backtrack_cmd;

	CommandID not_backtracking;
  };

};  /* SA_POP namespace */

#endif /* SA_POP_PLANNER_H_ */