trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
problem_description.hpp
1 #include "trajopt/common.hpp"
2 #include "json_marshal.hpp"
3 #include <boost/function.hpp>
4 #include <boost/shared_ptr.hpp>
5 #include "traj_plotter.hpp"
6 
7 namespace sco{struct OptResults;}
8 
9 namespace trajopt {
10 
11 using namespace json_marshal;
12 using namespace Json;
13 typedef Json::Value TrajOptRequest;
14 typedef Json::Value TrajOptResponse;
15 using std::string;
16 
17 struct TermInfo;
18 typedef boost::shared_ptr<TermInfo> TermInfoPtr;
19 class TrajOptProb;
20 typedef boost::shared_ptr<TrajOptProb> TrajOptProbPtr;
21 struct ProblemConstructionInfo;
22 struct TrajOptResult;
23 typedef boost::shared_ptr<TrajOptResult> TrajOptResultPtr;
24 
25 TrajOptProbPtr TRAJOPT_API ConstructProblem(const ProblemConstructionInfo&);
26 TrajOptProbPtr TRAJOPT_API ConstructProblem(const Json::Value&, OpenRAVE::EnvironmentBasePtr env);
27 TrajOptResultPtr TRAJOPT_API OptimizeProblem(TrajOptProbPtr, bool plot);
28 
29 enum TermType {
30  TT_COST,
31  TT_CNT
32 };
33 
34 #define DEFINE_CREATE(classname) \
35  static TermInfoPtr create() {\
36  TermInfoPtr out(new classname());\
37  return out;\
38  }
39 
40 
45 class TRAJOPT_API TrajOptProb : public OptProb {
46 public:
47  TrajOptProb();
48  TrajOptProb(int n_steps, ConfigurationPtr rad);
49  ~TrajOptProb() {}
50  VarVector GetVarRow(int i) {
51  return m_traj_vars.row(i);
52  }
53  Var& GetVar(int i, int j) {
54  return m_traj_vars.at(i,j);
55  }
56  VarArray& GetVars() {
57  return m_traj_vars;
58  }
59  int GetNumSteps() {return m_traj_vars.rows();}
60  int GetNumDOF() {return m_traj_vars.cols();}
61  ConfigurationPtr GetRAD() {return m_rad;}
62  OR::EnvironmentBasePtr GetEnv() {return m_rad->GetEnv();}
63 
64  void SetInitTraj(const TrajArray& x) {m_init_traj = x;}
65  TrajArray GetInitTraj() {return m_init_traj;}
66 
67  TrajPlotterPtr GetPlotter() {return m_trajplotter;}
68 
69  friend TrajOptProbPtr ConstructProblem(const ProblemConstructionInfo&);
70 
71 private:
72  VarArray m_traj_vars;
73  ConfigurationPtr m_rad;
74  TrajArray m_init_traj;
75  TrajPlotterPtr m_trajplotter;
76 };
77 
78 void TRAJOPT_API SetupPlotting(TrajOptProb& prob, Optimizer& opt);
79 
80 struct TRAJOPT_API TrajOptResult {
81  vector<string> cost_names, cnt_names;
82  vector<double> cost_vals, cnt_viols;
83  TrajArray traj;
85 };
86 
87 struct BasicInfo {
88  bool start_fixed;
89  int n_steps;
90  string manip;
91  string robot; // optional
92  IntVec dofs_fixed; // optional
93  void fromJson(const Json::Value& v);
94 };
95 
99 struct InitInfo {
100  enum Type {
101  STATIONARY,
102  GIVEN_TRAJ,
103  };
104  Type type;
105  TrajArray data;
106  void fromJson(const Json::Value& v);
107 };
108 
109 
110 struct TRAJOPT_API MakesCost {
111 };
112 struct TRAJOPT_API MakesConstraint {
113 };
114 
119 struct TRAJOPT_API TermInfo {
120 
121  string name; // xxx is this used?
122  TermType term_type;
123  virtual void fromJson(const Json::Value& v)=0;
124  virtual void hatch(TrajOptProb& prob) = 0;
125 
126 
127  static TermInfoPtr fromName(const string& type);
128 
133  typedef TermInfoPtr (*MakerFunc)(void);
134  static void RegisterMaker(const std::string& type, MakerFunc);
135 
136  virtual ~TermInfo() {}
137 private:
138  static std::map<string, MakerFunc> name2maker;
139 };
140 // void fromJson(const Json::Value& v, TermInfoPtr&);
141 
145 struct TRAJOPT_API ProblemConstructionInfo {
146 public:
147  BasicInfo basic_info;
148  vector<TermInfoPtr> cost_infos;
149  vector<TermInfoPtr> cnt_infos;
150  InitInfo init_info;
151 
152  OR::EnvironmentBasePtr env;
153  RobotAndDOFPtr rad;
154 
155  ProblemConstructionInfo(OR::EnvironmentBasePtr _env) : env(_env) {}
156  void fromJson(const Value& v);
157 };
158 
164 struct PoseCostInfo : public TermInfo, public MakesCost, public MakesConstraint {
165  int timestep;
166  Vector3d xyz;
167  Vector4d wxyz;
168  Vector3d pos_coeffs, rot_coeffs;
169  // double coeff;
170  KinBody::LinkPtr link;
171  void fromJson(const Value& v);
172  void hatch(TrajOptProb& prob);
173  DEFINE_CREATE(PoseCostInfo);
174 };
175 
176 
185 struct JointPosCostInfo : public TermInfo, public MakesCost {
186  DblVec vals, coeffs;
187  int timestep;
188  void fromJson(const Value& v);
189  void hatch(TrajOptProb& prob);
190  DEFINE_CREATE(JointPosCostInfo)
191 };
192 
193 
194 
200 struct CartVelCntInfo : public TermInfo, public MakesConstraint {
201  int first_step, last_step;
202  KinBody::LinkPtr link;
203  double max_displacement;
204  void fromJson(const Value& v);
205  void hatch(TrajOptProb& prob);
206  DEFINE_CREATE(CartVelCntInfo)
207 };
208 
217 struct JointVelCostInfo : public TermInfo, public MakesCost {
218  DblVec coeffs;
219  void fromJson(const Value& v);
220  void hatch(TrajOptProb& prob);
221  DEFINE_CREATE(JointVelCostInfo)
222 };
223 
225  DblVec vals;
226  int first_step, last_step;
227  void fromJson(const Value& v);
228  void hatch(TrajOptProb& prob);
229  DEFINE_CREATE(JointVelConstraintInfo)
230 };
231 
243 struct CollisionCostInfo : public TermInfo, public MakesCost {
245  int first_step, last_step;
247  DblVec coeffs;
249  DblVec dist_pen;
250  bool continuous;
252  int gap;
253  void fromJson(const Value& v);
254  void hatch(TrajOptProb& prob);
255  DEFINE_CREATE(CollisionCostInfo)
256 };
257 
258 
259 // TODO: unify with joint position constraint
265  DblVec vals;
267  int timestep;
268  void fromJson(const Value& v);
269  void hatch(TrajOptProb& prob);
270  DEFINE_CREATE(JointConstraintInfo)
271 };
272 
273 
274 
275 }