Пример #1
0
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;
}
Пример #2
0
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;
}