38 #include "examples/learningSpines/tgCPGCableControl.h"
40 #include "dev/dhustigschultz/BigPuppy_SpineOnly_Stats/BaseQuadModelLearning.h"
47 #include "examples/learningSpines/tgCPGCableControl.h"
49 #include "neuralNet/Neural Network v2/neuralNetwork.h"
51 #include <json/json.h>
97 lp, hp, kt, kp, kv, def, cl, lf, hf),
98 freqFeedbackMin(ffMin),
99 freqFeedbackMax(ffMax),
100 ampFeedbackMin(afMin),
101 ampFeedbackMax(afMax),
102 phaseFeedbackMin(pfMin),
103 phaseFeedbackMax(pfMax),
107 theirHipMuscles(thm),
109 theirLegMuscles(tlm),
110 ourHighMuscles(ohighm),
111 theirHighMuscles(thighm),
113 freqFeedbackMax2(ffMax2)
124 std::string resourcePath) :
132 JSONHierarchyFeedbackControl::~JSONHierarchyFeedbackControl()
144 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
145 if ( !parsingSuccessful )
148 std::cout <<
"Failed to parse configuration\n"
149 << reader.getFormattedErrorMessages();
150 throw std::invalid_argument(
"Bad filename for JSON");
155 Json::Value spineNodeVals = root.get(
"spineNodeVals",
"UTF-8");
156 Json::Value legNodeVals = root.get(
"legNodeVals",
"UTF-8");
157 Json::Value spineEdgeVals = root.get(
"spineEdgeVals",
"UTF-8");
158 Json::Value hipEdgeVals = root.get(
"hipEdgeVals",
"UTF-8");
159 Json::Value legEdgeVals = root.get(
"legEdgeVals",
"UTF-8");
161 std::cout << spineNodeVals << std::endl;
163 spineNodeVals = spineNodeVals.get(
"params",
"UTF-8");
164 legNodeVals = legNodeVals.get(
"params",
"UTF-8");
165 spineEdgeVals = spineEdgeVals.get(
"params",
"UTF-8");
166 hipEdgeVals = hipEdgeVals.get(
"params",
"UTF-8");
167 legEdgeVals = legEdgeVals.get(
"params",
"UTF-8");
170 array_4D spineEdgeParams =
scaleEdgeActions(spineEdgeVals,m_config.segmentSpan,m_config.theirMuscles,m_config.ourMuscles);
171 array_4D hipEdgeParams =
scaleEdgeActions(hipEdgeVals,m_config.segmentSpan,m_config.theirHipMuscles,m_config.ourHipMuscles);
172 array_4D legEdgeParams =
scaleEdgeActions(legEdgeVals,m_config.segmentSpan,m_config.theirLegMuscles,m_config.ourLegMuscles);
173 array_2D spineNodeParams = scaleNodeActions(spineNodeVals, m_config.highFreq, m_config.freqFeedbackMax);
174 array_2D legNodeParams = scaleNodeActions(legNodeVals, m_config.highFreq, m_config.freqFeedbackMax);
178 Json::Value highNodeVals = root.get(
"highNodeVals",
"UTF-8");
179 Json::Value highEdgeVals = root.get(
"highEdgeVals",
"UTF-8");
181 highNodeVals = highNodeVals.get(
"params",
"UTF-8");
182 highEdgeVals = highEdgeVals.get(
"params",
"UTF-8");
185 Json::Value highLowEdgeVals = root.get(
"hLowVals",
"UTF-8");
186 highLowEdgeVals = highLowEdgeVals.get(
"params",
"UTF-8");
188 array_4D highEdgeParams =
scaleEdgeActions(highEdgeVals,2,m_config.theirHighMuscles,m_config.ourHighMuscles);
189 array_2D highNodeParams = scaleNodeActions(highNodeVals, m_config.highFreq2, m_config.freqFeedbackMax2);
192 setupHighCPGs(subject, highNodeParams, highEdgeParams);
195 setupCPGs(subject, spineNodeParams, legNodeParams, spineEdgeParams, hipEdgeParams, legEdgeParams);
198 setupHighLowCouplings(subject, highLowEdgeVals);
200 Json::Value feedbackParams = root.get(
"feedbackVals",
"UTF-8");
201 feedbackParams = feedbackParams.get(
"params",
"UTF-8");
204 m_config.numStates = feedbackParams.get(
"numStates",
"UTF-8").asInt();
205 m_config.numActions = feedbackParams.get(
"numActions",
"UTF-8").asInt();
208 std::string nnFile = controlFilePath + feedbackParams.get(
"neuralFilename",
"UTF-8").asString();
210 nn =
new neuralNetwork(m_config.numStates, m_config.numStates*2, m_config.numActions);
212 nn->loadWeights(nnFile.c_str());
214 initConditions = subject.getSegmentCOM(m_config.segmentNumber);
215 for (
int i = 0; i < initConditions.size(); i++)
217 std::cout << initConditions[i] <<
" ";
219 std::cout << std::endl;
220 #ifdef LOGGING // Conditional compile for data logging
221 m_dataObserver.
onSetup(subject);
224 #if (0) // Conditional Compile for debug info
225 std::cout << *m_pCPGSys << std::endl;
234 std::vector<double> structureCOM = subject.getCOM(m_config.segmentNumber);
236 for(std::size_t i=0; i<3; i++)
238 metrics.push_back(structureCOM[i]);
243 Json::Value prevMetrics = root.get(
"metrics", Json::nullValue);
245 Json::Value subMetrics;
246 subMetrics[
"initial COM x"] = metrics[0];
247 subMetrics[
"initial COM y"] = metrics[1];
248 subMetrics[
"initial COM z"] = metrics[2];
250 prevMetrics.append(subMetrics);
251 root[
"metrics"] = prevMetrics;
254 payloadLog.open(controlFilename.c_str(),ofstream::out);
256 payloadLog << root << std::endl;
264 if (m_updateTime >= m_config.controlTime)
267 std::vector<double> desComs = getFeedback(subject);
270 std::size_t numControllers = subject.getNumberofMuslces() * 3;
272 double descendingCommand = 0.0;
273 std::vector<double> desComs (numControllers, descendingCommand);
277 m_pCPGSys->
update(desComs, m_updateTime);
279 catch (std::runtime_error& e)
286 #ifdef LOGGING // Conditional compile for data logging
287 m_dataObserver.
onStep(subject, m_updateTime);
293 double currentHeight = subject.getSegmentCOM(m_config.segmentNumber)[1];
296 if (currentHeight > m_config.maxHeight || currentHeight < m_config.minHeight)
300 throw std::runtime_error(
"Height out of range");
311 std::vector<double> finalConditions = subject.getSegmentCOM(m_config.segmentNumber);
313 const double newX = finalConditions[0];
314 const double newZ = finalConditions[2];
315 const double oldX = initConditions[0];
316 const double oldZ = initConditions[2];
318 const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
319 (newZ-oldZ) * (newZ-oldZ));
323 scores.push_back(-1.0);
327 scores.push_back(distanceMoved);
332 double totalEnergySpent=0;
336 for(std::size_t i=0; i<tmpStrings.size(); i++)
343 const double previousLength = stringHist.
restLengths[j-1];
344 const double currentLength = stringHist.
restLengths[j];
346 double motorSpeed = (currentLength-previousLength);
349 const double workDone = previousTension * motorSpeed;
350 totalEnergySpent += workDone;
354 scores.push_back(totalEnergySpent);
357 std::vector<double> structureCOM = subject.getCOM(m_config.segmentNumber);
359 for(std::size_t i=0; i<3; i++)
361 metrics.push_back(structureCOM[i]);
364 std::cout <<
"Dist travelled " << scores[0] << std::endl;
369 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
370 if ( !parsingSuccessful )
373 std::cout <<
"Failed to parse configuration\n"
374 << reader.getFormattedErrorMessages();
375 throw std::invalid_argument(
"Bad filename for JSON");
378 Json::Value prevScores = root.get(
"scores", Json::nullValue);
379 Json::Value prevMetrics = root.get(
"metrics", Json::nullValue);
381 Json::Value subScores;
382 subScores[
"distance"] = scores[0];
383 subScores[
"energy"] = scores[1];
385 Json::Value subMetrics;
386 subMetrics[
"final COM x"] = metrics[0];
387 subMetrics[
"final COM y"] = metrics[1];
388 subMetrics[
"final COM z"] = metrics[2];
390 prevScores.append(subScores);
391 prevMetrics.append(subMetrics);
393 root[
"scores"] = prevScores;
394 root[
"metrics"] = prevMetrics;
397 payloadLog.open(controlFilename.c_str(),ofstream::out);
399 payloadLog << root << std::endl;
404 for(
size_t i = 0; i < m_spineControllers.size(); i++)
406 delete m_spineControllers[i];
408 m_spineControllers.clear();
410 for(
size_t i = 0; i < m_leftShoulderControllers.size(); i++)
412 delete m_leftShoulderControllers[i];
414 m_leftShoulderControllers.clear();
416 for(
size_t i = 0; i < m_rightShoulderControllers.size(); i++)
418 delete m_rightShoulderControllers[i];
420 m_rightShoulderControllers.clear();
422 for(
size_t i = 0; i < m_leftHipControllers.size(); i++)
424 delete m_leftHipControllers[i];
426 m_leftHipControllers.clear();
428 for(
size_t i = 0; i < m_rightHipControllers.size(); i++)
430 delete m_rightHipControllers[i];
432 m_rightHipControllers.clear();
434 for(
size_t i = 0; i < m_leftForelegControllers.size(); i++)
436 delete m_leftForelegControllers[i];
438 m_leftForelegControllers.clear();
440 for(
size_t i = 0; i < m_rightForelegControllers.size(); i++)
442 delete m_rightForelegControllers[i];
444 m_rightForelegControllers.clear();
446 for(
size_t i = 0; i < m_leftHindlegControllers.size(); i++)
448 delete m_leftHindlegControllers[i];
450 m_leftHindlegControllers.clear();
452 for(
size_t i = 0; i < m_rightHindlegControllers.size(); i++)
454 delete m_rightHindlegControllers[i];
456 m_rightHindlegControllers.clear();
458 for(
size_t i = 0; i < m_highControllers.size(); i++)
460 delete m_highControllers[i];
462 m_highControllers.clear();
470 void JSONHierarchyFeedbackControl::setupCPGs(
BaseQuadModelLearning& subject, array_2D spineNodeActions, array_2D legNodeActions, array_4D spineEdgeActions, array_4D hipEdgeActions, array_4D legEdgeActions)
487 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
493 for (std::size_t i = 0; i < spineMuscles.size(); i++)
499 spineMuscles[i]->attach(pStringControl);
504 m_spineControllers.push_back(pStringControl);
508 for (std::size_t i = 0; i < m_spineControllers.size(); i++)
511 assert(pStringInfo != NULL);
518 if (m_config.useDefault)
520 pStringInfo->setupControl(*p_ipc);
524 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
533 for (std::size_t i = 0; i < leftShoulderMuscles.size(); i++)
539 leftShoulderMuscles[i]->attach(pStringControl);
544 m_leftShoulderControllers.push_back(pStringControl);
548 for (std::size_t i = 0; i < m_leftShoulderControllers.size(); i++)
551 assert(pStringInfo != NULL);
552 pStringInfo->
setConnectivity(m_leftShoulderControllers, hipEdgeActions);
558 if (m_config.useDefault)
560 pStringInfo->setupControl(*p_ipc);
564 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
569 for (std::size_t i = 0; i < rightShoulderMuscles.size(); i++)
575 rightShoulderMuscles[i]->attach(pStringControl);
580 m_rightShoulderControllers.push_back(pStringControl);
584 for (std::size_t i = 0; i < m_rightShoulderControllers.size(); i++)
587 assert(pStringInfo != NULL);
588 pStringInfo->
setConnectivity(m_rightShoulderControllers, hipEdgeActions);
594 if (m_config.useDefault)
596 pStringInfo->setupControl(*p_ipc);
600 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
605 for (std::size_t i = 0; i < leftHipMuscles.size(); i++)
611 leftHipMuscles[i]->attach(pStringControl);
616 m_leftHipControllers.push_back(pStringControl);
620 for (std::size_t i = 0; i < m_leftHipControllers.size(); i++)
623 assert(pStringInfo != NULL);
630 if (m_config.useDefault)
632 pStringInfo->setupControl(*p_ipc);
636 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
641 for (std::size_t i = 0; i < rightHipMuscles.size(); i++)
647 leftHipMuscles[i]->attach(pStringControl);
652 m_rightHipControllers.push_back(pStringControl);
656 for (std::size_t i = 0; i < m_rightHipControllers.size(); i++)
659 assert(pStringInfo != NULL);
666 if (m_config.useDefault)
668 pStringInfo->setupControl(*p_ipc);
672 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
681 for (std::size_t i = 0; i < leftForelegMuscles.size(); i++)
687 leftForelegMuscles[i]->attach(pStringControl);
692 m_leftForelegControllers.push_back(pStringControl);
696 for (std::size_t i = 0; i < m_leftForelegControllers.size(); i++)
699 assert(pStringInfo != NULL);
700 pStringInfo->
setConnectivity(m_leftForelegControllers, legEdgeActions);
706 if (m_config.useDefault)
708 pStringInfo->setupControl(*p_ipc);
712 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
717 for (std::size_t i = 0; i < rightForelegMuscles.size(); i++)
723 rightForelegMuscles[i]->attach(pStringControl);
728 m_rightForelegControllers.push_back(pStringControl);
732 for (std::size_t i = 0; i < m_rightForelegControllers.size(); i++)
735 assert(pStringInfo != NULL);
736 pStringInfo->
setConnectivity(m_rightForelegControllers, legEdgeActions);
742 if (m_config.useDefault)
744 pStringInfo->setupControl(*p_ipc);
748 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
753 for (std::size_t i = 0; i < leftHindlegMuscles.size(); i++)
759 leftHindlegMuscles[i]->attach(pStringControl);
764 m_leftHindlegControllers.push_back(pStringControl);
768 for (std::size_t i = 0; i < m_leftHindlegControllers.size(); i++)
771 assert(pStringInfo != NULL);
772 pStringInfo->
setConnectivity(m_leftHindlegControllers, legEdgeActions);
778 if (m_config.useDefault)
780 pStringInfo->setupControl(*p_ipc);
784 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
789 for (std::size_t i = 0; i < rightHindlegMuscles.size(); i++)
795 rightHindlegMuscles[i]->attach(pStringControl);
800 m_rightHindlegControllers.push_back(pStringControl);
804 for (std::size_t i = 0; i < m_rightHindlegControllers.size(); i++)
807 assert(pStringInfo != NULL);
808 pStringInfo->
setConnectivity(m_rightHindlegControllers, legEdgeActions);
814 if (m_config.useDefault)
816 pStringInfo->setupControl(*p_ipc);
820 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
825 void JSONHierarchyFeedbackControl::setupHighLowCouplings(
BaseQuadModelLearning& subject, Json::Value highLowEdgeActions)
831 double lowerLimit = m_config.lowPhase;
832 double upperLimit = m_config.highPhase;
833 double range = upperLimit - lowerLimit;
835 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
837 Json::Value::iterator edgeIt = highLowEdgeActions.end();
846 std::vector<int> spineConnectivityList;
847 std::vector<double> spineWeights;
848 std::vector<double> spinePhases;
850 std::vector<int> leftShoulderConnectivityList;
851 std::vector<double> leftShoulderWeights;
852 std::vector<double> leftShoulderPhases;
854 std::vector<int> leftForelegConnectivityList;
855 std::vector<double> leftForelegWeights;
856 std::vector<double> leftForelegPhases;
858 std::vector<int> rightShoulderConnectivityList;
859 std::vector<double> rightShoulderWeights;
860 std::vector<double> rightShoulderPhases;
862 std::vector<int> rightForelegConnectivityList;
863 std::vector<double> rightForelegWeights;
864 std::vector<double> rightForelegPhases;
866 std::vector<int> leftHipConnectivityList;
867 std::vector<double> leftHipWeights;
868 std::vector<double> leftHipPhases;
870 std::vector<int> leftHindlegConnectivityList;
871 std::vector<double> leftHindlegWeights;
872 std::vector<double> leftHindlegPhases;
874 std::vector<int> rightHipConnectivityList;
875 std::vector<double> rightHipWeights;
876 std::vector<double> rightHipPhases;
878 std::vector<int> rightHindlegConnectivityList;
879 std::vector<double> rightHindlegWeights;
880 std::vector<double> rightHindlegPhases;
882 Json::Value param = *edgeIt;
883 assert(param.size() == 2);
887 for (
size_t i = 0; i < m_spineControllers.size(); i++)
890 int nodeNum = m_spineControllers[i]->getNodeNumber();
893 spineConnectivityList.push_back(nodeNum);
894 spineWeights.push_back(param[0].asDouble());
895 spinePhases.push_back(param[1].asDouble() * (range) + lowerLimit);
897 m_CPGFBSys.defineConnections(b, spineConnectivityList, spineWeights, spinePhases);
902 for (
size_t i = 0; i < m_leftShoulderControllers.size(); i++)
905 int nodeNum = m_leftShoulderControllers[i]->getNodeNumber();
908 leftShoulderConnectivityList.push_back(nodeNum);
909 leftShoulderWeights.push_back(param[0].asDouble());
910 leftShoulderPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
912 m_CPGFBSys.defineConnections(b, leftShoulderConnectivityList, leftShoulderWeights, leftShoulderPhases);
916 for (
size_t i = 0; i < m_leftForelegControllers.size(); i++)
919 int nodeNum = m_leftForelegControllers[i]->getNodeNumber();
922 leftForelegConnectivityList.push_back(nodeNum);
923 leftForelegWeights.push_back(param[0].asDouble());
924 leftForelegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
926 m_CPGFBSys.defineConnections(b, leftForelegConnectivityList, leftForelegWeights, leftForelegPhases);
933 for (
size_t i = 0; i < m_rightShoulderControllers.size(); i++)
936 int nodeNum = m_rightShoulderControllers[i]->getNodeNumber();
939 rightShoulderConnectivityList.push_back(nodeNum);
940 rightShoulderWeights.push_back(param[0].asDouble());
941 rightShoulderPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
943 m_CPGFBSys.defineConnections(b, rightShoulderConnectivityList, rightShoulderWeights, rightShoulderPhases);
947 for (
size_t i = 0; i < m_rightForelegControllers.size(); i++)
950 int nodeNum = m_rightForelegControllers[i]->getNodeNumber();
953 rightForelegConnectivityList.push_back(nodeNum);
954 rightForelegWeights.push_back(param[0].asDouble());
955 rightForelegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
957 m_CPGFBSys.defineConnections(b, rightForelegConnectivityList, rightForelegWeights, rightForelegPhases);
964 for (
size_t i = 0; i < m_leftHipControllers.size(); i++)
967 int nodeNum = m_leftHipControllers[i]->getNodeNumber();
970 leftHipConnectivityList.push_back(nodeNum);
971 leftHipWeights.push_back(param[0].asDouble());
972 leftHipPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
974 m_CPGFBSys.defineConnections(b, leftHipConnectivityList, leftHipWeights, leftHipPhases);
978 for (
size_t i = 0; i < m_leftHindlegControllers.size(); i++)
981 int nodeNum = m_leftHindlegControllers[i]->getNodeNumber();
984 leftHindlegConnectivityList.push_back(nodeNum);
985 leftHindlegWeights.push_back(param[0].asDouble());
986 leftHindlegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
988 m_CPGFBSys.defineConnections(b, leftHindlegConnectivityList, leftHindlegWeights, leftHindlegPhases);
995 for (
size_t i = 0; i < m_rightHipControllers.size(); i++)
998 int nodeNum = m_rightHipControllers[i]->getNodeNumber();
1001 rightHipConnectivityList.push_back(nodeNum);
1002 rightHipWeights.push_back(param[0].asDouble());
1003 rightHipPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1005 m_CPGFBSys.defineConnections(b, rightHipConnectivityList, rightHipWeights, rightHipPhases);
1010 for (
size_t i = 0; i < m_rightHindlegControllers.size(); i++)
1013 int nodeNum = m_rightHindlegControllers[i]->getNodeNumber();
1016 rightHindlegConnectivityList.push_back(nodeNum);
1017 rightHindlegWeights.push_back(param[0].asDouble());
1018 rightHindlegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1020 m_CPGFBSys.defineConnections(b, rightHindlegConnectivityList, rightHindlegWeights, rightHindlegPhases);
1023 assert(highLowEdgeActions.begin() == edgeIt);
1026 void JSONHierarchyFeedbackControl::setupHighCPGs(
BaseQuadModelLearning& subject, array_2D highNodeActions, array_4D highEdgeActions)
1028 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
1031 for (std::size_t b = 0; b < 5; b++)
1045 m_highControllers.push_back(pStringControl);
1049 for (std::size_t i = 0; i < m_highControllers.size(); i++)
1052 assert(pStringInfo != NULL);
1058 array_2D JSONHierarchyFeedbackControl::scaleNodeActions (Json::Value actions,
double highFreq,
double freqFeedbackMax)
1060 std::size_t numControllers = actions.size();
1061 std::size_t numActions = actions[0].size();
1063 array_2D nodeActions(boost::extents[numControllers][numActions]);
1065 array_2D limits(boost::extents[2][numActions]);
1068 assert(numActions == 5);
1070 limits[0][0] = m_config.lowFreq;
1071 limits[1][0] = highFreq;
1072 limits[0][1] = m_config.lowAmp;
1073 limits[1][1] = m_config.highAmp;
1074 limits[0][2] = m_config.freqFeedbackMin;
1075 limits[1][2] = freqFeedbackMax;
1076 limits[0][3] = m_config.ampFeedbackMin;
1077 limits[1][3] = m_config.ampFeedbackMax;
1078 limits[0][4] = m_config.phaseFeedbackMin;
1079 limits[1][4] = m_config.phaseFeedbackMax;
1081 Json::Value::iterator nodeIt = actions.begin();
1084 for( std::size_t i = 0; i < numControllers; i++)
1086 Json::Value nodeParam = *nodeIt;
1087 for( std::size_t j = 0; j < numActions; j++)
1089 nodeActions[i][j] = ( (nodeParam.get(j, 0.0)).asDouble() *
1090 (limits[1][j] - limits[0][j])) + limits[0][j];
1099 (Json::Value edgeParam,
int segmentSpan,
int theirMuscles,
int ourMuscles)
1101 assert(edgeParam[0].size() == 2);
1103 double lowerLimit = m_config.lowPhase;
1104 double upperLimit = m_config.highPhase;
1105 double range = upperLimit - lowerLimit;
1107 array_4D actionList(boost::extents[segmentSpan][theirMuscles][ourMuscles][m_config.params]);
1120 Json::Value::iterator edgeIt = edgeParam.end();
1124 while (i < segmentSpan)
1126 while(j < theirMuscles)
1128 while(k < ourMuscles)
1130 if (edgeIt == edgeParam.begin())
1132 std::cout <<
"ran out before table populated!"
1139 if (i == 1 && j == k)
1147 Json::Value edgeParam = *edgeIt;
1148 assert(edgeParam.size() == 2);
1150 actionList[i][j][k][0] = edgeParam[0].asDouble();
1153 actionList[i][j][k][1] = edgeParam[1].asDouble() *
1154 (range) + lowerLimit;
1170 std::cout<<
"Params used: " << count << std::endl;
1172 assert(edgeParam.begin() == edgeIt);
1180 std::vector<double> feedback;
1184 double *inputs =
new double[m_config.numStates];
1186 std::size_t n = allCables.size();
1187 for(std::size_t i = 0; i != n; i++)
1189 std::vector< std::vector<double> > actions;
1192 std::vector<double > state = getCableState(cable);
1195 for (std::size_t i = 0; i < state.size(); i++)
1197 inputs[i]=state[i] / 2.0 + 0.5;
1200 double *output = nn->feedForwardPattern(inputs);
1201 vector<double> tmpAct;
1202 for(
int j=0;j<m_config.numActions;j++)
1204 tmpAct.push_back(output[j]);
1206 actions.push_back(tmpAct);
1208 std::vector<double> cableFeedback = transformFeedbackActions(actions);
1210 feedback.insert(feedback.end(), cableFeedback.begin(), cableFeedback.end());
1214 std::size_t n2 = m_highControllers.size();
1215 for (
size_t i = 0; i != n2; i++)
1217 std::vector< std::vector<double> > actions;
1220 for (std::size_t i = 0; i < m_config.numStates; i++)
1225 double *output = nn->feedForwardPattern(inputs);
1226 vector<double> tmpAct;
1227 for(
int j=0;j<m_config.numActions;j++)
1229 tmpAct.push_back(output[j]);
1231 actions.push_back(tmpAct);
1233 std::vector<double> cableFeedback = transformFeedbackActions(actions);
1235 feedback.insert(feedback.end(), cableFeedback.begin(), cableFeedback.end());
1245 std::vector<double> state;
1251 const double maxTension = cable.
getConfig().maxTens;
1252 state.push_back((cable.
getTension() - maxTension / 2.0) / maxTension);
1257 std::vector<double> JSONHierarchyFeedbackControl::transformFeedbackActions(std::vector< std::vector<double> >& actions)
1260 std::vector<double> feedback;
1263 const std::size_t numControllers = 1;
1264 const std::size_t numActions = m_config.numActions;
1266 assert( actions.size() == numControllers);
1267 assert( actions[0].size() == numActions);
1270 for( std::size_t i = 0; i < numControllers; i++)
1272 for( std::size_t j = 0; j < numActions; j++)
1274 feedback.push_back(actions[i][j] * 2.0 - 1.0);
JSONHierarchyFeedbackControl(JSONHierarchyFeedbackControl::Config config, std::string args, std::string resourcePath="")
Contains the definition of class ImpedanceControl. $Id$.
void update(std::vector< double > &descCom, double dt)
std::deque< double > tensionHistory
virtual const double getTension() const
virtual const double getStartLength() const
void setConnectivity(const std::vector< tgCPGActuatorControl * > &allStrings, array_4D edgeParams)
A controller for the template class BaseQuadModelLearning Implementing a CPG hierachy for MountainGoa...
virtual void onTeardown(BaseQuadModelLearning &subject)
std::deque< double > restLengths
virtual void onStep(BaseQuadModelLearning &subject, double dt)
Definition of the tgCPGStringControl observer class.
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...
A series of functions to assist with file input/output.
Config(int ss, int tm, int om, int param, int segnum=6, double ct=0.1, double la=0, double ha=30, double lp=-1 *M_PI, double hp=M_PI, double kt=0.0, double kp=1000.0, double kv=100.0, bool def=true, double cl=10.0, double lf=0.0, double hf=30.0, double ffMin=0.0, double ffMax=0.0, double afMin=0.0, double afMax=0.0, double pfMin=0.0, double pfMax=0.0, double maxH=60.0, double minH=1.0, int ohm=10, int thm=10, int olm=10, int tlm=10, int ohighm=5, int thighm=5, double hf2=20.0, double ffMax2=0.0)
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
Contains the definition of class tgBasicActuator.
const Config & getConfig() const
std::vector< T * > find(const tgTagSearch &tagSearch)
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(BaseQuadModelLearning &subject)
virtual array_4D scaleEdgeActions(Json::Value actions, int segmentSpan, int theirMuscles, int ourMuscles)
Definition of class CPGEquationsFB.
virtual void onSetup(tgModel &model)
virtual const double getCurrentLength() const
void notifyStep(double dt)
void assignNodeNumberFB(CPGEquationsFB &CPGSys, array_2D nodeParams)