コード例 #1
0
ファイル: pop03.cpp プロジェクト: simudream/censo
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;
}
コード例 #2
0
ファイル: pop10.cpp プロジェクト: simudream/censo
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;
}
コード例 #3
0
ファイル: pop06.cpp プロジェクト: bgrimstad/censo
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;
}
コード例 #4
0
ファイル: pop01.cpp プロジェクト: bgrimstad/censo
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;
}