432 lines
12 KiB
C++
432 lines
12 KiB
C++
|
|
|
|
|
|
//#include "sensors/SensorReader.h"
|
|
//#include "Interpolator.h"
|
|
//#include <KLib/misc/gnuplot/Gnuplot.h>
|
|
//#include <KLib/misc/gnuplot/GnuplotPlot.h>
|
|
//#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
|
|
//#include <KLib/misc/gnuplot/GnuplotMultiplot.h>
|
|
|
|
//#include <KLib/math/neuralnet/NeuralNetIHO.h>
|
|
//#include <KLib/math/optimization/NumOptAlgoGenetic.h>
|
|
|
|
//enum class PracticeType {
|
|
// REST,
|
|
// JUMPING_JACK,
|
|
// SITUPS,
|
|
// PUSHUPS,
|
|
// REJECT,
|
|
//};
|
|
|
|
///** interpolate the output for the given position using the provided range */
|
|
//template <typename T> T blur(K::Interpolator<uint64_t, T>& interpol, const uint64_t ms, const int s = 3) {
|
|
// return interpol.get(ms-s*2) * 0.1 +
|
|
// interpol.get(ms-s) * 0.2 +
|
|
// interpol.get(ms) * 0.4 +
|
|
// interpol.get(ms+s) * 0.2 +
|
|
// interpol.get(ms+s*2) * 0.1;
|
|
//}
|
|
|
|
//struct Practice {
|
|
|
|
// PracticeType type;
|
|
// Recording rec;
|
|
// std::vector<uint64_t> keyGyro;
|
|
|
|
// //Practice(const PracticeType p, const Recording& rec, const std::vector<uint64_t>& keyGyro) : p(p), rec(rec), keyGyro(keyGyro) {;}
|
|
|
|
// K::Interpolator<uint64_t, SensorGyro> getInterpol() {
|
|
// K::Interpolator<uint64_t, SensorGyro> interpol;
|
|
// for (auto it : rec.gyro.values) {interpol.add(it.ts, it.val);}
|
|
// interpol.makeRelative();
|
|
// return interpol;
|
|
// }
|
|
|
|
//};
|
|
|
|
//static constexpr int NUM_IN = 60;
|
|
//static constexpr int NUM_HID = 16;
|
|
//static constexpr int NUM_OUT = 4;
|
|
//static constexpr int NUM_ARGS = NUM_IN*NUM_HID + NUM_HID*NUM_OUT;
|
|
|
|
//static std::vector<float> getNetworkInput(K::Interpolator<uint64_t, SensorGyro>& interpol, const uint64_t pos) {
|
|
|
|
// std::vector<float> val;
|
|
// val.resize(NUM_IN);
|
|
// int idx = 0;
|
|
|
|
// for (int offset = -500; offset < 500; offset += 50) {
|
|
// SensorGyro gyro = interpol.get(pos + offset);
|
|
// val[idx++] = gyro.x;
|
|
// val[idx++] = gyro.y;
|
|
// val[idx++] = gyro.z;
|
|
// assert(idx <= NUM_IN);
|
|
// }
|
|
|
|
// return val;
|
|
|
|
//}
|
|
|
|
///** get the index of the largest element within vec */
|
|
//static int getMaxIdx(const K::NeuralNetResultIHO<NUM_OUT>& vec) {
|
|
// float max = 0;
|
|
// int idx = 0;
|
|
// for (int i = 0; i < NUM_OUT; ++i) {
|
|
// if (vec.values[i] > max) {
|
|
// max = vec.values[i];
|
|
// idx = i;
|
|
// }
|
|
// }
|
|
// return idx;
|
|
//}
|
|
|
|
//struct TMP {int index; float value;};
|
|
//static std::vector<TMP> getSorted(const K::NeuralNetResultIHO<NUM_OUT>& vec) {
|
|
// std::vector<TMP> tmp;
|
|
// for (int i = 0; i < NUM_OUT; ++i) {tmp.push_back( TMP{i, vec.values[i]} );}
|
|
// auto comp = [] (const TMP& t1, const TMP& t2) {return t2.value < t1.value;};
|
|
// std::sort(tmp.begin(), tmp.end(), comp);
|
|
// return tmp;
|
|
//}
|
|
|
|
//static void debug(Practice& p, K::NeuralNetResultIHO<NUM_OUT>& res) {
|
|
// const int maxIdx = getMaxIdx(res);
|
|
// const char max = (res.values[maxIdx] > 0.5) ? (maxIdx + '0') : ('?');
|
|
// std::cout << "practice was: " << (int)p.type;
|
|
// std::cout << " network says: " << max << "\t";
|
|
// std::cout << "[";
|
|
// for (int i = 0; i < NUM_OUT; ++i) {
|
|
// std::cout << res.values[i] << ", ";
|
|
// }
|
|
// std::cout << "]" << std::endl;
|
|
//}
|
|
|
|
//static void debugPlot(Practice& p) {
|
|
|
|
// static K::Gnuplot gp;
|
|
// K::GnuplotPlot plot;
|
|
// K::GnuplotPlotElementLines line[3];
|
|
|
|
// line[0].setColorHex("#ff0000"); line[0].setTitle("x");
|
|
// line[1].setColorHex("#00ff00"); line[1].setTitle("y");
|
|
// line[2].setColorHex("#0000ff"); line[2].setTitle("z");
|
|
|
|
// plot.add(&line[0]);
|
|
// plot.add(&line[1]);
|
|
// plot.add(&line[2]);
|
|
|
|
// K::Interpolator<uint64_t, SensorGyro> interpol = p.getInterpol();
|
|
|
|
// for (int ms = 0; ms < 20000; ms += 50) {
|
|
// SensorGyro s = interpol.get(ms);
|
|
// line[0].add(K::GnuplotPoint2(ms, s.x));
|
|
// line[1].add(K::GnuplotPoint2(ms, s.y));
|
|
// line[2].add(K::GnuplotPoint2(ms, s.z));
|
|
// }
|
|
|
|
// gp.setDebugOutput(true);
|
|
// gp.draw(plot);
|
|
// gp.flush();
|
|
|
|
//}
|
|
|
|
|
|
//int main(void) {
|
|
|
|
// std::vector<Practice> practices;
|
|
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::JUMPING_JACK,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/jumpingjack/jumpingjack_gl_5_subject_3_left.txt"),
|
|
// {1950, 2900, 3850, 4850, 5850, 6850, 7850, 8850, 9800, 10800, 11850}
|
|
// }
|
|
// );
|
|
|
|
|
|
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::REST,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/idle/restposition_gl_24.txt"),
|
|
// {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}
|
|
// }
|
|
// );
|
|
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::SITUPS,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/situps/situps_gl_12_subject_1_left.txt"),
|
|
// {1850, 3250, 4750, 6150, 7550, 8950, 10350, 11600, 13000}
|
|
// }
|
|
// );
|
|
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::PUSHUPS,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/pushups/pushups_gl_8_subject_4_right.txt"),
|
|
// {2750, 4200, 5850, 7400, 9000, 10650}
|
|
// //{3500, 5000, 8300, 9900, 11550}
|
|
// }
|
|
// );
|
|
|
|
|
|
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::REST,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/jumpingjack/jumpingjack_gl_5_subject_3_left.txt"),
|
|
// {1950+500, 2900+500, 3850+500, 4850+500, 5850+500, 6850+500, 7850+500, 8850+500, 9800+500, 10800+500, 11850+500}
|
|
// }
|
|
// );
|
|
//// practices.push_back(
|
|
//// Practice {
|
|
//// PracticeType::REST,
|
|
//// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/pushups/pushups_gl_8_subject_4_right.txt"),
|
|
//// //{2750, 4200, 5850, 7400, 9000, 10650}
|
|
//// {3500, 5000, 8300, 9900, 11550}
|
|
//// }
|
|
//// );
|
|
// practices.push_back(
|
|
// Practice {
|
|
// PracticeType::REST,
|
|
// SensorReader::read("/mnt/firma/kunden/HandyGames/daten/situps/situps_gl_12_subject_1_left.txt"),
|
|
// {1850+600, 3250+600, 4750+600, 6150+600, 7550+600, 8950+600, 10350+600, 11600+600, 13000+600}
|
|
// }
|
|
// );
|
|
|
|
|
|
// debugPlot(practices.back());
|
|
// sleep(100);
|
|
|
|
// class MyOpt : public K::NumOptFunction<NUM_ARGS> {
|
|
|
|
// public:
|
|
|
|
// std::vector<Practice>& practices;
|
|
// K::NeuralNetIHO<NUM_IN, NUM_HID, NUM_OUT>& net;
|
|
|
|
// /** ctor */
|
|
// MyOpt(std::vector<Practice>& practices, K::NeuralNetIHO<NUM_IN, NUM_HID, NUM_OUT>& net) : practices(practices), net(net) {
|
|
// ;
|
|
// }
|
|
|
|
// double getValue(const K::NumOptVector<NUM_ARGS>& args) const {
|
|
|
|
// // configure the network
|
|
// std::vector<float> vals;
|
|
// for(int i = 0; i < NUM_ARGS; ++i) {vals.push_back(args[i]);}
|
|
// net.setAll(vals);
|
|
|
|
// // temporals
|
|
// float points = 0;
|
|
|
|
// // process every practice
|
|
// for (Practice& p : practices) {
|
|
|
|
// // get the values for the neural-net-input
|
|
|
|
// K::Interpolator<uint64_t, SensorGyro> interpol = p.getInterpol();
|
|
|
|
// // process 4 (positive) occurences within the practice
|
|
// for (int key = 0; key < 4; ++key) {
|
|
|
|
// for (int o = -100; o <= +100; o +=50) {
|
|
|
|
// const uint64_t ts = p.keyGyro[key] + o;
|
|
// const std::vector<float> values = getNetworkInput(interpol, ts);
|
|
|
|
// // calculate the output
|
|
// const K::NeuralNetResultIHO<NUM_OUT> res = net.getOutput(values.data());
|
|
|
|
|
|
// // largest value matches the desired type -> good!
|
|
// std::vector<TMP> resSort = getSorted(res);
|
|
// if (resSort[0].index == (int) p.type) {
|
|
// //if ( (resSort[0].value - resSort[1].value) > 0.25 ) {
|
|
// ++points;
|
|
// points += resSort[0].value;
|
|
// points -= resSort[1].value;
|
|
// //}
|
|
// //points += resSort[0].value;
|
|
// //points += (resSort[0].value - resSort[1].value);
|
|
// } else {
|
|
// --points;
|
|
// }
|
|
|
|
//// // update the score
|
|
//// for (int i = 0; i < NUM_OUT; ++i) {
|
|
//// if (i == (int) p.type) {
|
|
//// points += 3 * res.values[i]; // matches
|
|
//// } else {
|
|
//// points -= res.values[i]; // does not match
|
|
//// }
|
|
//// }
|
|
|
|
//// int maxIdx = getMaxIdx(res);
|
|
//// if (maxIdx == (int) p.type) {
|
|
//// ++points;
|
|
//// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// std::cout << points << std::endl;
|
|
// return -points;
|
|
|
|
// }
|
|
|
|
|
|
// };
|
|
|
|
// K::NumOptAlgoGenetic<NUM_ARGS> opt;
|
|
// K::NumOptVector<NUM_ARGS> vec;
|
|
// K::NeuralNetIHO<NUM_IN, NUM_HID, NUM_OUT> net;
|
|
// MyOpt func(practices, net);
|
|
|
|
// opt.setElitism(0.025f);
|
|
// opt.setPopulationSize(300);
|
|
// opt.setMaxIterations(100);
|
|
// opt.setMutation(0.10f);
|
|
// opt.setValRange(0.5);
|
|
// opt.calculateOptimum(func, vec);
|
|
|
|
|
|
//// // process every practice
|
|
//// for (Practice& p : practices) {
|
|
|
|
//// // get the values for the neural-net-input
|
|
|
|
//// K::Interpolator<uint64_t, SensorGyro> interpol = p.getInterpol();
|
|
|
|
//// // process every (positive) occurence within the practice
|
|
//// for (uint64_t ts : p.keyGyro) {
|
|
|
|
//// std::vector<float> values = getNetworkInput(interpol, ts);
|
|
//// K::NeuralNetResultIHO<NUM_OUT> res = net.getOutput(values.data());
|
|
//// debug(p, res);
|
|
|
|
//// {
|
|
//// std::vector<float> values = getNetworkInput(interpol, ts+500);
|
|
//// K::NeuralNetResultIHO<NUM_OUT> res = net.getOutput(values.data());
|
|
//// std::cout << "###"; debug(p, res);
|
|
//// }
|
|
|
|
//// }getMaxIdx
|
|
|
|
//// }
|
|
|
|
|
|
// K::Gnuplot gp1;
|
|
// K::Gnuplot gp2;
|
|
|
|
// K::GnuplotPlot plot1;
|
|
// K::GnuplotPlot plot2;
|
|
|
|
// K::GnuplotMultiplot plot(2,1);
|
|
// plot.add(&plot1);
|
|
// plot.add(&plot2);
|
|
|
|
// K::GnuplotPlotElementLines line[3];
|
|
// line[0].setColorHex("#ff0000"); line[0].setTitle("x");
|
|
// line[1].setColorHex("#00ff00"); line[1].setTitle("y");
|
|
// line[2].setColorHex("#0000ff"); line[2].setTitle("z");
|
|
// plot1.add(&line[0]);
|
|
// plot1.add(&line[1]);
|
|
// plot1.add(&line[2]);
|
|
|
|
// K::GnuplotPlotElementLines netLines[NUM_OUT];
|
|
// netLines[0].setColorHex("#ff0000"); netLines[0].setTitle("REST"); netLines[0].setLineWidth(2);
|
|
// netLines[1].setColorHex("#00ff00"); netLines[1].setTitle("JUMPING_JACK"); netLines[1].setLineWidth(2);
|
|
// netLines[2].setColorHex("#0000ff"); netLines[2].setTitle("SITUPS"); netLines[2].setLineWidth(2);
|
|
// netLines[3].setColorHex("#ffff00"); netLines[3].setTitle("PUSBACKS"); netLines[3].setLineWidth(2);
|
|
|
|
// for (int i = 0; i < NUM_OUT; ++i) {
|
|
// plot2.add(&netLines[i]);
|
|
// }
|
|
|
|
// // process every practice
|
|
// for (Practice& p : practices) {
|
|
|
|
// // get the values for the neural-net-input
|
|
|
|
// K::Interpolator<uint64_t, SensorGyro> interpol = p.getInterpol();
|
|
|
|
// line[0].clear();
|
|
// line[1].clear();
|
|
// line[2].clear();
|
|
|
|
// for (int i = 0; i < NUM_OUT; ++i) {
|
|
// netLines[i].clear();
|
|
// }
|
|
|
|
// for (int ms = 0; ms < 20000; ms += 50) {
|
|
// SensorGyro s = interpol.get(ms);
|
|
// line[0].add(K::GnuplotPoint2(ms, s.x));
|
|
// line[1].add(K::GnuplotPoint2(ms, s.y));
|
|
// line[2].add(K::GnuplotPoint2(ms, s.z));
|
|
// }
|
|
|
|
// // process every (positive) occurence within the practice
|
|
// for (int ts = 1000; ts < 10000; ts += 50) {
|
|
|
|
// std::vector<float> values = getNetworkInput(interpol, ts);
|
|
// K::NeuralNetResultIHO<NUM_OUT> res = net.getOutput(values.data());
|
|
// debug(p, res);
|
|
|
|
// for (int i = 0; i < NUM_OUT; ++i) {
|
|
// netLines[i].add(K::GnuplotPoint2(ts, res.values[i]));
|
|
// }
|
|
|
|
// gp1 << "set arrow 1 from " << ts-500 << ",-10 to " << ts-500 << ",+10\n";
|
|
// gp1 << "set arrow 2 from " << ts+500 << ",-10 to " << ts+500 << ",+10\n";
|
|
// gp1.draw(plot1);
|
|
// gp1.flush();
|
|
|
|
// gp2.draw(plot2);
|
|
// gp2.flush();
|
|
|
|
// usleep(1000*33);
|
|
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
//// K::Gnuplot gp;
|
|
//// K::GnuplotPlot plot;
|
|
//// K::GnuplotPlotElementLines line[3];
|
|
//// line[0].setColorHex("#ff0000"); line[0].setTitle("x");
|
|
//// line[1].setColorHex("#00ff00"); line[1].setTitle("y");
|
|
//// line[2].setColorHex("#0000ff"); line[2].setTitle("z");
|
|
|
|
//// Practice p1 = practices[0];
|
|
|
|
//// auto interpol = p1.getInterpol();
|
|
//// for (int ms = 0; ms < 20000; ms += 50) {
|
|
//// SensorGyro s = blur(interpol, ms, 10);
|
|
//// line[0].add(K::GnuplotPoint2(ms, s.x));
|
|
//// line[1].add(K::GnuplotPoint2(ms, s.y));
|
|
//// line[2].add(K::GnuplotPoint2(ms, s.z));
|
|
//// }
|
|
|
|
//// plot.add(&line[0]);
|
|
//// plot.add(&line[1]);
|
|
//// plot.add(&line[2]);
|
|
//// gp.draw(plot);
|
|
//// for (uint64_t ts : p1.keyGyro) {
|
|
//// gp << "set arrow from " << ts << ",-10 to " << ts << ",+10\n";
|
|
//// }
|
|
//// gp.flush();
|
|
|
|
|
|
// sleep(1000);
|
|
|
|
//}
|