23 #include <boost/program_options.hpp> 24 namespace po = boost::program_options;
43 for (
unsigned int i=0; i<
data.size(); i++)
45 data[i].setValue(x, counter);
46 counter +=
data[i].dofs.size();
52 std::vector<std::string> text;
54 std::ifstream textstream;
56 textstream.open(filename.c_str());
59 while (textstream.good() && std::getline(textstream, line)) {
60 text.push_back(line +
"\n");
64 for (
unsigned int i=0; i < text.size(); i++)
90 if (extraparams != NULL)
91 ((
COptimizer*) extraparams)->callback(nparam, x, fj);
97 for (
int i=0; i<nparam; i++)
109 for (
unsigned int j=0; j<
examples.size(); j++)
125 for (
unsigned int i=0; i<
functions.size(); i++)
127 double tmpf =
functions[i]->getDistance();
128 LOG_MSG(6,
"%d %d: %f\n", j, i, tmpf);
130 distances[j] += tmpf;
159 LOG_MSG(2,
"Value: %f X: ", fj[0]);
160 for (
int i=0; i<nparam; i++)
168 LOG_MSG(2,
"Goal-Value: %f State-Values: ", fj[0]);
169 for (
int i=0; i<nparam; i++)
173 LOG_MSG(2,
"%s\n", tmp.c_str());
179 for (
unsigned int i=0; i<first.size() && i<second.size(); i++)
180 if (fabsf(first[i] - second[i]) > eps)
188 std::string line, prev;
189 std::ifstream textstream;
190 std::vector<double> input;
191 std::vector<std::vector<double> > examples2;
192 LOG_VERBOSE(
"Reading data from %s.\n", filename.c_str());
194 textstream.open(filename.c_str());
196 unsigned int lines = 0;
197 while (textstream.good() && std::getline(textstream, line))
200 if (lines <= startId)
205 if (line.find(
":") != std::string::npos)
206 line = line.substr(line.find(
":")+1, line.length());
210 if (line.size() <= 0)
220 examples2.push_back(input);
225 int exampleCount = examples2.size();
226 LOG_VERBOSE(
"Loaded %d ( %d ) examples\n", exampleCount, lines);
227 unsigned int offset = examples2.size() /
examplesMax;
230 for (
unsigned int i=0; i<examples2.size(); i+=offset)
241 for (
unsigned int i=0; i<
data.size(); i++)
243 CVec position, quater;
245 position = matrix[3];
251 tmp +=
printToString(
"Position (xyz): %f %f %f Quaternion (wxyz): %f %f %f %f EulerAngles(ZXZ): %f %f %f \n\n", position.
x, position.
y, position.
z,
252 quater.
w, quater.
x, quater.
y, quater.
z, eulerAngles.
x*180.0/M_PI, eulerAngles.
y*180.0/M_PI, eulerAngles.
z*180.0/M_PI);
260 std::ofstream textstream(filename.c_str());
261 if (textstream.fail())
264 textstream << buffer;
271 std::string tmp, tmp2;
273 size_t found = tmp.find(
"([");
274 size_t found2 = tmp.find(
"])");
276 if (found != std::string::npos && found2 !=std::string::npos)
278 tmp2 = tmp.substr(found+2, found2-found-2);
279 LOG_MSG(2,
"Value: %s\n", tmp2.c_str());
280 return (
int) atof(tmp2.c_str()) - 1;
293 std::vector<double> dofs_max, dofs_min;
294 std::vector<unsigned int>
dofs;
306 for (
unsigned int j=0; j<dofs.size(); j++)
307 LOG_VERBOSE(
"Loaded dof %d: %g %g\n", dofs[j], dofs_min[dofs[j]], dofs_max[dofs[j]]);
316 data.push_back(item);
330 config.
load(filename.c_str());
333 std::vector<TiXmlElement*>
result;
334 config.
findNodes(
"Frames.Frame", result);
335 for (
unsigned int i=0; i<result.size(); i++)
340 valueSetter.
x =
getValue(result[i],
"x");
341 valueSetter.
y =
getValue(result[i],
"y");
342 valueSetter.
z =
getValue(result[i],
"z");
347 valueSetter.
a =
getValue(result[i],
"a");
348 valueSetter.
b =
getValue(result[i],
"b");
349 valueSetter.
g =
getValue(result[i],
"g");
351 if (valueSetter.
x < 0 &&
354 valueSetter.
qw < 0 &&
355 valueSetter.
qx < 0 &&
356 valueSetter.
qy < 0 &&
357 valueSetter.
qz < 0 &&
379 config.
load(filename.c_str());
382 std::vector<TiXmlElement*>
result;
383 config.
findNodes(
"Goal.Function", result);
384 for (
unsigned int i=0; i<result.size(); i++)
392 config.
findNodes(
"Goal.GlobalPosition", result);
393 for (
unsigned int i=0; i<result.size(); i++)
403 config.
findNodes(
"Goal.GlobalOrientation", result);
404 for (
unsigned int i=0; i<result.size(); i++)
413 config.
findNodes(
"Goal.Position", result);
414 for (
unsigned int i=0; i<result.size(); i++)
422 config.
findNodes(
"Goal.Orientation", result);
423 for (
unsigned int i=0; i<result.size(); i++)
483 int nparam,maxIter,verbosity;
484 double *st,*bl,*bu,bigbnd,eps;
486 st=(
double*)calloc(nparam,
sizeof(
double));
487 bl=(
double *)calloc(nparam,
sizeof(
double));
488 bu=(
double *)calloc(nparam,
sizeof(
double));
494 for (
int i=0; i<nparam; i++)
501 if ((
int)initialValues.size() >= nparam)
502 for (
int i=0; i<nparam; i++)
504 st[i] = initialValues[i];
505 if (st[i] < 0.0 || st[i] > 1.0)
506 LOG_MSG(2,
"Error: %d'th value %f has to be in [0,1].\n", i, st[i]);
510 for (
int i=0; i<nparam; i++)
518 for (
int i=0; i<nparam; i++)
532 LOG_MSG(2,
"Best: %f\n", y[0]);
535 for (
int i=0; i<nparam; i++)
540 for (
int i=0; i<nparam; i++)
547 LOG_MSG(2,
"%s\n", output.c_str());
551 this->
result.resize(nparam);
552 for (
int i=0; i<nparam; i++)
564 optimizedPoses.clear();
566 timelineOfPoses.clear();
574 double *st = (
double*)calloc(
result.size(),
sizeof(double));
575 for (
unsigned int i=0; i<
result.size(); i++)
586 for (
unsigned int i=0; i<
data.size(); i++)
596 for (
unsigned int j=0; j<
examples.size(); j++)
std::vector< std::vector< double > > examples
bool isEqual(std::vector< double > &first, std::vector< double > &second, double eps=0.1)
std::vector< std::pair< std::string, CMatrix > > optimizedPoses
void run(const std::vector< double > &initialValues)
bool writeToFile(std::string filename, std::string buffer)
std::string printToString(const char *format,...)
void loadFromXml(CFrameContainer &frames, TiXmlElement *frameNode)
void callback(int nparam, double *x, double *fj)
bool getFrameByName(const char *name, CFrame *&frame)
std::vector< std::vector< std::pair< std::string, CMatrix > > > timelineOfPoses
bool getResult(COptimizerResult &out)
virtual std::string getFrameAsXml()
void getDofs(std::vector< unsigned int > &dofs)
static const char * getAttributeString(TiXmlElement *node, const char *str, const char *def="")
virtual CMatrix getRelativeToBase()
Returns pose in the base (frame with no predecessor) frame.
std::vector< double > values
int getValue(TiXmlElement *node, std::string item)
void setTrimming(unsigned int value)
#define LOG_MSG_LEVEL(index)
std::vector< CFrame * > getFrames()
std::vector< double > result
static void quaternionFromMatrix(const CMatrix &mat, CVec &quaternion)
Transforms a homogenous matrix into quaternion representation.
static void getEulerZXZ(CMatrix &mat, CVec &first)
Transforms homogenous matrix into Euler angle (YZX) representation (two solutions) ...
void setData(std::vector< double > &values)
virtual std::string generateOutput()
virtual void setFrame(CFrame *frame)
void clear()
Clears storage buffer.
std::string toString(bool round=false)
#define LOG_MSG(index, format,...)
bool load(const char *cfgFile)
void loadGoals(std::string filename)
void loadFromFile(const char *filename)
#define LOG_VERBOSE(format,...)
std::vector< unsigned int > dofs
virtual CMatrix getPose()
void setTrimming(double trim)
void loadData(std::string filename, unsigned int startId=0)
std::vector< ValueSetter > valueSetters
void strToArray(std::string text, std::vector< double > &result, std::string delimiter=" ")
CFrame * getFrame(unsigned int id)
void loadDofs(std::string filename)
void loadValueSetters(std::string filename)
std::vector< double > distances
static void rosenbrockCallback(int nparam, double *x, double *fj, void *extraparams)
void rosenbrock(int n, double *x, double *bl, double *bu, double bigbnd, int maxiter, double eps, int verbose, void obj(int, double *, double *, void *), void *extraparams)
std::vector< std::pair< std::string, CMatrix > > allPoses
void load(std::string cfg, std::string data, unsigned int start=0)
void findNodes(const char *name, std::vector< TiXmlElement * > &result, TiXmlElement *start, unsigned int level)
std::vector< OptimizerGoalGlobal * > globalFunctions
Configuration file wrapper.
std::vector< OptimizerContainer > data
std::vector< OptimizerGoal * > functions
std::vector< double > dofs_min
std::vector< double > dofs_max
std::string readFromFile(std::string filename)