### C++ ILOG CPLEX

I have C++ codes for 0/1 knapsack problem. How can I transform it into ILOG CPLEX environment?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

const int lag=1000;

int ebo(int,int);
int maximum(int,int);

int value[lag],
weight[lag],
v[lag][lag],
x[lag];

int i,
j,
m,
n;

int ebo(int i,int j)
{
int val;
if(v[i][j]==-1)
{
if(j<weight[i])

val=ebo(i-1, j);

else val=maximum(ebo(i-1, j),(ebo(i-1, j-weight[i])+value[i]));
v[i][j]=val;
}
return v[i][j];
}

void esra()
{
int k;

k=m;

cout<<"The cuts are as follows: \n\n";

for(int i=n; i>=0; i--)
{
if(v[i][k]!=v[i-1][k])
{

x[i]=1;

if(v[i][k]==0) break;
else

cout<<setw(10)<<"At stage "<<i<<": "<<"f["<<k<<"]"<<" = "<<v[i][k]<<": => ";

for(int p=1; p<=i; p++)
{

cout<<value[p]<<"x["<<p<<"]";
if(p==i) break;
else
cout<<" + ";
}

cout<<" <= "<<v[i][k]<<endl;
cout<<"\n"<<endl;

k=k-weight[i];
}
}
for(int i=1; i<=n; i++)
{
cout<<"The optimal solution: ";

for(i=1; i<=n; i++)

cout<<"\n x["<<i<<"] = "<<x[i]<<"\n"<<" ";
}
}

int maximum(int a, int b)
{
return (a>b)?a:b;
}

int main()
{

int f,
W;

cin>>n;

cout<<" \n"<<endl;
cout<<"Uncorrelated data instance for the 0-1 KP:\n"<<endl;

cout<<"weights"<<setw(9)<<"values"<<endl;
cout<<"________________"<<endl;

srand(time(0));
for(int i=1; i<=n; i++)
{
weight[i] = rand() % 10 + 1; // 1<=w<=10
value[i] = rand() % 10 + 1; // 1<=v<=10

cout<<setw(4)<<weight[i]<<setw(10)<<value[i]<<endl;

}
cout<<"\n"<<endl;

cout<<"Generate total weight capacity of knapsack:\n";
int total = 0 ;
for(int i = 0; i<=n; i++)
{
total = total + weight[i];
}
m = total/2;
cout<<"W = "<<m<<endl;

cout<<"\n"<<endl;
for(int i=0; i<=m; i++)
v[0][i]=0;
for(int i=0; i<=n; i++)
v[i][0]=0;
for(int i=1; i<=n; i++)

for(int j=1; j<=m; j++)

v[i][j]=-1;

f=ebo(n, m);

cout<<"The optimal value is f = "<<f<<endl;

cout<<" "<<endl;

for(int i=1; i<=n; i++)

x[i]=0;

esra();

system("pause");

return 0;
}
Topic archived. No new replies allowed.