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
/**
* @file ActionSimulator.h
* @author <a href="mailto:schlottb@informatik.hu-berlin.de">Stella Alice Schlotter</a>
* Declaration of class ActionSimulator
*/

#ifndef _ActionSimulator_H
#define _ActionSimulator_H

#include <ModuleFramework/Module.h>

// Representations
#include "Representations/Infrastructure/FrameInfo.h"
#include "Representations/Infrastructure/FieldInfo.h"
#include "Representations/Modeling/BallModel.h"
#include "Representations/Modeling/RobotPose.h"
//#include "Representations/Modeling/GoalModel.h"
#include "Representations/Modeling/KickActionModel.h"
//#include "Representations/Modeling/ObstacleModel.h"

//Tools
#include <Tools/Math/Vector2.h>
#include <Tools/Math/Line.h>

#include <Tools/Math/Probabilistics.h>
//#include "Tools/DataStructures/RingBufferWithSum.h"
#include <Tools/DataStructures/ParameterList.h>
//#include "Tools/Filters/AssymetricalBoolFilter.h"


// Debug
#include "Tools/Debug/DebugModify.h"
#include <Tools/Debug/DebugRequest.h>


BEGIN_DECLARE_MODULE(ActionSimulator)
  PROVIDE(DebugModify)
  PROVIDE(DebugRequest)
  PROVIDE(DebugDrawings)
  
  REQUIRE(FrameInfo)
  REQUIRE(FieldInfo)
  REQUIRE(BallModel)
  REQUIRE(RobotPose)

  PROVIDE(KickActionModel)
END_DECLARE_MODULE(ActionSimulator)

class ActionSimulator: public ActionSimulatorBase
{
public:
  ActionSimulator();
  ~ActionSimulator();

  virtual void execute();

  class ActionParams
  {
    public:
      ActionParams():
        speed(0.0),
        speed_std(0.0),
        angle(0.0),
        angle_std(0.0)
      {}
    public:
      double speed;
      double speed_std;
      double angle;
      double angle_std;
  };


  class Action
  {
  private:
    KickActionModel::ActionId _id;
    std::string _name;

    double action_speed;
    double action_speed_std;
    double action_angle;
    double action_angle_std;
    double friction;
    
  public:
    Action(KickActionModel::ActionId _id, const ActionParams& params, double friction) : 
		  _id(_id), 
      _name(KickActionModel::getName(_id)),
      action_speed(params.speed),
      action_speed_std(params.speed_std),
      action_angle(params.angle),
      action_angle_std(params.angle_std),
      friction(friction)
	  {
    }

	Vector2d predict(const Vector2d& ball, bool noise) const;
    KickActionModel::ActionId id() const { return _id; }
    const std::string& name() const { return _name; }
    //getter and setter???
    void setAngle(double angle){ action_angle = angle; }
    double getAngle() const { return action_angle; }
  };
  
  enum BallPositionCategory
  {
    INFIELD,
    OPPOUT,
    OWNOUT,
    LEFTOUT,
    RIGHTOUT,
    OPPGOAL,
    OWNGOAL,
    COLLISION,
    NUMBER_OF_BallPositionCategory
  };

  class CategorizedBallPosition
  {
    private:
      Vector2d ballPosition;
      BallPositionCategory category;
    public:
      CategorizedBallPosition(const Vector2d& position, BallPositionCategory cat):
        ballPosition(position),
        category(cat)
      {}
      BallPositionCategory cat() const {return category;} 
      const Vector2d& pos() const {return ballPosition;} 
  };

  class ActionResults 
  {
  public:
    typedef std::vector<CategorizedBallPosition> Positions;
  private:
    Positions ballPositions;
    // histogram of categories
    std::vector<int> cat_histogram;

  public:
    ActionResults() 
      : cat_histogram(NUMBER_OF_BallPositionCategory+1)
    {
    }

    const Positions& positions() const {
      return ballPositions;
    }

    int category(BallPositionCategory cat) const {
      return cat_histogram[cat];
    }

    double likelihood(BallPositionCategory cat) const {
      ASSERT(cat_histogram[NUMBER_OF_BallPositionCategory] > 0);
      return static_cast<double>(cat_histogram[cat]) / static_cast<double>(cat_histogram[NUMBER_OF_BallPositionCategory]);
    }

    void reset() {
      ballPositions.clear();
      cat_histogram.clear();
      cat_histogram.resize(NUMBER_OF_BallPositionCategory+1);
    }

    void add(const Vector2d& position, BallPositionCategory cat) {
      ballPositions.push_back(CategorizedBallPosition(position, cat));
      cat_histogram[cat]++;
      cat_histogram[NUMBER_OF_BallPositionCategory]++;
    }
  };
private:
  const std::vector<Math::LineSegment> ownGoalBackSides;
  const std::vector<Math::LineSegment> oppGoalBackSides;

private:
  bool calculateCollision(const std::vector<Math::LineSegment>& lines, const Vector2d& start, const Vector2d& end, Vector2d& result) const;

  BallPositionCategory classifyBallPosition( const Vector2d& ballPosition ) const;<--- Function 'classifyBallPosition' argument 1 names different: declaration 'ballPosition' definition 'globalBallPosition'.

private:
  inline double exp256(const double& x) const
  {
    // exp(x) = lim(n->inf) (1 + x/n)^n
    // for n=256 about 10x faster than exp but around 2.5 % off on x in [-10, 10]
    double y = 1.0 + x / 256.0;
    y *= y;
    y *= y;
    y *= y;
    y *= y;
    y *= y;
    y *= y;
    y *= y;
    y *= y;
    return y;
  }

  inline double gaussian(const double& x, const double& y, const double& muX, const double& muY, const double& sigmaX, const double& sigmaY) const
  {
    double facX = (x - muX) * (x - muX) / (2.0 * sigmaX * sigmaX);
    double facY = (y - muY) * (y - muY) / (2.0 * sigmaY * sigmaY);
    return exp256(-1.0 * (facX + facY));
  }

  inline double slope(const double& x, const double& y, const double& slopeX, const double& slopeY) const
  {
    return slopeX * x + slopeY * y;
  }

public:
  void simulateAction(const Action& action, ActionResults& result, size_t numParticles) const;
  double evaluateAction(const ActionResults& results) const;
  double evaluateAction(const Vector2d& a) const;
  void draw_potential_field() const;
};

#endif  /* _ActionSimulator_H */