[FlopCpp] runtime error when defining constraints in separate methods

Tim Hultberg Tim.Hultberg at eumetsat.int
Sat May 5 03:46:11 EDT 2007


To understand what happens, it is necessary to explain that flopc++
maintains
a "current" model to which all MP_constraints are added at construction
time.

[ actually, in line with this, (*mm).add(c1); is redundant ]

So when f1 is executed c2 gets added to the set of constraints in the
"current" model, mm. That is, a pointer to c2 is inserted in the
set<MP_constraint *> Constraints; member of mm. When c2 goes out of
scope
the object is destructed, but the pointer in mm remains. Now the
problems occur
when trying to generate a problem from the model mm, because that will
invoke
methods from the MP_constraints pointed to by the elements of
mm.Constraints.

I am considering to put code in the MP_constraint destructor to let it
erase
itself from the model to which it belongs. But if the intention is to
have c2
and c1 belong to the same model, it would be better to let c2 be a
member of
of model.   

Best regards, Tim


Tim Hultberg

>>> "Paul Huang" <huangpaul99 at gmail.com> 05/03/07 5:51 AM >>>
Hello group.

I create a class, which maintains a MP_model as a member. In two
separate
methods of this class, I define two constraints and add them to the
MP_model. At runtime, my program generates a runtime failure. Here is
the
error message

"Run-Time Check Failure #0 - The value of ESP was not properly saved
across
a function call. This is usually a result
of calling a function declared with one calling convention with a
function
pointer declared with a different calling
convention."

I am using Visual C++ 2005 Express. The source codes are attached below.
Can
someone please point out what the problem is and how to solve it?

An interesting observation is that if I comment out a MP_constraint
declaration is method f1(). The program will run without error even this
declaration seems to have nothing to do with another method f2(), which
declare another MP_constraint and maximize a simple objective.

Thanks,
Paul

------model.hpp----------------
#ifndef _model_hpp_
#define _model_hpp_

#include "flopc.hpp"
using namespace flopc;

    class model{
    public:

        MP_set S1, S2;
        MP_variable * Y;
        MP_model * mm;

        model(int numS1TimeRange,
              int numS2
             );

        ~model();

        void f1();
        void f2();
    };
#endif
----------------------------

-------model.cpp--------
#include "model.hpp"
#include <OsiCbcSolverInterface.hpp>


    model::model(int numS1,
        int numS2){
            S1= MP_set(numS1);
            S2 = MP_set(numS2);
            mm= new MP_model(new OsiCbcSolverInterface);
            Y=new MP_binary_variable(S1);

        };


    model::~model(){
            delete mm;
            delete Y;
        };


    void model::f1(){
        MP_constraint c2;   <===will run if this line is commented out.
    };


    void model::f2(){

        MP_constraint c1;
        c1=sum(S1, (*Y)(S1))<=2;
        (*mm).add(c1);

        (*mm).maximize(sum(S1, (*Y)(S1)));
        cout<<"Y size: "<<(*Y).size()<<endl;
        (*Y).display();

    }
--------------------------

-----main.cpp-------
#include "model.hpp"
#include <iostream>
using namespace std;
void main(){
    int numS1=5;
    int numS2=9;
    model m1(numS1, numS2);
    m1.f1();
    m1.f2();

}
----------------------------



More information about the FlopCpp mailing list