[gecode-users] Fwd: move from a choco model to Gecode model
manel askri
askri.manel1 at gmail.com
Thu Sep 5 04:11:22 CEST 2013
hi everyone!
what i want to do is represented by this model (see the following figure
:MMKP Model)
i will also explain what is the problem: I'm trying to solve the
multidimensional
and multiple knapsack problems, so i have a problem with the placement of
the right tester(or testers) in the right node ,we assume that execution
environment consists of m nodes and we have n test components that may be
assigned to them. We attempt to find an optimal solution of test component
placement not violating resource and connectivity constraints and also
maximizing
their placement profit.
Ihave solve this problem using the CHoco solver but i got some errors with
Gecode
I will send also the wholecode in java with comments :
public class MMKP {
//i'm trying to find two defferent types of solution the Optimal one
wchich has the Objective function and a realizable one which
//give me all the solutions
public int[][] find_Optimal_Solution(int n, int m, int[]R,
int[]C,int[]B,int[] Dr,int[] Dc,int[] Db,int[][] g)
{
int [][] results= new int[n][m];
//creation du model
CPModel model = new CPModel();
// creation des variables
IntegerVariable[][] X = new IntegerVariable[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
X[i][j] = Choco.makeIntVar("X" + i+j, 0, 1);}}
//objective variable
IntegerVariable Z = Choco.makeIntVar("gain", 1,
1000000,Options.V_OBJECTIVE);
//creations des contraintes
// ... over rows
Constraint[] rows = new Constraint[n];
for (int i = 0; i < n; i++) {
rows[i] = Choco.eq(Choco.sum(X[i]), 1);
}
model.addConstraints(rows);
//... over columns
// first, get the columns, with a temporary array
IntegerVariable[][] XDual = new IntegerVariable[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
XDual[i][j] = X[j][i];
}
}
Constraint[] cols_ram = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_ram[j] = Choco.leq(Choco.scalar(Dr,XDual[j]),R[j]);
}
Constraint[] cols_cpu = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_cpu[j] = Choco.leq(Choco.scalar(Dc,XDual[j]),C[j]);
}
Constraint[] cols_bat = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_bat[j] = Choco.leq(Choco.scalar(Db,XDual[j]),B[j]);
}
model.addConstraints(cols_ram);
model.addConstraints(cols_cpu);
model.addConstraints(cols_bat);
//Objective function : here i can't find with Gecode
IntegerExpressionVariable []exp1=new IntegerExpressionVariable [n];
for (int i = 0; i < n; i++)
exp1[i]=Choco.scalar(g[i], X[i]);
model.addConstraint(Choco.eq(Choco.sum(exp1),Z));
//solve the problem
Solver s = new CPSolver();
s.read(model);
s.maximize(s.getVar(Z), false);
System.out.println("Number of solutions found:"+s.getSolutionCount());
System.out.println("Solution:"+s.solutionToString());
//Print the values
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(s.getVar(X[i][j]).getVal() + " ");
results[i][j]=s.getVar(X[i][j]).getVal();
}
System.out.println();
}
return results;
}
public int[][] find_realizable_solution(int n, int m, int[]R,
int[]C,int[]B,int[] Dr,int[] Dc,int[] Db)
{
int[][] results= new int[n][m];
//creation du model
CPModel model = new CPModel();
// creation des variables
IntegerVariable[][] X = new IntegerVariable[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
X[i][j] = Choco.makeIntVar("X" + i+j, 0, 1);
}
}
//objective variable
IntegerVariable Z = Choco.makeIntVar("gain", 1,
1000000,Options.V_OBJECTIVE);
//creations des contraintes
// ... over rows
Constraint[] rows = new Constraint[n];
for (int i = 0; i < n; i++) {
rows[i] = Choco.eq(Choco.sum(X[i]), 1);
}
model.addConstraints(rows);
//... over columns
// first, get the columns, with a temporary array
IntegerVariable[][] XDual = new IntegerVariable[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
XDual[i][j] = X[j][i];
}
}
Constraint[] cols_ram = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_ram[j] = Choco.leq(Choco.scalar(Dr,XDual[j]),R[j]);
}
Constraint[] cols_cpu = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_cpu[j] = Choco.leq(Choco.scalar(Dc,XDual[j]),C[j]);
}
Constraint[] cols_bat = new Constraint[m];
for (int j = 0; j < m; j++) {
cols_bat[j] = Choco.leq(Choco.scalar(Db,XDual[j]),B[j]);
}
model.addConstraints(cols_ram);
model.addConstraints(cols_cpu);
model.addConstraints(cols_bat);
//solve the problem
Solver s = new CPSolver();
s.read(model);
s.solve();
System.out.println("Number of solutions found:"+s.getSolutionCount());
System.out.println("Solution:"+s.solutionToString());
//Print the values
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(s.getVar(X[i][j]).getVal() + " ");
results[i][j]=s.getVar(X[i][j]).getVal();
}
System.out.println();
}
return results;
}
}
Sorry for the last time i didn't wrote it properly
Now , My code in Gecode :
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/search.hh>
#include <gecode/minimodel.hh>
using namespace Gecode;
using namespace std;
class MMKP : public Script{
protected:
static const int n=4 ;//number of testers, number of the rows
static const int m=5;// number of nodes, number of the comluns
static const int max=100;// the max that can be taking by a node or by a
test
IntArgs R;
IntArgs C;
IntArgs B;
IntArgs Dr;
IntArgs Dc;
IntArgs Db;
IntArgs g;
BoolVarArray a;
IntVar l;
public :
enum {
find_Optimal_Solution, find_realizable_solution
};
MMKP (const Options& opt) : a(*this,n*m, 0,1){
Matrix <BoolVarArray> X (a,m,n);
R=IntArgs(m,9,15,17,20,40);
C=IntArgs(m,15,20,25,30,46);
B=IntArgs(m,30,37,59,60,70);
Dr=IntArgs(n,3,5,7,10);
Dc=IntArgs(n,5,10,15,30);
Db=IntArgs(n,10,13,17,19);
//creation variables
// objectiv variable
IntVar gain (*this, 1,1000000);
//creation of constraints
// ... over rows
for ( int j=0; j<n;j++)
{
linear(*this , X.row(j),IRT_EQ,1);
}
//... over columns
// first, get the columns, we will use an intermidiare matrix XDual
for (int i = 0; i < m; i++) {
linear(*this, Dr,X.col(i),IRT_NQ, R[i]);
}
for (int i = 0; i < m; i++) {
linear (*this, Dc, X.col(i), IRT_NQ,C[i]);
}
for (int i = 0; i < m; i++) {
linear (*this, Db, X.col(i), IRT_NQ,B[i]);
}
switch (opt.model()) {
case find_Optimal_Solution:
g=IntArgs( n*m,0,1000);//this is a matrix provided by another class but i
choose to start with simple example
//Objective function: i don't know how to write it
for (int i = 0; i < n; i++)
{ //IntArgs row=g.slice(i*n, 1, m);
//BoolVarArray row1=X.row(i);
//linear(*this, g,X.row(i), IRT_EQ, 1);
}
break;
case find_realizable_solution:
cout << "manel"<< endl;
break;
}
// post branching
//branch(*this, a, INT_VAR_SIZE_MAX(), INT_VAL_MAX());
for( int i=0;i<m;i++)
{
branch(*this, X.col(i), INT_VAR_SIZE_MAX(), INT_VAL_MAX());
}
for( int i=0;i<n;i++)
{
branch(*this, X.row(i), INT_VAR_SIZE_MAX(), INT_VAL_MAX());
}
}
// search support
MMKP(bool share, MMKP& s) : Script(share, s){
a.update(*this, share, s.a);
}
virtual Space* copy(bool share) {
return new MMKP(share,*this);
}
// print solution
void print(std::ostream& os) const {
Matrix <BoolVarArray> X (a,m,n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++)
os << std::setw(4) << a[i * n + j];
os << std::endl;
}
os << std::endl;
}
};
// main function
int main(int argc, char* argv[]) {
Options opt("MMKP");
//opt.model(MMKP::find_Optimal_Solution, "optimal","utiliser
find_Optimal_Solution" );
opt.model(MMKP::find_realizable_solution, "réalisable","utiliser
find_realizable_solution" );
opt.parse(argc,argv);
Script::run<MMKP,DFS,Options>(opt);
system("pause");
return 0;
}
so this is all my work with Gecode
if YOU have any other quation i'm ready to answer it
I have an other quation : is it possible to passe variables from another
class to Gecode class , like we always do in C++, because all the variables
that i use here (IntArgs R;IntArgs C;IntArgs B;IntArgs Dr;IntArgs Dc;IntArgs
Db;IntArgs g;) there are honnestly provided by some other work in c++
Thank YOU very much for your help
MANEL
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.gecode.org/pipermail/users/attachments/20130905/05824e80/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MMKP Model.png
Type: image/png
Size: 29099 bytes
Desc: not available
URL: <http://www.gecode.org/pipermail/users/attachments/20130905/05824e80/attachment-0001.png>
More information about the users
mailing list