[FlopCpp] Re : Iterating over a solution variable and a domain.

Tim Hultberg Tim.Hultberg at eumetsat.int
Tue Jan 15 03:04:05 EST 2008


Hi YO,
   here is a small example, deriving from Functor. In the example, I
simply send the data to cout, but you can easily change that to store
the values in some array or similar.

Hope this helps,
                            Tim

----------------------------------->
#include "flopc.hpp"
using namespace flopc;
#include <OsiClpSolverInterface.hpp>

class MyFunctor : public Functor {
public:
  MyFunctor(MP_variable &v, MP_index& i, MP_index& j, MP_index &ij) 
    : V(v), I(i), J(j), IJ(ij) {}
  
  void operator()() const {
    cout<<I.evaluate()<<" "<<J.evaluate()<<" 
"<<V.level(IJ.evaluate())<<endl;
  }

  MP_variable &V;
  MP_index &I;
  MP_index &J;
  MP_index &IJ;
};

int main() {
    MP_model::getDefaultModel().setSolver(new OsiClpSolverInterface);
    enum  {seattle, sandiego, numS}; 
    enum  {newyork, chicago, topeka,numD};

    MP_set S(numS);          // Sources 
    MP_set D(numD);          // Destinations 
    MP_subset<2> Link(S,D);  // Transportation links (sparse subset of
S*D)

    Link.insert(seattle,newyork);
    Link.insert(seattle,chicago);
    Link.insert(sandiego,chicago);
    Link.insert(sandiego,topeka);

    MP_data SUPPLY(S);
    MP_data DEMAND(D);

    SUPPLY(seattle)=350;  SUPPLY(sandiego)=600;
    DEMAND(newyork)=325;  DEMAND(chicago)=300;  DEMAND(topeka)=275;
    
    MP_data COST(Link);

    COST(Link(seattle,newyork)) = 2.5;  
    COST(Link(seattle,chicago)) = 1.7;  
    COST(Link(sandiego,chicago))= 1.8;  
    COST(Link(sandiego,topeka)) = 1.4;

    COST(Link) = 90 * COST(Link) / 1000.0;
    
    MP_variable x(Link);

    MP_constraint supply(S);
    MP_constraint demand(D);  

    supply(S) =  sum( Link(S,D), x(Link) ) <= SUPPLY(S);
    demand(D) =  sum( Link(S,D), x(Link) ) >= DEMAND(D);
    
    minimize( sum(Link, COST(Link)*x(Link)) );
    
    assert(MP_model::getDefaultModel()->getNumRows()==5);
    assert(MP_model::getDefaultModel()->getNumCols()==4);
    assert(MP_model::getDefaultModel()->getNumElements()==8);
    assert(MP_model::getDefaultModel()->getObjValue()>=156.14 &&
MP_model::getDefaultModel()->getObjValue()<=156.16);
    
    x.display("Optimal solution:");
    supply.display("Supply dual solution");
    cout<<"Test transport passed."<<endl;

    MyFunctor doTheStuff(x,S,D,Link);
    
    forall( Link(S,D), doTheStuff);
}


>>> yo yo <yoyovicks at yahoo.fr> 11/01/2008 14:06 >>>
Hello Tim,

Thanks for your answers.
I had noticed the display method but I don't want to display them. So,
I had the idea deriving from Functor and using forall but it sounds
quite complicated for a such necessary feature so I asked the question
on this ml first.

The context is the following:
I have an application written in C# that needs to solve an LP problem.
I want to use C++/CLI to create the model with FLOPC++ based on data
coming from a Managed DataSet in C#.(this is ok so far).
Then solve it. (this is also ok).
Then I want to get back the solution from flopc++ into a managed
dataset.(this is the problem.)
Therefore I need a way to enumerate the content of flopc++ variables
and to populate the dataset. dimensions are in columns.

I am using subsets and the variables are very sparses so I cannot
iterate continuously using a for loop.
So, my only chance is to derive the functor class?
Do you have any example on how deriving the functor class, please?

Thanks for you help.
YO.


----- Message d'origine ----
De : Tim Hultberg <Tim.Hultberg at eumetsat.int>
À : flopcpp at list.coin-or.org; yoyovicks at yahoo.fr 
Envoyé le : Mardi, 8 Janvier 2008, 18h34mn 19s
Objet : Re: [FlopCpp] Iterating over a solution variable and a domain.

Some alternatives:
1. The display method of MP_variable does exactly this. See the
 examples.
2. Generally sets are indexed from 0 to size()-1
3. You might use forall to iterate over a domain, but then you will
 have to derive a class from Functor. I can show you how to do this if
you
 want and give me the context.

Cheers, Tim

>>> yo yo <yoyovicks at yahoo.fr> 01/08/08 4:48 PM >>>
Hello,

I am trying to use flopcpp, so far the modelling works OK. Now I would
 like to dump the content of a solution variable. I cannot find any
way
 to iterate over a variable and its domain. How can I do? I do not
know
 the values of the index because they are generated automatically.
I would like to make a "for each" on my solution variables and write
 the value (with index) to somewhere else. Is this possible?

Thanks for your help.
YO.




    
 
_____________________________________________________________________________

Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers
Yahoo!
 Mail http://mail.yahoo.fr 

_______________________________________________
FlopCpp mailing list
FlopCpp at list.coin-or.org 
http://list.coin-or.org/mailman/listinfo/flopcpp 






     
_____________________________________________________________________________

Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo!
Mail http://mail.yahoo.fr 

_______________________________________________
FlopCpp mailing list
FlopCpp at list.coin-or.org 
http://list.coin-or.org/mailman/listinfo/flopcpp


More information about the FlopCpp mailing list