DataTable operator+(const DataTable &lhs, const DataTable &rhs) { if(lhs.getNumVariables() != rhs.getNumVariables()) { throw Exception("operator+(DataTable, DataTable): trying to add two DataTable's of different dimensions!"); } DataTable result; for(auto it = lhs.cbegin(); it != lhs.cend(); it++) { result.addSample(*it); } for(auto it = rhs.cbegin(); it != rhs.cend(); it++) { result.addSample(*it); } return result; }
void Michalewicz::runProblem() { double pi = atan(1)*4; std::vector<VariablePtr> vars = { std::make_shared<Variable>(0, 0, pi), std::make_shared<Variable>(0, 0, pi), std::make_shared<Variable>(1) }; // Set starting points vars.at(0)->setValue(1.0); vars.at(1)->setValue(1.0); DataTable data; double dx = 0.05; for (double x1 = 0; x1 <= pi; x1+=dx) { for (double x2 = 0; x2 <= pi; x2+=dx) { std::vector<double> x = {x1, x2}; DenseVector xd(2); xd << x1, x2; DenseVector yd = michalewiczFunction(xd); data.addSample(x,yd(0)); } } // Test accuracy of B-spline // DenseVector (*foo)(DenseVector); // foo = &michalewiczFunction; // BSpline* bs = new BSpline(*data, 3); // bool testpassed = bs->testBspline(foo); // if (testpassed) // { // cout << "B-spline is very accurate:)" << endl; // } // else // { // cout << "B-spline is NOT very accurate:(" << endl; // } BSpline bs(data, BSplineType::CUBIC); auto constraint = std::make_shared<ConstraintBSpline>(vars, bs, false); //SolverIpopt solver(constraint); BB::BranchAndBound solver(constraint); // Optimize SolverResult result = solver.optimize(); cout << result << endl; fopt_found = result.objectiveValue; zopt_found = result.primalVariables; cout << zopt_found << endl; }
void Michalewicz::runProblem() { double pi = atan(1)*4; std::vector<VariablePtr> vars = { std::make_shared<Variable>(0, 0, pi), std::make_shared<Variable>(0, 0, pi), std::make_shared<Variable>(1) }; // Set starting points vars.at(0)->setValue(1.0); vars.at(1)->setValue(1.0); DataTable data; unsigned int nums = 10; // 60x60 yields is sufficient to model function around optimum auto x1 = linspace(0, 4, nums); auto x2 = linspace(0, 4, nums); for (auto x1i : x1) { for (auto x2i : x2) { DenseVector xd(2); xd << x1i, x2i; double yd = michalewiczFunction(xd); data.addSample(xd, yd); } } // Test accuracy of B-spline // DenseVector (*foo)(DenseVector); // foo = &michalewiczFunction; // BSpline* bs = new BSpline(*data, 3); // bool testpassed = bs->testBspline(foo); // if (testpassed) // { // cout << "B-spline is very accurate:)" << endl; // } // else // { // cout << "B-spline is NOT very accurate:(" << endl; // } BSpline bs = BSpline::Builder(data).degree(3).build(); auto constraint = std::make_shared<ConstraintBSpline>(vars, bs, false); //SolverIpopt solver(constraint); BB::BranchAndBound solver(constraint); // Optimize SolverResult result = solver.optimize(); cout << result << endl; fopt_found = result.objectiveValue; zopt_found = result.primalVariables; cout << zopt_found << endl; }
DataTable operator-(const DataTable &lhs, const DataTable &rhs) { if(lhs.getNumVariables() != rhs.getNumVariables()) { throw Exception("operator-(DataTable, DataTable): trying to subtract two DataTable's of different dimensions!"); } DataTable result; auto rhsSamples = rhs.getSamples(); // Add all samples from lhs that are not in rhs for(auto it = lhs.cbegin(); it != lhs.cend(); it++) { if(rhsSamples.count(*it) == 0) { result.addSample(*it); } } return result; }
void BilinearRelaxationTest::runProblem() { std::vector<double> cost, lb, ub, z0; cost.push_back(0); cost.push_back(0); cost.push_back(1); lb.push_back(-1.); lb.push_back(-1.); lb.push_back(-INF); ub.push_back(1); ub.push_back(2.5); ub.push_back(INF); z0.push_back(1); z0.push_back(-0.5); z0.push_back(-0.5); std::vector<VariablePtr> vars; for (unsigned int i = 0; i < 3; i++) { auto var = std::make_shared<Variable>(cost.at(i), lb.at(i), ub.at(i)); var->setValue(z0.at(i)); vars.push_back(var); } // Constraints ConstraintSetPtr constraints = std::make_shared<ConstraintSet>(); ConstraintPtr myBilinearConstraint = std::make_shared<ConstraintBilinear>(vars,1,0,0); constraints->add(myBilinearConstraint); DataTable data; double dx = 0.5; for (double x1 = lb.at(0); x1 <= ub.at(0); x1+=dx) { for (double x2 = lb.at(1); x2 <= ub.at(1); x2+=dx) { std::vector<double> x; x.push_back(x1); x.push_back(x2); DenseVector xd; xd.setZero(2); xd(0) = x1; xd(1) = x2; DenseVector yd = bilinearFunction(xd); data.addSample(x,yd(0)); } } ConstraintSetPtr constraints2 = std::make_shared<ConstraintSet>(); BSpline bs = BSpline::Builder(data).degree(3).build(); ConstraintPtr cbspline = std::make_shared<ConstraintBSpline>(vars, bs, true); constraints2->add(cbspline); // Test accuracy of B-spline // DenseVector (*foo)(DenseVector); // foo = &bilinearFunction; // BSpline* bs = new BSpline(*data, 3); // bool testpassed = bs->testBspline(foo); // if (testpassed) // { // cout << "B-spline is very accurate:)" << endl; // } // else // { // cout << "B-spline is NOT very accurate:(" << endl; // } BB::BranchAndBound solver(constraints); //SolverIpopt solver(constraints); SolverResult res = solver.optimize(); cout << res << endl; zopt_found = res.primalVariables; fopt_found = res.objectiveValue; }