HOG2
MountainCar.cpp
Go to the documentation of this file.
1 /*
2  * MountainCar.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 11/29/10.
6  * Copyright 2010 University of Denver. All rights reserved.
7  *
8  */
9 
10 #include "MountainCar.h"
11 #include <math.h>
12 
14 {
15  minPosition = -1.2;
16  maxPosition = 0.6;
17  minVelocity = -0.07;
18  maxVelocity = 0.07;
19  goalPosition = 0.5;
20  accelerationFactor = 0.001;
21  gravityFactor = -0.0025;
22  hillPeakFrequency = 3.0;
23 }
24 
25 void MountainCarEnvironment::GetSuccessors(const MountainCarState &nodeID, std::vector<MountainCarState> &neighbors) const
26 {
27  neighbors.resize(0);
28  neighbors.push_back(nodeID);
29  neighbors.push_back(nodeID);
30  neighbors.push_back(nodeID);
31  ApplyAction(neighbors[0], 1);
32  ApplyAction(neighbors[1], 0);
33  ApplyAction(neighbors[2], -1);
34 }
35 
36 void MountainCarEnvironment::GetActions(const MountainCarState &nodeID, std::vector<MountainCarAction> &actions) const
37 {
38  actions.resize(0);
39  actions.push_back(1);
40  actions.push_back(0);
41  actions.push_back(-1);
42 }
43 
45 {
46  assert(!"GetAction not implemented");
47 }
48 
50 {
52  if (s.speed > maxVelocity) {
53  s.speed = maxVelocity;
54  }
55  if (s.speed < minVelocity)
56  {
57  s.speed = minVelocity;
58  }
59  s.loc += s.speed;
60  if (s.loc > maxPosition)
61  {
62  s.loc = maxPosition;
63  }
64  if (s.loc < minPosition)
65  {
66  s.loc = minPosition;
67  }
68  if (s.loc == minPosition && s.speed < 0)
69  {
70  s.speed = 0;
71  }
72 }
73 
74 double MountainCarEnvironment::GetHeightAtPosition(double queryPosition) const
75 {
76  return -sin(hillPeakFrequency * (queryPosition));
77 }
78 
79 double MountainCarEnvironment::GetSlope(double queryPosition) const
80 {
81  /*The curve is generated by cos(hillPeakFrequency(x-pi/2)) so the
82  * pseudo-derivative is cos(hillPeakFrequency* x)
83  */
84  return cos(hillPeakFrequency * queryPosition);
85 }
86 
87 
89 {
90  s2 = s1;
91  ApplyAction(s2, a);
92 }
93 
95 {
96  a = -a;
97  return false; // actions aren't really invertable
98 }
99 
101 double MountainCarEnvironment::HCost(const MountainCarState &node1, const MountainCarState &node2) const
102 { return 0; }
103 
104 double MountainCarEnvironment::GCost(const MountainCarState &node1, const MountainCarState &node2) const
105 {
106  return 1;
107 }
108 
110 {
111  return 1;
112 }
113 
115 {
116  return fgreater(node.loc, goalPosition);
117 }
118 
120 {
121  return (((int)(node.loc*1000))<<10) + node.speed*1000;
122 }
123 
125 {
126  return act+1;
127 }
128 
130 {
131 // minPosition = -1.2;
132 // maxPosition = 0.6;
133  glBegin(GL_LINE_STRIP);
134  glColor3f(0, 1.0, 0);
135  for (double x = minPosition; x < maxPosition; x += 0.1)
136  {
137  glVertex3f(x, GetHeightAtPosition(x), 0);
138  }
139  glEnd();
140 }
141 
145 
MountainCar.h
MountainCarState::speed
double speed
Definition: MountainCar.h:21
MountainCarEnvironment::GetAction
virtual MountainCarAction GetAction(const MountainCarState &s1, const MountainCarState &s2) const
Definition: MountainCar.cpp:44
MountainCarState::loc
double loc
Definition: MountainCar.h:20
MountainCarEnvironment::maxPosition
double maxPosition
Definition: MountainCar.h:72
MountainCarEnvironment::GoalTest
virtual bool GoalTest(const MountainCarState &node, const MountainCarState &goal) const
Definition: MountainCar.cpp:114
MountainCarEnvironment::hillPeakFrequency
double hillPeakFrequency
Definition: MountainCar.h:78
MountainCarEnvironment::HCost
virtual double HCost(const MountainCarState &node1, const MountainCarState &node2) const
Heuristic value between two arbitrary nodes.
Definition: MountainCar.cpp:101
MountainCarEnvironment::MountainCarEnvironment
MountainCarEnvironment()
Definition: MountainCar.cpp:13
MountainCarEnvironment::InvertAction
virtual bool InvertAction(MountainCarAction &a) const
Definition: MountainCar.cpp:94
MountainCarEnvironment::GetStateHash
virtual uint64_t GetStateHash(const MountainCarState &node) const
Definition: MountainCar.cpp:119
MountainCarEnvironment::goalPosition
double goalPosition
Definition: MountainCar.h:75
MountainCarEnvironment::GetHeightAtPosition
double GetHeightAtPosition(double queryPosition) const
Definition: MountainCar.cpp:74
MountainCarEnvironment::minPosition
double minPosition
Definition: MountainCar.h:71
MountainCarEnvironment::maxVelocity
double maxVelocity
Definition: MountainCar.h:74
MountainCarEnvironment::minVelocity
double minVelocity
Definition: MountainCar.h:73
MountainCarEnvironment::ApplyAction
virtual void ApplyAction(MountainCarState &s, MountainCarAction a) const
Definition: MountainCar.cpp:49
MountainCarEnvironment::GetSuccessors
virtual void GetSuccessors(const MountainCarState &nodeID, std::vector< MountainCarState > &neighbors) const
Definition: MountainCar.cpp:25
MountainCarEnvironment::GetActions
virtual void GetActions(const MountainCarState &nodeID, std::vector< MountainCarAction > &actions) const
Definition: MountainCar.cpp:36
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
MountainCarEnvironment::GetActionHash
virtual uint64_t GetActionHash(MountainCarAction act) const
Definition: MountainCar.cpp:124
MountainCarEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: MountainCar.cpp:129
MountainCarState
Definition: MountainCar.h:15
MountainCarAction
int MountainCarAction
Definition: MountainCar.h:28
MountainCarEnvironment::accelerationFactor
double accelerationFactor
Definition: MountainCar.h:76
MountainCarEnvironment::GCost
virtual double GCost(const MountainCarState &node1, const MountainCarState &node2) const
Definition: MountainCar.cpp:104
MountainCarEnvironment::GetNextState
virtual void GetNextState(const MountainCarState &, MountainCarAction, MountainCarState &) const
Definition: MountainCar.cpp:88
MountainCarEnvironment::GetSlope
double GetSlope(double queryPosition) const
Definition: MountainCar.cpp:79
MountainCarEnvironment::gravityFactor
double gravityFactor
Definition: MountainCar.h:77
node
Nodes to be stored within a Graph.
Definition: Graph.h:170