Exemplo n.º 1
0
void testConvexRelaxations()
{
    DenseMatrix A(1,3);
    A << 1, 2, 3;

    DenseVector b(1);
    b(0) = 1;

    std::vector<VariablePtr> vars =
    {
        std::make_shared<Variable>(1,0,6),
        std::make_shared<Variable>(2,0,4),
        std::make_shared<Variable>(3,0,2)
    };

    ConstraintPtr lincon1 = std::make_shared<ConstraintLinear>(vars, A, b, true);

    A << 3, 2, 1;
    ConstraintPtr lincon2 = std::make_shared<ConstraintLinear>(vars, A, b, true);

    SPLINTER::DataTable data;
    data.addSample(0,0);
    data.addSample(0.5,0.5);
    data.addSample(1,1);

    SPLINTER::BSpline bs(data, SPLINTER::BSplineType::LINEAR);
    auto bsvars = {vars.at(0), vars.at(1)};
    ConstraintPtr bscon = std::make_shared<ConstraintBSpline>(bsvars, bs, true);

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();
    cs->add(lincon1);
    cs->add(lincon2);
    cs->add(bscon);

    cout << cs->getVariables() << endl;

    cout << "Relaxed" << endl;

    ConstraintPtr csr = cs->getConvexRelaxation();

    cout << csr->getVariables() << endl;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
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;

}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
void testNewConstraintClasses()
{
    // Create some vars
    std::vector<VariablePtr> vars;

    for (int i = 0; i < 3; i++)
    {
        auto var = std::make_shared<Variable>(1, 0, i+1);
        vars.push_back(var);
        cout << *var << endl;
    }

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();

    DenseMatrix A = DenseMatrix::Zero(3,3);
    DenseVector b = DenseVector::Zero(3);
    b(0) = 0.5;

    A(0,0) = 1;
    ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, true);

    A(1,1) = 2;
    ConstraintPtr lincon2 = std::make_shared<ConstraintLinear>(vars, A, b, true);

    A(2,2) = 3;
    ConstraintPtr lincon3 = std::make_shared<ConstraintLinear>(vars, A, b, true);

    cs->add(lincon);
    //cs->add(lincon2); // TODO: continue here tomorrow!
    //cs->add(lincon3);

    ConstraintSetPtr cs2 = std::make_shared<ConstraintSet>(*cs); // Shallow copy

    ConstraintSetPtr cs4 = std::make_shared<ConstraintSet>();
    cs4->add(cs); // Shallow copy

    ConstraintSetPtr cs5 = std::make_shared<ConstraintSet>(*cs, true); // Deep copy

    DenseVector x = DenseVector::Zero(3);
    x(0) = 1;
    x(1) = 1;
    x(2) = 1;

    DenseVector y;
    DenseVector dy;
    DenseVector ddy;

    {
        cout << "Evaluating cs" << endl;

        y = cs->eval(x);
        dy = cs->evalJacobian(x);
        ddy = cs->evalHessian(x);
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;
    }

    ConstraintPtr cs3 = cs->clone(); // Deep copy

    {
        cout << "Evaluating cs without x" << endl;

        std::vector<VariablePtr> vars = cs->getVariables();
        vars.at(0)->setValue(2);

        y = cs->eval();
        dy = cs->evalJacobian();
        ddy = cs->evalHessian();
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;
    }

    {
        cout << "Evaluating cs2 (shallow copy)." << endl;

        y = cs2->eval();
        dy = cs2->evalJacobian();
        ddy = cs2->evalHessian();
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;
    }

    {
        cout << "Evaluating cs3 (deep copy)." << endl;

        y = cs3->eval();
        dy = cs3->evalJacobian();
        ddy = cs3->evalHessian();
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;
    }

    {
        cout << "Evaluating cs4 (shallow composite)." << endl;

        y = cs4->eval();
        dy = cs4->evalJacobian();
        ddy = cs4->evalHessian();
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;

    }

    {
        cout << "Evaluating cs5 (deep copy)." << endl;

        y = cs5->eval();
        dy = cs5->evalJacobian();
        ddy = cs5->evalHessian();
        cout << "y:" << endl;
        cout << y << endl;
        cout << "dy" << endl;
        cout << dy << endl;
        cout << "ddy" << endl;
        cout << ddy << endl;
    }
}
Exemplo n.º 7
0
void LP02::runProblem()
{
    int numVars = 12;
    std::vector<VariablePtr> variables;
    std::vector<double> costs = {20, 40, 35, 120, 50, 60, 20, 70, 90, 35, 70, 40};
    for (int i = 0; i < numVars; i++)
    {
        auto var = std::make_shared<Variable>(costs.at(i), 0, INF);
        variables.push_back(var);
    }

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();

    {
        std::vector<VariablePtr> vars = {variables.at(0),
                                         variables.at(1),
                                         variables.at(2),
                                         variables.at(3)};

        DenseMatrix A(1,4); A << 1, 1, 1, 1;
        DenseVector b(1);   b << 100;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(4),
                                         variables.at(5),
                                         variables.at(6),
                                         variables.at(7)};

        DenseMatrix A(1,4); A << 1, 1, 1, 1;
        DenseVector b(1);   b << 75;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(8),
                                         variables.at(9),
                                         variables.at(10),
                                         variables.at(11)};

        DenseMatrix A(1,4); A << 1, 1, 1, 1;
        DenseVector b(1);   b << 90;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(0),
                                         variables.at(4),
                                         variables.at(8)};

        DenseMatrix A(1,3); A << -1, -1, -1;
        DenseVector b(1);   b << -30;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(1),
                                         variables.at(5),
                                         variables.at(9)};

        DenseMatrix A(1,3); A << -1, -1, -1;
        DenseVector b(1);   b << -75;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(2),
                                         variables.at(6),
                                         variables.at(10)};

        DenseMatrix A(1,3); A << -1, -1, -1;
        DenseVector b(1);   b << -90;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    {
        std::vector<VariablePtr> vars = {variables.at(3),
                                         variables.at(7),
                                         variables.at(11)};

        DenseMatrix A(1,3);  A << -1, -1, -1;
        DenseVector b(1);   b << -50;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(vars, A, b, false);
        cs->add(lincon);
    }

    //cout << *cs << endl;

    SolverGurobi solver(cs);
    //SolverIpopt solver(cs);
    auto res = solver.optimize();
    cout << res << endl;

    fopt_found = res.objectiveValue;
}
Exemplo n.º 8
0
void POP01::runProblem()
{
    // Problem 1 (Nataraj)
    // cout << "\n\nSolving problem P01..." << endl;

    int dim = 4;

    // x1,x2,l1,l2
    std::vector<double> costs = {-1, -1, 0, 0};
    std::vector<double> lb = {0,0,-INF,-INF};
    std::vector<double> ub = {3,4,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>();

    { // 2*x1^4 - 8*x1^3 + 8*x1^2 + 2

        std::vector<VariablePtr> cvars = {vars.at(0), vars.at(2)};
        std::vector<double> thislb = {cvars.at(0)->getLowerBound()};
        std::vector<double> thisub = {cvars.at(0)->getUpperBound()};

        std::vector<unsigned int> deg = {4};

        DenseVector c(5);
        c.setZero();
        c(0) = 2;
        c(1) = 0;
        c(2) = 8;
        c(3) = -8;
        c(4) = 2;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        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);
    }

    { // 4*x1^4 - 32*x1^3 + 88*x1^2 - 96*x1 + 36

        std::vector<VariablePtr> cvars = {vars.at(0), vars.at(3)};
        std::vector<double> thislb = {cvars.at(0)->getLowerBound()};
        std::vector<double> thisub = {cvars.at(0)->getUpperBound()};

        std::vector<unsigned int> deg = {4};

        DenseVector c(5); c.setZero();
        c(0) = 36;
        c(1) = -96;
        c(2) = 88;
        c(3) = -32;
        c(4) = 4;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        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);
    }

    { // x2 - l1 <= 0, x2 - l2 <= 0

        std::vector<VariablePtr> cvars = {
            vars.at(1),
            vars.at(2),
            vars.at(3)
        };

        DenseMatrix A(2,3);
        A.setZero();
        A(0,0) = 1; A(0,1) = -1;
        A(1,0) = 1; A(1,2) = -1;

        DenseVector b; b.setZero(2);

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false);

        cs->add(lincon);
    }    

    BB::BranchAndBound solver(cs);
    Timer timer;
    timer.start();
    SolverResult res = solver.optimize();
    timer.stop();
    cout << res << endl;
    cout << res.primalVariables << endl;
    cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl;

    if (res.status == SolverStatus::OPTIMAL)
        fopt_found = res.objectiveValue;
}
Exemplo n.º 9
0
void Asaadi1::runProblem()
{
    int numVars = 5; // 4 + 1 epigraph variable

//    std::vector<VariableType> varTypes(numVars,VariableType::CONTINUOUS);

//    std::vector<VariableType> varTypes = {
//        VariableType::INTEGER,
//        VariableType::INTEGER,
//        VariableType::CONTINUOUS,
//        VariableType::INTEGER,
//        VariableType::CONTINUOUS
//    };

    std::vector<VariableType> varTypes = {
        VariableType::INTEGER,
        VariableType::INTEGER,
        VariableType::INTEGER,
        VariableType::INTEGER,
        VariableType::CONTINUOUS
    };

    std::vector<double> costs = {0, 0, 0, 0, 1};
    std::vector<double> lb = {0, 0, 0, 0, -INF};
    std::vector<double> lb_unbounded(numVars, -INF);
    std::vector<double> ub = {100, 100, 100, 100, INF};

    std::vector<VariablePtr> vars;

    for (int i = 0; i < numVars; i++)
    {
        auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i), varTypes.at(i));
        vars.push_back(var);
    }

    std::vector<VariablePtr> xvars = {
        vars.at(0),
        vars.at(1),
        vars.at(2),
        vars.at(3)
    };

    // Constraints
    ConstraintSetPtr constraints = std::make_shared<ConstraintSet>();

    // Constraint due to epigraph form
    DenseVector c0(9);
    c0.setZero();
    DenseMatrix C0(9,5);
    C0.setZero();

    c0(0) = 1; C0(0,0) = 2;
    c0(1) = 1; C0(1,1) = 2;
    c0(2) = 2; C0(2,2) = 2;
    c0(3) = 1; C0(3,3) = 2;
    c0(4) = -5; C0(4,0) = 1;
    c0(5) = -5; C0(5,1) = 1;
    c0(6) = -21; C0(6,2) = 1;
    c0(7) = 7; C0(7,3) = 1;
    c0(8) = -1; C0(8,4) = 1; // -t (epigraph)

    ConstraintPtr cPoly0 = std::make_shared<ConstraintPolynomial>(vars, c0, C0, false);
    constraints->add(cPoly0);

    DenseVector c1(9);
    c1.setZero();
    DenseMatrix C1(9,4);
    C1.setZero();

    c1(0) = -1; C1(0,0) = 2;
    c1(1) = -1; C1(1,1) = 2;
    c1(2) = -1; C1(2,2) = 2;
    c1(3) = -1; C1(3,3) = 2;
    c1(4) = -1; C1(4,0) = 1;
    c1(5) = 1; C1(5,1) = 1;
    c1(6) = -1; C1(6,2) = 1;
    c1(7) = 1; C1(7,3) = 1;
    c1(8) = 8; // -8 <= p(x) <=> 0 <= 8 + p(x)

    ConstraintPtr cPoly1 = std::make_shared<ConstraintPolynomial>(xvars, -c1, C1, false);
    constraints->add(cPoly1);

    DenseVector c2(7);
    c2.setZero();
    DenseMatrix C2(7,4);
    C2.setZero();

    c2(0) = -1; C2(0,0) = 2;
    c2(1) = -2; C2(1,1) = 2;
    c2(2) = -1; C2(2,2) = 2;
    c2(3) = -2; C2(3,3) = 2;
    c2(4) = 1; C2(4,0) = 1;
    c2(5) = 1; C2(5,3) = 1;
    c2(6) = 10; // -10 <= p(x)

    ConstraintPtr cPoly2 = std::make_shared<ConstraintPolynomial>(xvars, -c2, C2, false);
    constraints->add(cPoly2);

    DenseVector c3(7);
    c3.setZero();
    DenseMatrix C3(7,4);
    C3.setZero();

    c3(0) = -2; C3(0,0) = 2;
    c3(1) = -1; C3(1,1) = 2;
    c3(2) = -1; C3(2,2) = 2;
    c3(3) = -2; C3(3,0) = 1;
    c3(4) = 1; C3(4,1) = 1;
    c3(5) = 1; C3(5,3) = 1;
    c3(6) = 5; // -5 <= p(x)

    ConstraintPtr cPoly3 = std::make_shared<ConstraintPolynomial>(xvars, -c3, C3, false);
    constraints->add(cPoly3);

    // Problem 1: all variables are continuous and unbounded (not bounded from below)
    // f* = -44, x* = (0, 1, 2, -1)

    // Problem 2: all variables are continuous and non-negative
    // f* = -40.96, x* = (0, 1.038, 2.227, 0)

    // Problem 3: variables x1, x2, and x4 are non-negative integers, x3 is non-negative and continous
    // f* = -40.957, x* = (0, 1, 2.236, 0)

    // Problem 4: all variables are non-negative integers
    // f* = -38.000, x* = (0, 1, 2, 0)

    cout << constraints->isConstraintConvex() << endl;

    if (!constraints->isConstraintConvex())
        cout << "NOOOOOOOOOOOOOT CONVEEEEEEEEEEEEEEEX!!!!!!!!!!" << endl;
    else
        cout << "CONVEEEEEEEEEEEEEEEX!!!!!!!!!!" << endl;
    constraints->writeToGAMS("asaadi.gms");

    //SolverBonmin solver(constraints);
    BB::BranchAndBound solver(constraints);
    SolverResult res = solver.optimize();

    cout << res << endl;
    cout << res.primalVariables << endl;

    zopt_found = res.primalVariables;
    fopt_found = res.objectiveValue;
}