以下是我的CPLEX代码,我正在其中尝试实现云计算定价问题(the link of paper)。这是一个最大化问题,目的是最大化云服务提供商的收入。我面临的问题是我无法将常量添加到目标函数。
package cplex;
import java.util.Arrays;
import java.util.Random;
import ilog.concert.*;
import ilog.cplex.*;
import java.util.*;
class providers
{
double price;
int available;
}
public class Main {
public static void main(String args[]) {
double cin=6.951; //lumped cost
double phi=0.01; //difference b/w customer cost and in-source cost
int K=20,N=100; //K is no pf service provider in federation
//N is total number of customers
int uin=5; //average user request for instance in
int pin=5;//average provider request for instance in
Random rand=new Random();
//declare k-1 providers;
providers[] pi=new providers[K-1];
double umin; // lower bound of price proposed to users
umin=(cin+phi*(K-1)*pin)/(N*uin+(K-1)*pin);//using theorem 4.1
double[] insrcprices=new double[K-1]; //insourcing prices proposed by other providers at previous hour
System.out.println("insourcing prices proposed by other providers\n");
for(int i=0;i<K-1;i++)
{
pi[i]=new providers();
double temp=rand.nextInt()%100+1;
insrcprices[i]=(temp<0?-(temp/1000):temp/1000);
pi[i].price=insrcprices[i]+phi;
System.out.print(insrcprices[i]+" ");
}
Arrays.sort(insrcprices);
//find minimum price satisfying the constraint
double ustr=0.0;
for(int i=0;i<insrcprices.length;i++)
{
if(insrcprices[i]>umin)
{
ustr=insrcprices[i]+phi;
break;
}
}
System.out.println("\nlower bound of price proposed to users is:");
System.out.printf("%.3f",umin);
System.out.println("\nupper bound of price proposed to end users is ");
System.out.printf("%.3f",ustr);
double uSelected=umin+phi+(ustr+-umin-phi)*new Random().nextDouble();
System.out.println("\nthe selected price proposed to users is ");
System.out.printf("%.3f",uSelected);
double uInSelected=uSelected=0.01;
int[] bj=new int[K-1];
System.out.println("the amount of available resources\n");
for(int i=0;i<K-1;i++)
{
bj[i]=new Random().nextInt(20-0+1)+0;
pi[i].available=bj[i];
System.out.print(bj[i]+" ");
}
Arrays.sort(bj);
System.out.println("K-1 providers are\n");
for(int i=0;i<K-1;i++)
{
System.out.println("provider "+i+"price = "+pi[i].price+" available resources = "+pi[i].available);
}
model1(uInSelected,uSelected,pi,K);
}
public static void model1(double uij,double uii,providers[] pi,int K)
{
try{
double Ro=100.0;
double dp=10; //provider request
double du=100; // user request
IloNumVar[] xji=new IloNumVar[K-1];
int yil=new Random().nextInt(20-0+1)+0;
double c=0.005;//cost per hour
IloCplex cplex=new IloCplex();
//variable
IloNumVar xii=cplex.numVar(0, Integer.MAX_VALUE,"x");
IloNumVar xij=cplex.numVar(0, Integer.MAX_VALUE,"y");
for(int i=0;i<K-1;i++)
{
xji[i]=cplex.numVar(0,Double.MAX_VALUE,"xji"+i);
}
//expressions
IloLinearNumExpr objective=cplex.linearNumExpr();
objective.addTerm(uii, xii);
objective.addTerm(uij, xij);
for(int i=0;i<K-1;i++)
{
objective.addTerm(uii-pi[i].price, xji[i]);
}
//objective.addTerm(0.12, x);
//objective.addTerm(0.15, y);
//define objective
cplex.addMaximize(objective);
//define constraints
IloLinearNumExpr outsrc=cplex.linearNumExpr();
for(int i=0;i<K-1;i++)
{
outsrc.addTerm(1.0, xji[i]);
}
outsrc.addTerm(1.0,xii);
cplex.addEq(outsrc, du);
cplex.addLe(xij,dp);
cplex.addLe(outsrc, yil);
cplex.addGe(cplex.sum(objective,-(c*yil)),Ro);
for(int i=0;i<K-1;i++)
{
cplex.addLe(xji[i],pi[i].available);
}
/*cplex.addGe(cplex.sum(cplex.prod(60, x),cplex.prod(60, y)), 300); // addGe(LHS,RHS) of inequality
cplex.addGe(cplex.sum(cplex.prod(12, x),cplex.prod(6, y)), 36);
cplex.addGe(cplex.sum(cplex.prod(10, x),cplex.prod(30, y)), 90);*/
//solve
if(cplex.solve())
{
double x=cplex.getBestObjValue();
System.out.println("model solved objective = "+(x-c*yil));
System.out.println("model solved objective = "+(int)cplex.getValue(xii));
System.out.println("model solved objective = "+(int)cplex.getValue(xij));
}
else
{
System.out.println("some thing went wrong!!!\n model not solved");
}
}
catch(IloException e)
{
e.printStackTrace();
}
}
}
我得到以下输出:
其他提供商提出的内包价格
0.047 0.069 0.061 0.025 0.078 0.028 0.048 0.059 0.057 0.097 0.021 0.027 0.018 0.092 0.094 0.025 0.092 0.092 0.034
lower bound of price proposed to users is:
0.013
upper bound of price proposed to end users is
0.028
the selected price proposed to users is
0.026the amount of available resources
4 1 3 17 18 14 7 10 14 4 13 20 6 20 14 12 16 16 16 K-1 providers are
provider 0price = 0.057 available resources = 4
provider 1price = 0.079 available resources = 1
provider 2price = 0.071 available resources = 3
provider 3price = 0.035 available resources = 17
provider 4price = 0.088 available resources = 18
provider 5price = 0.038 available resources = 14
provider 6price = 0.058 available resources = 7
provider 7price = 0.06899999999999999 available resources = 10
provider 8price = 0.067 available resources = 14
provider 9price = 0.107 available resources = 4
provider 10price = 0.031 available resources = 13
provider 11price = 0.037 available resources = 20
provider 12price = 0.027999999999999997 available resources = 6
provider 13price = 0.102 available resources = 20
provider 14price = 0.104 available resources = 14
provider 15price = 0.035 available resources = 12
provider 16price = 0.102 available resources = 16
provider 17price = 0.102 available resources = 16
provider 18price = 0.044000000000000004 available resources = 16
Constraints 'c1' and 'c3' are inconsistent.
Presolve time = 0.00 sec. (0.01 ticks)
some thing went wrong!!!
model not solved
我看不到错误。我是cplex的新手。