43 #include "tgSineStringControl.h"
74 std::vector<double> state;
78 array_2D nodeParams = scaleNodeActions(nodeAdapter.step(dt, state));
80 setupWaves(subject, nodeParams, edgeParams);
82 initConditions = subject.getSegmentCOM(m_config.segmentNumber);
83 #if (0) // Conditional compile for data logging
84 m_dataObserver.
onSetup(subject);
95 if (m_updateTime >= m_config.controlTime)
98 #if (0) // Conditional compile for data logging
99 m_dataObserver.
onStep(subject, m_updateTime);
108 std::vector<double> scores;
111 std::vector<double> finalConditions = subject.getSegmentCOM(m_config.segmentNumber);
113 const double newX = finalConditions[0];
114 const double newZ = finalConditions[2];
115 const double oldX = initConditions[0];
116 const double oldZ = initConditions[2];
118 const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
119 (newZ-oldZ) * (newZ-oldZ));
121 scores.push_back(distanceMoved);
125 double totalEnergySpent=0;
127 vector<tgSpringCableActuator* > tmpSCAs = subject.getAllMuscles();
128 vector<tgBasicActuator* > tmpStrings = tgCast::filter<tgSpringCableActuator, tgBasicActuator>(tmpSCAs);
129 for(
int i=0; i<tmpStrings.size(); i++)
136 const double previousLength = stringHist.
restLengths[j-1];
137 const double currentLength = stringHist.
restLengths[j];
139 double motorSpeed = (currentLength-previousLength);
142 const double workDone = previousTension * motorSpeed;
143 totalEnergySpent += workDone;
147 scores.push_back(totalEnergySpent);
149 edgeAdapter.endEpisode(scores);
150 nodeAdapter.endEpisode(scores);
152 for(
size_t i = 0; i < m_sineControllers.size(); i++)
154 delete m_sineControllers[i];
156 m_sineControllers.clear();
157 m_allControllers.clear();
160 void LearningSpineSine::setupWaves(
BaseSpineModelLearning& subject, array_2D nodeActions, array_2D edgeActions)
162 std::vector <tgSpringCableActuator*> allMuscles = subject.getAllMuscles();
167 double controlLength;
169 for (std::size_t i = 0; i < allMuscles.size(); i++)
171 if (allMuscles[i]->hasTag(
"inner top"))
176 controlLength = allMuscles[i]->getStartLength();
178 else if (allMuscles[i]->hasTag(
"outer top"))
183 controlLength = 19.5;
185 else if (allMuscles[i]->hasTag(
"inner"))
190 controlLength = allMuscles[i]->getStartLength();
192 else if (allMuscles[i]->hasTag(
"outer"))
197 controlLength = 19.5 ;
201 throw std::runtime_error(
"Missing tags!");
217 allMuscles[i]->attach(pStringControl);
218 m_sineControllers.push_back(pStringControl);
221 assert(m_sineControllers.size() == allMuscles.size());
229 (vector< vector <double> > actions)
231 std::size_t numControllers = edgeConfigData.getintvalue(
"numberOfControllers");
232 std::size_t numActions = edgeConfigData.getintvalue(
"numberOfActions");
234 assert( actions.size() == numControllers);
235 assert( actions[0].size() == numActions);
237 array_2D edgeActions(boost::extents[numControllers][numActions]);
239 array_2D limits(boost::extents[2][numActions]);
242 assert(numActions == 1);
245 limits[0][0] = m_config.lowPhase;
246 limits[1][0] = m_config.highPhase;
249 for( std::size_t i = 0; i < numControllers; i++)
251 for( std::size_t j = 0; j < numActions; j++)
253 edgeActions[i][j] = ( actions[i][j] *
254 (limits[1][j] - limits[0][j])) + limits[0][j];
261 array_2D LearningSpineSine::scaleNodeActions
262 (vector< vector <double> > actions)
264 std::size_t numControllers = nodeConfigData.getintvalue(
"numberOfControllers");
265 std::size_t numActions = nodeConfigData.getintvalue(
"numberOfActions");
267 assert( actions.size() == numControllers);
268 assert( actions[0].size() == numActions);
270 array_2D nodeActions(boost::extents[numControllers][numActions]);
272 array_2D limits(boost::extents[2][numActions]);
275 assert(numActions == 2);
278 limits[0][0] = m_config.lowAmp;
279 limits[1][0] = m_config.highAmp;
280 limits[1][1] = m_config.lowFreq;
281 limits[1][1] = m_config.highFreq;
284 for( std::size_t i = 0; i < numControllers; i++)
286 for( std::size_t j = 0; j < numActions; j++)
288 nodeActions[i][j] = ( actions[i][j] *
289 (limits[1][j] - limits[0][j])) + limits[0][j];
Contains the definition of class ImpedanceControl. $Id$.
std::deque< double > tensionHistory
std::deque< double > restLengths
virtual void onSetup(BaseSpineModelLearning &subject)
A class to read a learning configuration from a .ini file.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
virtual void onTeardown(BaseSpineModelLearning &subject)
Contains the definition of class tgBasicActuator.
virtual array_2D scalePhaseActions(std::vector< std::vector< double > > actions)
virtual void onStep(BaseSpineModelLearning &subject, double dt)
virtual void onStep(tgModel &model, double dt)
LearningSpineSine(BaseSpineCPGControl::Config config, std::string args, std::string ec="edgeConfig.ini", std::string nc="nodeConfig.ini")
void initialize(AnnealEvolution *evo, bool isLearning, configuration config)
virtual void onSetup(tgModel &model)
Controller for TetraSpineLearningModel.
void notifyStep(double dt)