Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only

please help me find the problem

  • 1.  please help me find the problem

    Posted 03/23/12 07:09 AM

    Originally posted by: CPLEXprobleminC++


    Deat friends,
    I am a beginner in CPLEX.
    I have run this program many times ,but the result apparently wrong.who can tell me waht may be wrong.
    data.txt is the storage place for data.
    here is the whole program,and I have attached part of the results.

    #include <ilcplex/ilocplex.h>
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    #define nPlant 2 //number of plants -i
    #define nCustm 5 //number of customers -j
    #define nVehic 3 //the type of the vehicle -l
    #define nTime 5 //number of periods -t
    #define Infin 1000000
    typedef IloArray<IloNumVarArray> NumVarMatrix;
    typedef IloArray<NumVarMatrix> NumVar3Matrix;
    typedef IloArray<NumVar3Matrix> NumVar4Matrix;

    ILOSTLBEGIN

    int MnVehic+1,dnCustm+1nTime+1;
    //int MnVehic+1,dnCustm+1nTime+1;
    double AnVehic+1,SnPlant+1,pnPlant+1,hnPlant+1;

    int main(int, char**) {
    int i,j,l,t;

    FILE *fp;
    fp=fopen("data.txt","r");
    float temper2;
    //int temper1;
    IloInt temper1;
    for(i=1;i<=nPlant;i++){
    fscanf_s(fp,"%f\t",&temper2); //格式化输出
    p[i]=temper2;
    printf("%f\t",p[i]);
    }

    for(i=1;i<=nPlant;i++){
    fscanf_s(fp,"%f\t",&temper2); //格式化输出
    S[i]=temper2;
    printf("%f\t",S[i]);
    }

    for(i=1;i<=nPlant;i++){
    fscanf_s(fp,"%f\t",&temper2); //格式化输出
    h[i]=temper2;
    printf("%f\t",h[i]);
    }

    for(i=1;i<=nVehic;i++){
    fscanf_s(fp,"%f\t",&temper2); //格式化输出
    A[i]=temper2;
    printf("%f\t",A[i]);
    }

    for(i=1;i<=nVehic;i++){
    fscanf_s(fp,"%d\t",&temper1); //格式化输出
    M[i]=temper1;
    printf("%d\t",M[i]);
    }

    for(i=1;i<=nCustm;i++){
    for(j=1;j<=nTime;j++){
    fscanf_s(fp,"%d\t",&temper1); //格式化输出
    d[i][j]=temper1;
    printf("%d\t",d[i][j]);
    }
    }

    fclose(fp);

    IloEnv env;
    IloModel model(env);

    //定义二维变量Xit:Production quantity in plant i in period t;
    NumVarMatrix Product_qua(env,nPlant+1);
    for(i=1;i<=nPlant;i++)
    {
    Product_qua[i]=IloNumVarArray(env,nTime+1);
    for(t=1; t<=nTime; t++)
    {
    Product_qua[i][t]=IloNumVar(env,0,IloInfinity,ILOINT);
    }
    }

    //定义二维变量Iit:Inventory quantity in plant i in period t;
    NumVarMatrix Invent_qua(env,nPlant+1);
    for(i=1;i<=nPlant;i++)
    {
    Invent_qua[i]=IloNumVarArray(env,nTime+1);
    for(t=0; t<=nTime; t++){
    Invent_qua[i][t]=IloNumVar(env,0,IloInfinity,ILOINT);}
    }

    //定义二元变量y[i][t]
    NumVarMatrix y(env,nPlant+1);
    for(i=1;i<=nPlant ;i++)
    {
    y[i]=IloNumVarArray(env,nTime+1);
    for(t=1; t<=nTime; t++){
    y[i][t]=IloNumVar(env,0,IloInfinity,ILOINT);}
    }

    //定义三维变量Zijt:Transportation quantity from plant i to customer j in period t;
    NumVar3Matrix Tran_qua(env,nPlant+1);
    for(i=1; i<=nPlant; i++) {
    Tran_qua[i] = NumVarMatrix(env, nCustm+1);
    for(j=1; j<=nCustm; j++) {
    Tran_qua[i][j] = IloNumVarArray(env, nTime+1);
    for(t=1; t<=nTime; t++) {
    Tran_qua[i][j][t] = IloNumVar(env,0,IloInfinity,ILOINT);
    }
    }
    }
    //定义四维Uijlt变量
    NumVar4Matrix Vehic_num(env,nPlant+1);
    for(i=1;i<=nPlant; i++){
    Vehic_num[i]=NumVar3Matrix(env,nCustm+1);
    for(j=1;j<=nCustm; j++) {
    Vehic_num[i][j] = NumVarMatrix(env,nVehic+1);
    for(l=1;l<=nVehic;l++) {
    Vehic_num[i][j][l] = IloNumVarArray(env, nTime+1);
    for(t=1;t<=nTime; t++) {
    Vehic_num[i][j][l][t] = IloNumVar(env,0,IloInfinity,ILOINT);
    }
    }
    }
    }
    IloExpr expr1(env);
    for (i=1;i<=nPlant; i++) {
    for (t =1; t<=nTime; t++){
    expr1 += S[i] * y[i][t]+p[i]* Product_qua[i][t]+h[i]*Invent_qua[i][t];
    }
    }
    IloExpr expr2(env);
    for (i =1; i<=nPlant; i++){
    for (j =1; j<=nCustm; j++) {
    for (l =1; l<=nVehic; l++) {
    for (t =1; t<=nTime; t++) {
    expr2 +=A[l]*Vehic_num[i][j][l][t]; //∑ijlt Al[l]*Vehic_num[i][j][l][t]
    }
    }
    }
    }

    model.add(IloMinimize(env,expr1+expr2));
    expr1.end();
    expr2.end();
    for (i =1; i<=nPlant; i++)
    {
    model.add(Invent_qua[i][0]==0); //初始库存为0
    }

    //constrains :inventery balance

    for (i = 1; i <=nPlant; i++){
    for (t = 1; t <= nTime; t++) {
    IloExpr expr(env);
    for (j= 1; j<=nCustm; j++)
    {
    expr+=Tran_qua[i][j][t];
    }
    model.add(Invent_qua[i]t-1+Product_qua[i][t]-expr==Invent_qua[i][t]);
    //printf("Invent_qua%d%d=%d\t",i,t,Invent_qua[i][t]);
    expr.end();
    }
    }
    //constrains :vehicle capacity
    for (i =1; i <=nPlant; i++){
    for (j =1; j <=nCustm; j++) {
    for (t = 1; t <=nTime; t++) {
    IloExpr expr(env);
    for (l =1; l<=nVehic; l++)
    {
    expr+=M[l]*Vehic_num[i][j][l][t];
    }
    model.add(Tran_qua[i][j][t]<=expr);
    //printf("Tran_qua%d%d%d=%d\texpr=%d\t",i,j,t,d[j][t],expr);
    expr.end();
    }
    }
    }

    //constraints:demand satisfaction
    for (j = 1; j <=nCustm; j++) {
    for (t =1; t <=nTime; t++) {
    IloExpr expr(env);
    for (i =1; i <=nPlant; i++)
    {
    expr+=Tran_qua[i][j][t];
    }
    model.add(d[j][t]<=expr);
    //printf("d%d%d=%d\texpr=%d\t",j,t,d[j][t],expr);
    expr.end();
    }
    }

    for (i = 1; i <=nPlant; i++){
    for (t =1; t <=nTime; t++) {
    model.add(Product_qua[i][t]<=y[i][t]*Infin);
    }
    }

    IloCplex cplex(model);
    cplex.solve();

    // Print results
    env.out() << "Cost:" << cplex.getObjValue() << endl;

    env.out() << "Production quantity\tInventory quantity in plant i in period t:" << endl;
    for (i = 1; i <=nPlant; i++){
    for (t = 1; t <=nTime; t++) {
    env.out() << '\t' << i
    << '\t' << t
    << '\t' << cplex.getValue(Product_qua[i][t])
    << '\t' << cplex.getValue(Invent_qua[i][t])<< endl;
    }
    }
    env.out() << "Transportation quantity from plant i to customer j in period t:" << endl;
    for (i = 1; i <=nPlant; i++){
    for (j = 1; j <=nCustm; j++) {
    for (t = 1; t <=nTime; t++) {
    env.out()<< '\t' << i
    << '\t' << j
    << '\t' << t
    << '\t' <<cplex.getValue(Tran_qua[i][j][t]) << endl;
    }
    }
    }
    env.out() << "Number of vehicles with type l required from plant i to customer j in period t : " << endl;
    for (i =1; i <=nPlant; i++) {
    for (j =1; j <=nCustm; j++) {
    for (l =1; l <=nVehic; l++) {
    for (t =1; t <=nTime; t++){
    env.out()<< '\t' << i
    << '\t' << j
    << '\t' << l
    << '\t' << t
    << '\t' <<cplex.getValue(Vehic_num[i][j][l][t]) << endl;
    }
    }
    }
    }

    env.end();
    return 0;
    system("pause");
    }
    #DecisionOptimization
    #OPLusingCPLEXOptimizer