stanle / madopt

Apache License 2.0
16 stars 5 forks source link

Error compiling CPP examples #26

Open CIT2708 opened 2 years ago

CIT2708 commented 2 years ago

After compiling and installing madopt with the following instructions:

sudo git clone https://github.com/stanle/madopt.git
cd madopt
mkdir build
cd build
cmake -DCUSTOM_LIBRARY=/usr/local/lib/ -DCUSTOM_INCLUDE=/usr/local/include/ ..
sudo make install

I've tried to compile the get_started.cpp file in the examples folder but it has consistently failed with the same error:

g++ get_started.cpp -g -O2
/usr/bin/ld: /tmp/cceTAgTA.o: in function `main':
/mnt/c/madopt/examples/get_started.cpp:26: undefined reference to `MadOpt::IpoptModel::IpoptModel()'
/usr/bin/ld: /tmp/cceTAgTA.o: in function `void std::_Construct<MadOpt::Var>(MadOpt::Var*)':
/usr/include/c++/9/new:174: undefined reference to `MadOpt::Var::Var()'
/usr/bin/ld: /tmp/cceTAgTA.o: in function `main':
/mnt/c/madopt/examples/get_started.cpp:68: undefined reference to `MadOpt::Model::addVar(double, double, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: /tmp/cceTAgTA.o: in function `MadOpt::Param::Param()':
/usr/local/include/madopt/param.hpp:28: undefined reference to `MadOpt::Expr::Expr()'
/usr/bin/ld: /tmp/cceTAgTA.o: in function `main':
/mnt/c/madopt/examples/get_started.cpp:81: undefined reference to `MadOpt::Model::addParam(double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::cos(MadOpt::Expr const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::operator*(double const&, MadOpt::Expr const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::pow(MadOpt::Expr const&, double const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::Expr::operator+(MadOpt::Expr const&) const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::operator-(MadOpt::Expr const&, MadOpt::Expr const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::Expr::operator*(MadOpt::Expr const&) const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::operator-(MadOpt::Expr const&, MadOpt::Expr const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:107: undefined reference to `MadOpt::Model::addEqConstr(MadOpt::Expr const&, double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:113: undefined reference to `MadOpt::Expr::Expr(int)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:115: undefined reference to `MadOpt::operator-(MadOpt::Expr const&, double const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:115: undefined reference to `MadOpt::pow(MadOpt::Expr const&, double const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:115: undefined reference to `MadOpt::Expr::operator+=(MadOpt::Expr const&&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:118: undefined reference to `MadOpt::Model::setObj(MadOpt::Expr const&)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:123: undefined reference to `MadOpt::IpoptModel::solve()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:128: undefined reference to `MadOpt::Model::hasSolution() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:131: undefined reference to `MadOpt::Model::objValue() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:134: undefined reference to `MadOpt::Model::status() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:137: undefined reference to `MadOpt::Var::x() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:140: undefined reference to `MadOpt::Constraint::lam() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:148: undefined reference to `MadOpt::Var::lb(double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:149: undefined reference to `MadOpt::Var::ub(double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:150: undefined reference to `MadOpt::Var::init(double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:152: undefined reference to `MadOpt::Var::lb()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:152: undefined reference to `MadOpt::Var::ub()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:152: undefined reference to `MadOpt::Var::init()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:156: undefined reference to `MadOpt::Constraint::lb(double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:157: undefined reference to `MadOpt::Constraint::ub(double)'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:159: undefined reference to `MadOpt::Constraint::lb()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:159: undefined reference to `MadOpt::Constraint::ub()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:171: undefined reference to `MadOpt::Model::solAsInit()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:174: undefined reference to `MadOpt::IpoptModel::solve()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:175: undefined reference to `MadOpt::Model::hasSolution() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:178: undefined reference to `MadOpt::Model::status() const'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:26: undefined reference to `MadOpt::IpoptModel::~IpoptModel()'
/usr/bin/ld: /mnt/c/madopt/examples/get_started.cpp:26: undefined reference to `MadOpt::IpoptModel::~IpoptModel()'
collect2: error: ld returned 1 exit status

A few things I have tried so far are:

All these still lead to the same result. I appreciate any help on this issue. Thank you.

stanle commented 2 years ago

Hello. I get the same error when doing the same thing as you.

Unfortunately, I haven't done C++ in quite a while. This looks like one of these problems, where you probably get much faster help, asking an experienced C++ developer. I will see if I can refresh my memory in the next few days, but no guarantees.

best of luck

ccoffrin commented 2 years ago

@hhijazi or @dexterurbane may be able to help on the general C++ side.

dexterurbane commented 2 years ago

Yes, I can help here. Does this work?

g++ -o get_started get_started.cpp -lmadopt -lmadopt_ipopt -lipopt
stanle commented 2 years ago

Works for me. Thanks for the shortcut on refreshing my memory :).

dexterurbane commented 2 years ago

Excellent, no worries!

CIT2708 commented 2 years ago

That worked for me too. Thank you @stanle and @dexterurbane for your prompt response.

dexterurbane commented 2 years ago

All good!

CIT2708 commented 2 years ago

As an extension to my previous question, if the get_started.cpp file is modified for BONMIN with -lmadopt_bonmin and -lbonmin flags, the code still produces error while trying to run. Are there any additional flags I need to add to it while compiling?

The code:

/*
 * Copyright 2014 National ICT Australia Limited (NICTA)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//#include <madopt/ipopt_model.hpp>
#include <madopt/bonmin_model.hpp>
#include <vector>
#include <math.h>
using namespace std;

int main(){
    int N = pow(10, 4);

    //create IpoptModel
    //MadOpt::IpoptModel m;

    //create BonminModel
    MadOpt::BonminModel m;

    // Options
    //
    //
    //m.setIntegerOption("print_level", 5);
    //m.setStringOption("print_timing_statistics", "yes");
    //m.setStringOption("hessian_approximation", "limited-memory");

    // shortcut flags
    // set flag if solver output should be printed
    m.show_solver = true;
    // timelimit, a negative value disables the time limit
    m.timelimit = 12;

    // Variables
    //
    //
    // create continuous variable x
    // lower bound = -1.5
    // upper bound = 0
    // initial value = -0.5
    // MadOpt::Var x = m.addVar(-1.5, 0, -0.5, "x");

    // to set a bound to infinity use MadOpt::INF
    // MadOpt::Var v = m.addVar(MadOpt::INF, 0, -0.5, "v");

    // create integer variable y
    // lower bound = 1
    // upper bound = 10
    // initial value = 4
    // MadOpt::Var y = m.addIVar(1, 10, 4, "y");

    // create binary variable z
    // initial value = 1
    // MadOpt::Var z = m.addBVar(1, "z");

    vector<MadOpt::Var> x(N);
    for (int i=0; i<N; i++){
        x[i] = m.addVar(-1.5, 0, -0.5, "x" + to_string(i));
    }

    // Parameters
    //
    //
    // create Parameter
    // value = 1
    // name = p
    // MadOpt::Param p = m.addParam(1, "p");
    vector<MadOpt::Param> p(N-2);
    for (int i=0; i<N-2; i++){
        double a = double(i+2)/(double)N;
        p[i] = m.addParam(a, "p"+to_string(i));
    }

    // Constraints
    //
    //
    // expr = expression build from constants, variables MadOpt::Var, parameters MadOpt::Param
    // lb = lower bound, double
    // ub = upper bound, double
    //
    // add constraint of type:
    //
    // lb <= expr <= ub
    // m.addConstr(lb, expr, ub);
    //
    // lb <= expr <= INF
    // m.addConstr(lb, expr);
    //
    // INF <= expr <= ub
    // m.addConstr(expr, ub);
    //
    // expr == eq
    // m.addEqConstr(expr, eq);

    vector<MadOpt::Constraint> c(N-2);
    for (int i=0; i<N-2; i++){
        c[i] = m.addEqConstr((MadOpt::pow(x[i+1], 2) + 1.5*x[i+1] - p[i])*MadOpt::cos(x[i+2]) - x[i], 0);
    }

    // Objective
    //
    //
    MadOpt::Expr obj(0);
    for (int i=0; i<N; i++)
        obj += MadOpt::pow(x[i] - 1, 2);

    // set objective
    m.setObj(obj);

    // calling the solver
    //
    //
    m.solve();

    // check if a solution exists, or in other words if the solver succeeded
    //
    //
    if (m.hasSolution()){

        // access objective value
        cout<<"Objective:"<<m.objValue()<<endl;

        // access solver status == Ipopt status values
        cout<<"Solver status:"<<m.status()<<endl;

        //print solution value of first variable
        cout<<"Solution value for x0:"<<x[0].x()<<endl;

        //print lambda value of constraint, only available for IpoptModel
        cout<<"Lambda value of constraint 0:"<<c[0].lam()<<endl;
    }
   /***
    //changing the model
    //
    //
    // Variables
    //change variable bounds and initial value
    x[1].lb(-12);
    x[1].ub(1);
    x[1].init(0);
    //access bounds and initial values
    cout<<"Variable values: lb="<<x[1].lb()<<" ub="<<x[1].ub()<<" init="<<x[1].init()<<endl;

    //Constraints
    //change constraint bounds
    c[0].lb(-1);
    c[0].ub(2);
    //access bounds 
    cout<<"Constraint bounds: lb="<<c[0].lb()<<" ub="<<c[0].ub()<<endl;

    //Parameter
    // change value
    p[0].value(12);
    // access value
    cout<<"Parameter value:"<<p[0].value()<<endl;

    // resolve
    //
    //
    // set solution as initial values
    m.solAsInit();

    m.show_solver = false;
    m.solve();
    if (not m.hasSolution())
        cout<<"no solution"<<endl;
    // test for specific solver status
    if (m.status()==MadOpt::Solution::LOCAL_INFEASIBILITY)
        cout<<"Infeasible"<<endl;
    ***/
    return 0;
}

Compile command: g++ get_started.cpp -lmadopt -lmadopt_bonmin -lbonmin -lmadopt_ipopt -lipopt -lcoinmumps -g -O2

Note: It wouldn't compile without the IPOPT library flags and in the IPOPT example, the code wouldn't run without the coinmumps flag.

@stanle @dexterurbane do you have any ideas on how to solve this issue?

stanle commented 2 years ago

Hi. Sorry I was not around.

Your code above throws an error on the line

        //print lambda value of constraint, only available for IpoptModel
        cout<<"Lambda value of constraint 0:"<<c[0].lam()<<endl;

This line only works for IpoptModels. If you comment it out, then the code works for me.

best regards Karsten

CIT2708 commented 2 years ago

Hi @stanle, thank you for your response. Unfortunately, that didn't seem to solve the issue for me. I still get

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

What flags did you use for compiling your code?

stanle commented 2 years ago

I used what you have in your previous post.

On Fri, 12 Nov. 2021, 06:36 CIT2708, @.***> wrote:

Hi @stanle https://github.com/stanle, thank you for your response. Unfortunately, that didn't seem to solve the issue for me. I still get

terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted

What flags did you use for compiling your code?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stanle/madopt/issues/26#issuecomment-966674351, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACNXWXXO6XE6JZ6WWIESQWDULRAPBANCNFSM5HDILFYA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.