1 // $Id$
2 #include <OsiCbcSolverInterface.hpp>
3 #include <flopc.hpp>
4 using namespace flopc;
5 using namespace std;
6 
MultiProduct(MP_set & Cities,MP_set & Products,MP_subset<3> & Routes,MP_subset<2> & Suppliers,MP_subset<2> & Customers,MP_subset<2> & Connections,MP_data & supply,MP_data & demand,double & limit,MP_data & cost)7 void MultiProduct(
8     MP_set& Cities,
9     MP_set& Products,
10     MP_subset<3>& Routes,
11     MP_subset<2>& Suppliers,
12     MP_subset<2>& Customers,
13     MP_subset<2>& Connections,
14     MP_data& supply,
15     MP_data& demand,
16     double& limit,
17     MP_data& cost
18 ) {
19     MP_index p,o,d;
20     MP_variable trans(Routes);
21 
22     MP_constraint sup(Suppliers);
23     MP_constraint dem(Customers);
24     MP_constraint lim(Connections);
25 
26     sup(Suppliers(p,o)) =
27 	sum(Routes(p,o,d), trans(Routes(p,o,d))) <= supply(Suppliers(p,o));
28 
29     dem(Customers(p,d)) =
30  	sum(Routes(p,o,d), trans(Routes(p,o,d))) >= demand(Customers(p,d));
31 
32     lim(Connections(o,d)) =
33 	sum(Routes(p,o,d), trans(Routes(p,o,d))) <= limit;
34 
35     minimize( sum(Routes(p,o,d), cost(Routes(p,o,d)) * trans(Routes(p,o,d))) );
36 
37     assert(MP_model::getDefaultModel()->getNumRows()==41);
38     assert(MP_model::getDefaultModel()->getNumCols()==18);
39     assert(MP_model::getDefaultModel()->getNumElements()==54);
40 
41     trans.display("solution");
42 }
43 
random(double min,double max)44 double random(double min, double max) {
45     return min + (max-min)*(rand()/(RAND_MAX+0.0));
46 }
47 
main()48 int main() {
49     MP_model::getDefaultModel().setSolver(new OsiCbcSolverInterface);
50     MP_model::getDefaultModel().verbose();
51     enum {Godiva,Neuhaus,Leonidas,nbrProducts};
52     enum {Brussels,Amsterdam,Antwerpen,Paris,Milan,Cassis,Bonn,Madrid,Bergen,
53 	  London,nbrCities};
54 
55     MP_set Cities(nbrCities);
56     MP_set Products(nbrProducts);
57     MP_subset<3> Routes(Products,Cities,Cities);
58 
59     Routes.insert(Godiva,Brussels,Paris);
60     Routes.insert(Godiva,Brussels,Bonn);
61     Routes.insert(Godiva,Amsterdam,London);
62     Routes.insert(Godiva,Amsterdam,Milan);
63     Routes.insert(Godiva,Antwerpen,Madrid);
64     Routes.insert(Godiva,Antwerpen,Bergen);
65 
66     Routes.insert(Neuhaus,Brussels,Madrid);
67     Routes.insert(Neuhaus,Brussels,Bergen);
68     Routes.insert(Neuhaus,Amsterdam,Madrid);
69     Routes.insert(Neuhaus,Amsterdam,Cassis);
70     Routes.insert(Neuhaus,Antwerpen,Paris);
71     Routes.insert(Neuhaus,Antwerpen,Bonn);
72 
73     Routes.insert(Leonidas,Brussels,Bonn);
74     Routes.insert(Leonidas,Brussels,Milan);
75     Routes.insert(Leonidas,Amsterdam,Paris);
76     Routes.insert(Leonidas,Amsterdam,Cassis);
77     Routes.insert(Leonidas,Antwerpen,London);
78     Routes.insert(Leonidas,Antwerpen,Bergen);
79 
80     cout<<"Number of Routes = "<<Routes.size()<<endl;
81 
82     MP_subset<2> Suppliers(Products,Cities);
83     MP_subset<2> Customers(Products,Cities);
84     MP_subset<2> Connections(Cities,Cities);
85 
86     MP_index p,o,d;
87 
88     // Set "projections"
89     Suppliers(p,o) <<= Routes(p, o, MP_index::Any);
90     Customers(p,d) <<= Routes(p, MP_index::Any, d);
91     Connections(o,d) <<= Routes(MP_index::Any, o, d);
92 
93     cout<<Suppliers.size()<<endl;
94     cout<<Customers.size()<<endl;
95     cout<<Connections.size()<<endl;
96 
97     Routes.display();
98     Suppliers.display();
99     Customers.display();
100     Connections.display();
101 
102     MP_data supply(Suppliers);
103     MP_data demand(Customers);
104 
105     MP_data cost(Routes);
106 
107     for (int i=0; i<Suppliers.size(); i++) {
108 	supply(i) = random(200,300);
109     }
110     for (int i=0; i<Customers.size(); i++) {
111 	demand(i) = random(50,100);
112     }
113     for (int i=0; i<Routes.size(); i++) {
114 	cost(i) = random(1,2);
115     }
116     double limit = 500;
117 
118     MultiProduct(Cities,Products,Routes,Suppliers,Customers, Connections,
119 		 supply,demand,limit,cost);
120 
121     cout<<"Test multiproduct passed."<<endl;
122 }
123 
124