33 #include "neuralNet/Neural Network v2/neuralNetwork.h"
35 #include <json/json.h>
37 #include <boost/program_options.hpp>
44 namespace po = boost::program_options;
56 unsigned long long rdtsc(){
58 __asm__ __volatile__ (
"rdtsc" :
"=a" (lo),
"=d" (hi));
59 return ((
unsigned long long)hi << 32) | lo;
71 int main(
int argc,
char** argv)
73 std::cout <<
"AppGATests" << std::endl;
84 std::string lowerPath;
87 po::options_description desc(
"Allowed options");
89 (
"help,h",
"produce help message")
90 (
"steps,s", po::value<int>(&nSteps),
"Number of steps per episode to run. Default=60K (60 seconds)")
91 (
"learning_controller,l", po::value<std::string>(&suffix),
"Which learned controller to write to or use. Default = default")
92 (
"blocks,b", po::value<bool>(&add_blocks)->implicit_value(
false),
"Add a block field as obstacles.")
93 (
"hills,H", po::value<bool>(&add_hills)->implicit_value(
false),
"Use hilly terrain.")
94 (
"angle,a", po::value<double>(&startAngle),
"Angle of starting rotation for robot. Degrees. Default = 0")
95 (
"goal_angle,B", po::value<double>(&goalAngle),
"Angle of starting rotation for goal box. Degrees. Default = 0")
96 (
"lower_path,P", po::value<std::string>(&lowerPath),
"Which resources folder in which you want to store controllers. Default = default")
100 po::store(po::parse_command_line(argc, argv, desc), vm);
103 std::string fileName =
"Config.ini";
104 std::string path =
"bmirletz/GATests/";
111 std::string controlFilename = fullPath + suffix;
113 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
114 if ( !parsingSuccessful )
117 std::cout <<
"Failed to parse configuration\n"
118 << reader.getFormattedErrorMessages();
119 throw std::invalid_argument(
"Bad filename for JSON");
122 Json::Value feedbackParams = root.get(
"feedbackVals",
"UTF-8");
123 feedbackParams = feedbackParams.get(
"params",
"UTF-8");
126 const int numberOfInputs = feedbackParams.get(
"numStates",
"UTF-8").asInt();
127 const int numberOfOutputs = feedbackParams.get(
"numActions",
"UTF-8").asInt();
128 const int numberHidden = feedbackParams.get(
"numHidden",
"UTF-8").asInt();
130 std::string nnFile = fullPath + feedbackParams.get(
"neuralFilename",
"UTF-8").asString();
132 neuralNetwork* nn =
new neuralNetwork(numberOfInputs,numberHidden, numberOfOutputs);
134 nn->loadWeights(nnFile.c_str());
136 std::vector<double> state;
137 for (
int i = 0; i < numberOfInputs; i++)
140 state.push_back(1.0);
142 double goal = 1.0 * (double) numberOfOutputs;
146 double *inputs =
new double[numberOfInputs];
147 for (std::size_t i = 0; i < state.size(); i++)
149 inputs[i] = state[i];
152 double *output = nn->feedForwardPattern(inputs);
154 for(std::size_t i = 0; i < numberOfOutputs; i++)
161 for (
int i = 0; i < numberOfInputs; i++)
164 state.push_back(-1.0);
167 for (std::size_t i = 0; i < state.size(); i++)
169 inputs[i] = state[i];
172 double *output2 = nn->feedForwardPattern(inputs);
174 for(std::size_t i = 0; i < numberOfOutputs; i++)
176 score2 += output2[i];
177 if (output2[i] < 0.0)
179 std::cout <<
"Negative value! " << output2[i] << std::endl;
183 std::vector<double> scores;
184 scores.push_back(score1 - score2);
185 scores.push_back(0.0);
188 Json::Value values = feedbackParams.get(
"neuralParams",
"UTF-8");
191 for (
int i = 0; i < values.size(); i++)
193 score1 += values[i].asDouble() * values[i].asDouble();
198 Json::Value prevScores = root.get(
"scores", Json::nullValue);
200 Json::Value subScores;
201 subScores[
"distance"] = score1;
202 subScores[
"energy"] = 0.0;
204 prevScores.append(subScores);
205 root[
"scores"] = prevScores;
207 std::ofstream payloadLog;
208 payloadLog.open(controlFilename.c_str(),std::ofstream::out);
210 payloadLog << root << std::endl;
212 std::cout <<
"Score " << score1 << std::endl;
int main(int argc, char **argv)
A series of functions to assist with file input/output.
static std::string getResourcePath(std::string relPath)
unsigned long long rdtsc()
Rand seeding simular to the evolution and terrain classes.