void POP03::runProblem() { // Problem 3 (Nataraj) // cout << "\n\nSolving problem P03..." << endl; int dim = 4; // x1,x2,l1,l2 std::vector<double> costs = {1, 0, 0, 0}; std::vector<double> lb = {-10,-10,0,-1000}; std::vector<double> ub = {10,10,100,1000}; std::vector<double> z0(dim,0); std::vector<VariablePtr> vars; for (int i = 0; i < dim; i++) { auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i)); vars.push_back(var); } ConstraintSetPtr cs = std::make_shared<ConstraintSet>(); { // x1^2 = l1 VariablePtr var = vars.at(0); std::vector<double> thislb = {var->getLowerBound()}; std::vector<double> thisub = {var->getUpperBound()}; std::vector<unsigned int> deg = {2}; DenseVector c(3); c.setZero(); c(0) = 0; c(1) = 0; c(2) = 1; DenseMatrix T = getTransformationMatrix(deg, thislb, thisub); DenseMatrix coeffs = T*c; std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub); BSpline bs(coeffs.transpose(), knots, deg); std::vector<VariablePtr> cvars = {var, vars.at(2)}; ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true); cs->add(cbs); } { // x1^3 = l1 VariablePtr var = vars.at(0); std::vector<double> thislb = {var->getLowerBound()}; std::vector<double> thisub = {var->getUpperBound()}; std::vector<unsigned int> deg = {3}; DenseVector c(4); c.setZero(); c(0) = 0; c(1) = 0; c(2) = 0; c(3) = 1; DenseMatrix T = getTransformationMatrix(deg, thislb, thisub); DenseMatrix coeffs = T*c; std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub); BSpline bs(coeffs.transpose(), knots, deg); std::vector<VariablePtr> cvars = {var, vars.at(3)}; ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true); cs->add(cbs); } { // x2 - l1 <= 0, x2 - l2 <= 0 DenseMatrix A(2,3); A.setZero(); A(0,0) = -1; A(0,1) = 1; A(1,0) = 1; A(1,1) = 2; A(1,2) = -1; DenseVector b; b.setZero(2); b(1) = -1e-5; std::vector<VariablePtr> cvars = { vars.at(1), vars.at(2), vars.at(3) }; ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false); cs->add(lincon); } BB::BranchAndBound bnb(cs); Timer timer; timer.start(); SolverResult res = bnb.optimize(); timer.stop(); cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl; if (res.status == SolverStatus::OPTIMAL) fopt_found = res.objectiveValue; }
void POP06::runProblem() { // Problem 6 (Nataraj) // 5 2-D B-splines int dim = 4+2; // x1,x2,x3,x4,l1,l2 std::vector<double> costs = {0, 0, 0, 0, 1, 0}; std::vector<double> lb = {1,0.625,47.5,90,-INF,-INF}; std::vector<double> ub = {1.1375,1,52.5,112,INF,INF}; std::vector<double> z0(dim,0); // x1,x2,x3,x4,l1,l2,l3,l4,l5 // std::vector<double> lb = {1,0.625,47.5,90,-INF,-INF,-INF,-INF,-INF}; // std::vector<double> ub = {1.1375,1,52.5,112,INF,INF,INF,INF,INF}; std::vector<VariablePtr> vars; for (int i = 0; i < dim; i++) { auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i)); vars.push_back(var); } ConstraintSetPtr cs = std::make_shared<ConstraintSet>(); { // obj = l1 std::vector<VariablePtr> cvars = { vars.at(0), vars.at(1), vars.at(2), vars.at(3), vars.at(4) }; std::vector<double> thislb = { cvars.at(0)->getLowerBound(), cvars.at(1)->getLowerBound(), cvars.at(2)->getLowerBound(), cvars.at(3)->getLowerBound() }; std::vector<double> thisub = { cvars.at(0)->getUpperBound(), cvars.at(1)->getUpperBound(), cvars.at(2)->getUpperBound(), cvars.at(3)->getUpperBound() }; std::vector<unsigned int> deg = {2,1,2,1}; // Poly coeffs DenseVector c(4); c.setZero(); c(0) = 0.6224; c(1) = 1.7781; c(2) = 3.1661; c(3) = 19.84; // Poly exponents DenseMatrix E(4,4); E.setZero(); E(0,2) = 1; E(0,3) = 1; E(1,1) = 1; E(1,2) = 2; E(2,0) = 2; E(2,3) = 1; E(3,0) = 2; E(3,2) = 1; DenseMatrix coeffs = getBSplineBasisCoefficients(c, E, thislb, thisub); std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub); BSpline bs(coeffs, knots, deg); ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true); cs->add(cbs); //DenseMatrix cpoints = bs.getControlPoints(); //cout << cpoints << endl; } { // noncon = l2 std::vector<VariablePtr> cvars = { vars.at(2), vars.at(3), vars.at(5) }; std::vector<double> thislb = { cvars.at(0)->getLowerBound(), cvars.at(1)->getLowerBound() }; std::vector<double> thisub = { cvars.at(0)->getUpperBound(), cvars.at(1)->getUpperBound() }; std::vector<unsigned int> deg = {3,1}; // Poly coeffs DenseVector c(2); c.setZero(); c(0) = -1; c(1) = -(4.0/3.0); // Poly exponents DenseMatrix E(2,2); E.setZero(); E(0,0) = 2; E(0,1) = 1; E(1,0) = 3; DenseMatrix coeffs = getBSplineBasisCoefficients(c, E, thislb, thisub); std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub); BSpline bs(coeffs, knots, deg); ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true); cs->add(cbs); } { // Linear constraints of auxiliary variables DenseMatrix A(4,6); A.setZero(); A(0,0) = -1; A(0,2) = 0.0193; A(1,1) = -1; A(1,2) = 0.00954; A(2,5) = 1; A(3,3) = 1; DenseVector b; b.setZero(4); b(2) = -750.1728/3.14159265359; b(3) = 240; ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false); cs->add(lincon); } BB::BranchAndBound bnb(cs); Timer timer; timer.start(); SolverResult res = bnb.optimize(); timer.stop(); cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl; cout << "Time: " << timer.getMicroSeconds() << " (us)" << endl; //1 0.625 47.5 90 6395.50783 -345958.333 if (res.status == SolverStatus::OPTIMAL) fopt_found = res.objectiveValue; }
void run_test(Cost_Map &map, Point &start, Point &goal, vector<int> ×) { struct timeval pre; struct timeval post; /// printf("UCS\n"); UCS ucs(&map, start, goal); gettimeofday(&pre, NULL); Path path = ucs.search(); gettimeofday(&post, NULL); double reference_cost = path.length; check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Manhattan Manhattan(&map, start, goal); gettimeofday(&pre, NULL); path = Manhattan.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Euclidean euclidean(&map, start, goal); gettimeofday(&pre, NULL); path = euclidean.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("A*\n"); Octile octile(&map, start, goal); gettimeofday(&pre, NULL); path = octile.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("Coarse single\n"); CUCS_Heuristic cucs(&map, start, goal); gettimeofday(&pre, NULL); path = cucs.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("BB\n"); Boundaries_Blocking bb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = bb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Boundaries_Blocking bb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = bb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("BN\n"); Boundaries_NonBlocking bnb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = bnb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Boundaries_NonBlocking bnb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = bnb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("CB\n"); Corners_Blocking cb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = cb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Corners_Blocking cb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = cb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); /// printf("CN\n"); Corners_NonBlocking cnb2(&map, start, goal, 2, xscale, yscale); gettimeofday(&pre, NULL); path = cnb2.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); Corners_NonBlocking cnb(&map, start, goal, levels, xscale, yscale); gettimeofday(&pre, NULL); path = cnb.search(); gettimeofday(&post, NULL); check_and_update(start, goal, pre, post, reference_cost, path.length, times); }
void POP10::runProblem() { // Problem 10 (Nataraj), QP // Five 1-D B-splines cout << "\n\nSolving problem P10..." << endl; int dim = 6+5; // x1,..,x5,y,l1,...,l5 // x1,x2,x3,x4,l1,l2,l3 std::vector<double> costs = {0,0,0,0,0,-10,1,1,1,1,1}; std::vector<double> lb = {0,0,0,0,0,0,-INF,-INF,-INF,-INF,-INF}; std::vector<double> ub = {1,1,1,1,1,INF,INF,INF,INF,INF,INF}; std::vector<double> z0(dim,0); std::vector<VariablePtr> vars; for (int i = 0; i < dim; i++) { auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i)); vars.push_back(var); } ConstraintSetPtr cs = std::make_shared<ConstraintSet>(); std::vector<double> a = {-10.5,-7.5,-3.5,-2.5,-1.5}; // Add one B-spline for each variable for (int i = 0; i < 5; i++) { std::vector<VariablePtr> cvars = { vars.at(i), vars.at(i+6) }; std::vector<double> thislb = {cvars.at(0)->getLowerBound()}; std::vector<double> thisub = {cvars.at(0)->getUpperBound()}; std::vector<unsigned int> deg = {2}; // Poly coeffs DenseVector c(3); c.setZero(); c(0) = 0; c(1) = a.at(i); c(2) = -0.5; // -50 or -0.5 (Floudas' problem has -50) DenseMatrix T = getTransformationMatrix(deg, thislb, thisub); DenseMatrix coeffs = T*c; std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub); BSpline bs(coeffs.transpose(), knots, deg); ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true); cs->add(cbs); } { // Linear constraints std::vector<VariablePtr> cvars = { vars.at(0), vars.at(1), vars.at(2), vars.at(3), vars.at(4), vars.at(5) }; DenseMatrix A = DenseMatrix::Zero(2,6); A(0,0) = 6; A(0,1) = 3; A(0,2) = 3; A(0,3) = 2; A(0,4) = 1; A(1,0) = 10; A(1,2) = 10; A(1,5) = 1; DenseVector b; b.setZero(2); b(0) = 6.5; b(1) = 20; ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false); cs->add(lincon); } BB::BranchAndBound bnb(cs); Timer timer; timer.start(); SolverResult res = bnb.optimize(); timer.stop(); cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl; cout << "Time: " << timer.getMicroSeconds() << " (us)" << endl; if (res.status == SolverStatus::OPTIMAL) fopt_found = res.objectiveValue; }