void checkRawAccess() { Printf("Raw access+++++++++++++++++++++++++++\n"); VD data = { 0, 1, 1, 2, 3, 2, 1, 3, 2, 4, 2, 2}; Matrix m(3, data); print(m); double expected, test; size_t rows = data.size() / 3; // check read for (int i = 0; i < rows; i++) { for (int j = 0; j < 3; j++) { expected = data[i * 3 + j]; test = m(i, j); unitpp::assert_eq(spf("Expected value not equal to readen at row: %i, col: %i", i, j), test, expected); } } // check write int val = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < 3; j++) { m(i, j) = val++; } } VD testVector; m.rowPackedCopy(testVector); for (int i = 0; i < testVector.size(); i++) { test = testVector[i]; unitpp::assert_eq(spf("Value found: %i, but was expected: %i", test, i), test, i); } }
double doExec() { if (!loadTestData()) { std::cerr << "Failed to load test data" << std::endl; return -1; } // // Start solution testing // double score = 0, sse; int scenario = 0; for (int i = 0; i < subsetsNum; i++) { scenario = i % 3; generateTestData(scenario, i); VD res = test->predict(0, scenario, DTrain, DTest); Assert(groundTruth.size() == res.size(), "Expected results count not equal to found. Expected: %lu, but found: %lu", groundTruth.size(), res.size()); sse = 0; for (int j = 0; j < res.size(); j++) { double e = res[j] - groundTruth[j]; sse += e * e; } // calculate score double s = 1000000 * fmax(0, 1.0 - sse/sse0); Printf("%i.) Score = %f, sse: %f, sse0: %f\n", i, s, sse, sse0); score += s; } return score / subsetsNum; }
int main() { const int m = 4; const int n = 3; DOUBLE _A[m][n] = { { 6, -1, 0 }, { -1, -5, 0 }, { 1, 5, 1 }, { -1, -5, -1 } }; DOUBLE _b[m] = { 10, -4, 5, -5 }; DOUBLE _c[n] = { 1, -1, 0 }; VVD A(m); VD b(_b, _b + m); VD c(_c, _c + n); for (int i = 0; i < m; i++) A[i] = VD(_A[i], _A[i] + n); LPSolver solver(A, b, c); VD x; DOUBLE value = solver.Solve(x); cerr << "VALUE: "<< value << endl; cerr << "SOLUTION:"; for (size_t i = 0; i < x.size(); i++) cerr << " " << x[i]; cerr << endl; return 0; }
LPSolver(const VVD &A, const VD &b, const VD &c) : m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, VD(n + 2)) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = A[i][j]; for (int i = 0; i < m; i++) { B[i] = n + i; D[i][n] = -1; D[i][n + 1] = b[i]; } for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; } N[n] = -1; D[m + 1][n] = 1; }
void MLComputeTest::dotProduct_benchmark_vector() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { MLCompute::dotProduct(data, data); } }
void MLComputeTest::correlation_benchmark_parallel() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { correlation_parallel(data, data); } }
void MLComputeTest::correlation_benchmark_alternative() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { correlation_helper(data, data); } }
void MLComputeTest::stdev_benchmark() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { MLCompute::stdev(data); } }
LPSolver(const VD &b, const VD &c) : m(b.size()), n(c.size()), N(n+1), B(m), D(m+2, VD(n+2)) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = AA[i][j]; for (int i = 0; i < m; i++) { B[i] = n+i; D[i][n] = -1; D[i][n+1] = b[i]; } for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; } N[n] = -1; D[m+1][n] = 1; /*for (int i = 0; i < m+2; i++) { for (int j = 0; j < n+2; j++){ cout<<D[i][j]<<" ";} cout<<endl; }*/ }
int main() { int n; cin >> n; VD arr(n+1); for (int i = 0; i <= n; i++) cin >> arr[i]; VD ans = roots(poly(arr)); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) printf("%.4f\n", ans[i]); return 0; }
void MLComputeTest::min_benchmark() { VD data; data.reserve(10000); for (int i = 0; i < 10000; ++i) { data.append(-10000 + qrand()); } QBENCHMARK { MLCompute::min(data.size(), data.data()); } }
VD generateVoronoi(int ** setPoints, int numberofPoints){ VD vd; int i = 0; for(i = 0; i < numberofPoints * 3; i+=3){ Site_2 p(Point_2((*setPoints)[i], (*setPoints)[i + 1])); vd.insert(p); } return vd; }
void set(VVD & A, VD & B, VD & C) { n = C.size(); m = A.size(); left.resize(m); up.resize(n); pos.resize(n); res.resize(n); status = -2; v = 0; a = A; b = B; c = C; }
void solve() { for (int i = 0; i < n; i++) up[i] = i; for (int i = 0; i < m; i++) left[i] = i + n; while (1) { int x = -1; for (int i = 0; i < m; i++) if (lt(b[i], 0) && (x == -1 || b[i] < b[x])) { x = i; } if (x == -1) break; int y = -1; for (int j = 0; j < n; j++) if (lt(a[x][j], 0)) { y = j; break; } if (y == -1) { status = -1; return; assert(false); // no solution } pivot(x, y); } while (1) { int y = -1; for (int i = 0; i < n; i++) if (lt(0, c[i]) && (y == -1 || (c[i] > c[y]))) { y = i; } if (y == -1) break; int x = -1; for (int i = 0; i < m; i++) { if (lt(0, a[i][y])) { if (x == -1 || (b[i] / a[i][y] < b[x] / a[x][y])) { x = i; } } } if (y == -1) { status = 1; return; assert(false); // infinite solution } pivot(x, y); } res.assign(n, 0); for (int i = 0; i < m; i++) { if (left[i] < n) { res[left[i]] = b[i]; } } status = 0; }
VD roots(const poly &p) { if (p.is_constant()) return VD(0); VD critical_values = roots(p.derivative()); critical_values.push_back(1e6); VD ans; double lower = -1e6; for (int i = 0; i < critical_values.size(); i++) { double upper = critical_values[i]; if (sgn(p.eval(lower)) != sgn(p.eval(upper))) ans.push_back(bisect(p, lower, upper)); lower = upper; } return ans; }
double eval(double x) const { double ans = 0; for (int i = coeff.size() - 1; i >= 0; i--) { ans *= x; ans += coeff[i]; } return ans; }
void init(VD & t, int & n, PDD * p, PDD & a, PDD & b) { t.clear(); double tmp; for (int i = 0; i < n; ++i) { scanf("%lf", &tmp); t.PB(tmp); } std::sort(t.begin(), t.end()); t.erase(std::unique(t.begin(), t.end()), t.end()); n = t.size(); for (int i = 0; i < n; ++i) { p[i] = at(a, b, t[i]); } }
virtual QStringList formatFromPixelCoordinate( const VD & pix ) override { CARTA_ASSERT( pix.size() >= 2 ); QStringList res; res.append( QString::number( pix[0] ) ); res.append( QString::number( pix[1] ) ); return res; }
DotNode* GraphKMeans::getNextMean(const vector<DotNode*>& means, ConnectedDotGraph& g) { VD minDist; for (int i = 0; i < (int)g.nodes.size(); i++) { double minD = 123456789.0; for (int j = 0; j < (int)means.size(); j++) { double d = g.getShortestPath(g.nodes[i], means[j], true); minD = min(minD, d); } minDist.push_back(Sqr2(minD)); } int p = randWithProbability(minDist); if (minDist[p] < 1e-6) return NULL; return g.nodes[p]; }
void MLComputeTest::dotProduct_benchmark_arrays() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { MLCompute::dotProduct(data.size(), data.data(), data.data()); } }
void MLComputeTest::dotProduct_benchmark_inner_product() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { std::inner_product(data.constBegin(), data.constEnd(), data.constBegin(), 0); } }
void MLComputeTest::mean_benchmark_alternative() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { (void)(std::accumulate(data.constBegin(), data.constEnd(), 0) / data.size()); } }
double stdev_helper(const VD& data) { double sumsqr = std::inner_product(data.constBegin(), data.constEnd(), data.constBegin(), 0); double sum = std::accumulate(data.constBegin(), data.constEnd(), 0); const int ct = data.size(); return sqrt((sumsqr - sum * sum / ct) / (ct - 1)); }
double correlation_parallel(const VD& data1, const VD& data2) { if (data1.size() != data2.size()) return 0; std::promise<corr_intermediate> sum1_promise; std::future<corr_intermediate> sum1_future = sum1_promise.get_future(); std::thread sum1_thread( correlation_parallel_helper, data1.begin(), data1.end(), std::move(sum1_promise)); std::promise<corr_intermediate> sum2_promise; std::future<corr_intermediate> sum2_future = sum2_promise.get_future(); std::thread sum2_thread( correlation_parallel_helper, data2.begin(), data2.end(), std::move(sum2_promise)); corr_intermediate itm1 = std::move(sum1_future.get()); corr_intermediate itm2 = std::move(sum2_future.get()); sum1_thread.join(); sum2_thread.join(); if (!itm1.stdev || !itm2.stdev) return 0; return std::inner_product(itm1.Y.constBegin(), itm1.Y.constEnd(), itm2.Y.constBegin(), 0); }
vector<Item> fixItems(vector<Item>& curItems, int curRoadId) { vector<Item> goodItems; VD sps; int curTime = -1; for (int i = 0; i < (int)curItems.size(); i++) { if ( curItems[i].time == curTime ) { sps.push_back(curItems[i].speed); } else { if ( !sps.empty() ) goodItems.push_back(Item(curTime, MedianValue(sps))); sps.clear(); sps.push_back(curItems[i].speed); curTime = curItems[i].time; } } if ( !sps.empty() ) goodItems.push_back(Item(curTime, MedianValue(sps))); return goodItems; }
int main(){ int n; while(cin >> n and n){ vector<point> myGoats(n); for(int i = 0; i < n; ++i){ double x,y; cin >> x >> y; myGoats[i] = (point(x,y)); } map< int, pair<int,int> > At; VD b; VD c (n,1); int cont = 0; for(int i = 0; i < n; ++i){ for(int j = i + 1; j < n; ++j){ b.push_back(myGoats[i].dist2point(myGoats[j])); At[cont].first = i; At[cont].second = j; cont += 1; } } for(int i = 0; i < b.size(); ++i) for(int j = 0; j < n; ++j) AA[i][j] = 0; for(int i = 0; i < b.size(); ++i){ AA[i][At[i].first] = 1; AA[i][At[i].second] = 1; } VD x; LPSolver mySolver = LPSolver( b, c); double d = mySolver.Solve(x); //double acum = 0.0; //for(int i = 0; i < n; ++i) acum += x[i]; printf("%.2f\n", d); } return 0; }
void MLComputeTest::sum_benchmark_accumulate() { VD data; data.resize(10000); std::iota(data.begin(), data.end(), 1); QBENCHMARK { std::accumulate(data.constBegin(), data.constEnd(), 0); } }
MotionPlannerGraph* MotionPlanner::init_graph(int island_idx) { if (this->graphs[island_idx + 1] == NULL) { Polygons pp; if (island_idx == -1) { pp = this->outer; } else { pp = this->inner[island_idx]; } MotionPlannerGraph* graph = this->graphs[island_idx + 1] = new MotionPlannerGraph(); // add polygon boundaries as edges size_t node_idx = 0; Lines lines; for (Polygons::const_iterator polygon = pp.begin(); polygon != pp.end(); ++polygon) { graph->nodes.push_back(polygon->points.back()); node_idx++; for (Points::const_iterator p = polygon->points.begin(); p != polygon->points.end(); ++p) { graph->nodes.push_back(*p); double dist = graph->nodes[node_idx-1].distance_to(*p); graph->add_edge(node_idx-1, node_idx, dist); graph->add_edge(node_idx, node_idx-1, dist); node_idx++; } polygon->lines(&lines); } // add Voronoi edges as internal edges { typedef voronoi_diagram<double> VD; typedef std::map<const VD::vertex_type*,size_t> t_vd_vertices; VD vd; t_vd_vertices vd_vertices; boost::polygon::construct_voronoi(lines.begin(), lines.end(), &vd); for (VD::const_edge_iterator edge = vd.edges().begin(); edge != vd.edges().end(); ++edge) { if (edge->is_infinite()) continue; const VD::vertex_type* v0 = edge->vertex0(); const VD::vertex_type* v1 = edge->vertex1(); Point p0 = Point(v0->x(), v0->y()); Point p1 = Point(v1->x(), v1->y()); // contains() should probably be faster than contains(), // and should it fail on any boundary points it's not a big problem if (island_idx == -1) { if (!this->outer.contains(p0) || !this->outer.contains(p1)) continue; } else { if (!this->inner[island_idx].contains(p0) || !this->inner[island_idx].contains(p1)) continue; } t_vd_vertices::const_iterator i_v0 = vd_vertices.find(v0); size_t v0_idx; if (i_v0 == vd_vertices.end()) { graph->nodes.push_back(p0); v0_idx = node_idx; vd_vertices[v0] = node_idx; node_idx++; } else { v0_idx = i_v0->second; } t_vd_vertices::const_iterator i_v1 = vd_vertices.find(v1); size_t v1_idx; if (i_v1 == vd_vertices.end()) { graph->nodes.push_back(p1); v1_idx = node_idx; vd_vertices[v1] = node_idx; node_idx++; } else { v1_idx = i_v1->second; } double dist = graph->nodes[v0_idx].distance_to(graph->nodes[v1_idx]); graph->add_edge(v0_idx, v1_idx, dist); } } return graph; } return this->graphs[island_idx + 1]; }
MotionPlannerGraph* MotionPlanner::init_graph(int island_idx) { if (this->graphs[island_idx + 1] == NULL) { // if this graph doesn't exist, initialize it MotionPlannerGraph* graph = this->graphs[island_idx + 1] = new MotionPlannerGraph(); /* We don't add polygon boundaries as graph edges, because we'd need to connect them to the Voronoi-generated edges by recognizing coinciding nodes. */ typedef voronoi_diagram<double> VD; VD vd; // mapping between Voronoi vertices and graph nodes typedef std::map<const VD::vertex_type*,size_t> t_vd_vertices; t_vd_vertices vd_vertices; // get boundaries as lines ExPolygonCollection env = this->get_env(island_idx); Lines lines = env.lines(); boost::polygon::construct_voronoi(lines.begin(), lines.end(), &vd); // traverse the Voronoi diagram and generate graph nodes and edges for (VD::const_edge_iterator edge = vd.edges().begin(); edge != vd.edges().end(); ++edge) { if (edge->is_infinite()) continue; const VD::vertex_type* v0 = edge->vertex0(); const VD::vertex_type* v1 = edge->vertex1(); Point p0 = Point(v0->x(), v0->y()); Point p1 = Point(v1->x(), v1->y()); // skip edge if any of its endpoints is outside our configuration space if (!env.contains_b(p0) || !env.contains_b(p1)) continue; t_vd_vertices::const_iterator i_v0 = vd_vertices.find(v0); size_t v0_idx; if (i_v0 == vd_vertices.end()) { graph->nodes.push_back(p0); vd_vertices[v0] = v0_idx = graph->nodes.size()-1; } else { v0_idx = i_v0->second; } t_vd_vertices::const_iterator i_v1 = vd_vertices.find(v1); size_t v1_idx; if (i_v1 == vd_vertices.end()) { graph->nodes.push_back(p1); vd_vertices[v1] = v1_idx = graph->nodes.size()-1; } else { v1_idx = i_v1->second; } // Euclidean distance is used as weight for the graph edge double dist = graph->nodes[v0_idx].distance_to(graph->nodes[v1_idx]); graph->add_edge(v0_idx, v1_idx, dist); } return graph; } return this->graphs[island_idx + 1]; }
int main() { int n; scanf("%d", &n); int cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%d", g[i]+j), to[i][j] = j, di[i][j] = g[i][j]; if (g[i][j] > 0) { id[i][j] = cnt++; } } } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (g[i][k] < 0 || g[k][j] < 0) continue; if (g[i][j] < 0 || g[i][j] > g[i][k] + g[k][j]) { g[i][j] = g[i][k] + g[k][j]; to[i][j] = to[i][k]; } } } } VVD A; VD B; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (di[i][j] <= 0) continue; VD cur(cnt, 0); cur[id[i][j]] = 1; A.push_back(cur); B.push_back(1 * di[i][j]); } } int r; scanf("%d", &r); for (int i = 0; i < r; ++i) { int s, d, t; scanf("%d %d %d", &s, &d, &t); t -= g[s][d]; VD cur(cnt, 0); while (s != d) { int v = to[s][d]; cur[id[s][v]] = 1; s = v; } A.push_back(cur); B.push_back(t + EPS); for (int j = 0; j < cnt; ++j) cur[j] *= -1; A.push_back(cur); B.push_back(-t + EPS); } int q; scanf("%d", &q); simplex solver; for (int i = 0; i < q; ++i) { int s, d; scanf("%d %d", &s, &d); LD low = g[s][d], up = g[s][d] * 2; if (g[s][d] != 0) { VD x(cnt), c(cnt); int now = s; while (now != d) { int v = to[now][d]; c[id[now][v]] = 1; now = v; } solver.set(A, B, c); solver.solve(); if (solver.status == 0) up = solver.v + g[s][d]; for (int j = 0; j < cnt; ++j) c[j] *= -1; solver.set(A, B, c); solver.solve(); if (solver.status == 0) low = -solver.v + g[s][d]; } printf("%d %d %.20lf %.20lf\n", s, d, (double)low, (double)up); } return 0; }