43 #include "tgSineStringControl.h"
54 std::string resourcePath,
75 std::vector<double> state;
79 array_2D nodeParams = scaleNodeActions(nodeAdapter.step(dt, state));
81 setupWaves(subject, nodeParams, edgeParams);
83 initConditions = subject.getSegmentCOM(m_config.segmentNumber);
84 #if (0) // Conditional compile for data logging
85 m_dataObserver.
onSetup(subject);
96 if (m_updateTime >= m_config.controlTime)
99 #if (0) // Conditional compile for data logging
100 m_dataObserver.
onStep(subject, m_updateTime);
109 std::vector<double> scores;
112 std::vector<double> finalConditions = subject.getSegmentCOM(m_config.segmentNumber);
114 const double newX = finalConditions[0];
115 const double newZ = finalConditions[2];
116 const double oldX = initConditions[0];
117 const double oldZ = initConditions[2];
119 const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
120 (newZ-oldZ) * (newZ-oldZ));
122 scores.push_back(distanceMoved);
126 double totalEnergySpent=0;
128 vector<tgSpringCableActuator* > tmpSCAs = subject.getAllMuscles();
129 vector<tgBasicActuator* > tmpStrings = tgCast::filter<tgSpringCableActuator, tgBasicActuator>(tmpSCAs);
130 for(std::size_t i=0; i<tmpStrings.size(); i++)
137 const double previousLength = stringHist.
restLengths[j-1];
138 const double currentLength = stringHist.
restLengths[j];
140 double motorSpeed = (currentLength-previousLength);
143 const double workDone = previousTension * motorSpeed;
144 totalEnergySpent += workDone;
148 scores.push_back(totalEnergySpent);
151 nodeAdapter.endEpisode(scores);
153 for(
size_t i = 0; i < m_sineControllers.size(); i++)
155 delete m_sineControllers[i];
157 m_sineControllers.clear();
158 m_allControllers.clear();
161 void LearningSpineSine::setupWaves(
BaseSpineModelLearning& subject, array_2D nodeActions, array_2D edgeActions)
163 std::vector <tgSpringCableActuator*> allMuscles = subject.getAllMuscles();
168 double controlLength;
170 for (std::size_t i = 0; i < allMuscles.size(); i++)
172 if (allMuscles[i]->hasTag(
"inner top"))
177 controlLength = allMuscles[i]->getStartLength();
179 else if (allMuscles[i]->hasTag(
"outer top"))
184 controlLength = 19.5;
186 else if (allMuscles[i]->hasTag(
"inner"))
191 controlLength = allMuscles[i]->getStartLength();
193 else if (allMuscles[i]->hasTag(
"outer"))
198 controlLength = 19.5 ;
202 throw std::runtime_error(
"Missing tags!");
218 allMuscles[i]->attach(pStringControl);
219 m_sineControllers.push_back(pStringControl);
222 assert(m_sineControllers.size() == allMuscles.size());
230 (vector< vector <double> > actions)
232 std::size_t numControllers = edgeConfigData.getintvalue(
"numberOfControllers");
233 std::size_t numActions = edgeConfigData.getintvalue(
"numberOfActions");
235 assert( actions.size() == numControllers);
236 assert( actions[0].size() == numActions);
238 array_2D edgeActions(boost::extents[numControllers][numActions]);
240 array_2D limits(boost::extents[2][numActions]);
243 assert(numActions == 1);
246 limits[0][0] = m_config.lowPhase;
247 limits[1][0] = m_config.highPhase;
250 for( std::size_t i = 0; i < numControllers; i++)
252 for( std::size_t j = 0; j < numActions; j++)
254 edgeActions[i][j] = ( actions[i][j] *
255 (limits[1][j] - limits[0][j])) + limits[0][j];
262 array_2D LearningSpineSine::scaleNodeActions
263 (vector< vector <double> > actions)
265 std::size_t numControllers =
nodeConfigData.getintvalue(
"numberOfControllers");
266 std::size_t numActions =
nodeConfigData.getintvalue(
"numberOfActions");
268 assert( actions.size() == numControllers);
269 assert( actions[0].size() == numActions);
271 array_2D nodeActions(boost::extents[numControllers][numActions]);
273 array_2D limits(boost::extents[2][numActions]);
276 assert(numActions == 2);
279 limits[0][0] = m_config.lowAmp;
280 limits[1][0] = m_config.highAmp;
281 limits[1][1] = m_config.lowFreq;
282 limits[1][1] = m_config.highFreq;
285 for( std::size_t i = 0; i < numControllers; i++)
287 for( std::size_t j = 0; j < numActions; j++)
289 nodeActions[i][j] = ( actions[i][j] *
290 (limits[1][j] - limits[0][j])) + limits[0][j];
Contains the definition of class ImpedanceControl. $Id$.
AnnealEvolution edgeEvolution
std::deque< double > tensionHistory
std::deque< double > restLengths
configuration nodeConfigData
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...
AnnealAdapter edgeAdapter
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 resourcePath="", 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)