summaryrefslogtreecommitdiff
path: root/SA_POP/SANet/SANet.h
blob: 50a8eb8c98fff15f7b6aa1fc8411dfb3549f8a1f (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
// -*- C++ -*-

//=============================================================================
/**
 * @file  SANet.h
 *
 * This file contains the Network class definition for spreading activation
 * networks.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#ifndef SA_NETWORK_H_
#define SA_NETWORK_H_

#include <iostream>
#include <map>
#include <stdexcept>
#include "SANet_Types.h"
#include "SANode.h"

namespace SANet {
  /// Map from task node ID to pointer.
  typedef std::map<TaskID, TaskNode *> TaskNodeMap;

  /// Map from condition node ID to pointer.
  typedef std::map<CondID, CondNode *> CondNodeMap;

  /// Map of precondition links to ports.
  typedef std::map<PrecondLink, PortID> PrecondLinkPortMap;

  /// Map of effect links to ports.
  typedef std::map<EffectLink, PortID> EffectLinkPortMap;

  /**
   * @class Network
   *
   * @brief Spreading activation network class.
   */
  class Network {
  public:
    /// Constructor.
    Network (void);

    /// Destructor.
    virtual ~Network ();



    // ************************************************************************
    // Network creations methods.
    // ************************************************************************

    /// Add a new task node to the network.
    /**
     * @param ID  Node ID.
     *
     * @param name  Node name.
     *
     * @param atten_factor  Attenuation factor (to bias toward shorter plans).
     *
     * @param cost  Cost of performing task.
     *
     * @param prior_prob  Prior probability of success.
     */
    virtual void add_task (TaskID ID, std::string name,
      MultFactor atten_factor, TaskCost cost, Probability prior_prob);

    /// Add a new condition node to the network.
    /**
     * @param ID  Node ID.
     *
     * @param name  Node name.
     *
     * @param atten_factor  Attenuation factor (to bias toward shorter plans).
     *
     * @param true_prob  Initial probability that value is true.
     *
     * @param false_prob  Initial probability that value is false.
     *
     * @param cond_kind The type of condition
     *
     * @param goal_util  Initial utility (positive for goals, zero otherwise).
     */
    virtual void add_cond (CondID ID, std::string name,
      MultFactor atten_factor, Probability true_prob, Probability false_prob,
      Utility goal_util, CondKind cond_kind);

    /// Add condition to task link.
    /**
     * @param cond_ID  Condition node ID.
     *
     * @param task_ID  Task node ID.
     *
     * @param true_prob  Conditional probability of task success given
     * condition node = true.
     *
     * @param false_prob  Conditional probability of task success given
     * condition node = false.
     *
     * @param port_ID  ID of port (on task) associated with this condition
     *                 (used for data nodes).
     */
    virtual void add_precond_link (CondID cond_ID, TaskID task_ID,
      Probability true_prob, Probability false_prob, PortID port_ID = "");

    /// Add task to condition link.
    /**
     * @param cond_ID  Condition node ID.
     *
     * @param task_ID  Task node ID.
     *
     * @param weight  Link weight (probability task sets condition to
     * true, or negative of the probability task sets condition to false).
     *
     * @param port_ID  ID of port (on task) associated with this condition
     *                 (used for data nodes).
     */
    virtual void add_effect_link (TaskID task_ID, CondID cond_ID,
      LinkWeight weight, PortID port_ID = "");



    // ************************************************************************
    // Print methods.
    // ************************************************************************

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

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



    // ************************************************************************
    // Network update methods (spreading activation, environment/system state
    // changes, and goal changes).
    // ************************************************************************

    /// Run spreading activation.
    /**
     * @param max_steps  Maximum steps for which to run spreading activation.
     */
    virtual void update (int max_steps);

    /// 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, Probability 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, Utility 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 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 condition's current value (probability of being true).
    /**
     * @param cond_id  The condition id.
     *
     * @return  Probability that condition is true.
     */
    virtual Probability 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 current expected utility.
    /**
     * @param task_id  The task id.
     *
     * @return  Current task expected utility.
     */
    virtual Utility 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 Utility 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 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);

    /// 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 the name of the port associated with a precondition of a task.
    /**
     * @param cond_id  The precondition id.
     *
     * @param task_id  The task id.
     *
     * @return  Port id.
     */
    virtual PortID get_precond_port (CondID cond_id, TaskID task_id);

    /// Get the name of the port associated with an effect of a task.
    /**
     * @param task_id  The task id.
     *
     * @param cond_id  The effect condition id.
     *
     * @return  Port id.
     */
    virtual PortID get_effect_port (TaskID task_id, CondID cond_id);

    /// 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);

	/// Get the duration of a task
	/**
	 * @param task_id ID of the Task
	 *
	 * @return duration of the task
	 */
	virtual TimeValue get_duration (TaskID task_id);

  protected:
    /// Map from ID to node pointer for all task nodes in network.
    TaskNodeMap task_nodes_;

    /// Map from ID to node pointer for all condition nodes in network.
    CondNodeMap cond_nodes_;

    /// Map from precondition links to associated ports.
    PrecondLinkPortMap precond_links_;

    /// Map from effect links to associated ports.
    EffectLinkPortMap effect_links_;

    /// Goals.
    GoalMap goals_;

    /// Current step.
    int step_;
  };
};


#endif /* SA_NETWORK_H_ */