Any problems with the code below? It showed "Failed" and "Error: Unknown exception caught!" in the execution window. Thanks for any help.

#include<ilcplex/ilocplex.h>
#include<math.h>
#include<iostream>
#include<fstream>

using namespace std;

ILOSTLBEGIN

typedef IloArray<IloNumVarArray>IloNumVarArray2;

int main(int argc, char **argv)

{

    IloEnv env;

    try{
        const char* filename="instance 1.txt";
        if(argc>=2)
            filename=argv[1];
        ifstream file(filename);

        if(!file){
            cerr<<"No such file: "<<filename<<endl;
            throw(-1);
        }

        char s[100];
        IloInt N, T;
        IloNum Q, inventPmax, f, K, C, V, hP, inv0;
        IloNumArray hC(env), inventCmax(env);
        IloNumArray2 dem(env), dist(env), eit(env);

        file>>s>>N>>s>>T>>s>>C>>s>>f>>s>>V>>s>>K>>s>>Q>>s>>hP>>s>>inventPmax>>s>>inv0
            >>s>>hC>>s>>inventCmax>>s>>dist>>s>>dem>>s>>eit;

        for(int c=1; c<=N; c++)
            file>>hC[c];

        for(int c=1; c<=N; c++)
            file>>inventCmax[c];

        for(int c1=0; c1<=N; c1++){
            for(int c2=0; c2<=N; c2++){
                file>>dist;
            }
        }

        for(int c=1; c<=N; c++){
            for(int t=1; t<=T; t++){
                file>>dem;
            }
        }

        for(int c=1; c<=N; c++){
            for(int t=1; t<=T; t++){
                file>>eit;
            }
        }file.close();

        IloInt c, t, t0, t1, t2;

        IloModel model(env);

        //Decision Variables

        IloNumVarArray inventP(env, T+1, 0, inventPmax);

        IloNumVarArray zP(env, T+1, 0, 1, ILOBOOL);

        IloNumVarArray p(env, T+1, 0, IloInfinity);

        IloNumVarArray2 inventC(env), w(env);

        for(c=0; c<=N; c++){
            inventC.add(IloNumVarArray(env, T+1, 0, inventCmax[c]));
            w.add(IloNumVarArray(env, T+1, 0, IloInfinity, ILOINT));
        }

        //Objective Function

        IloExpr expr1(env), expr2(env), expr3(env), expr4(env);

        for(t=1; t<=T; t++){
            expr1+=f*zP[t];
        }

        for(c=1; c<=N; c++){
            for(t=1; t<=T; t++){
                expr2+=eit[c][t]*w[c][t];
            }
        }

        for(t1=0; t1<=T-1; t1++){
            expr3+=hP*inventP[t1];
        }

        for(c=1; c<=N; c++){
            for(t2=1; t2<=T-1; t2++){
                expr4+=hC[c]*inventC[c][t2];
            }
        }

        model.add(IloMinimize(env, expr1+expr2+expr3+expr4));

        expr1.end(); expr2.end(); expr3.end(); expr4.end();

        //Non-negativity constraints

        for(int i=0; i<=N; i++){
            model.add(inventP[T]==0);
            model.add(inventC[i][0]==0); //set initial inventory to 0
            model.add(inventC[i][T]==0);
        }

        for(t2=1; t2<=T-1; t2++){
            model.add(inventP[t2]>=0);
            model.add(inventP[t2]<=inventPmax);
        }

        for(t0=0; t0<=T; t0++){
            model.add(p[t0]>=0);
        }

        for(int t=1; t<=T; t++){
            for(int i=1; i<=N; i++){
                model.add(inventC[i][t]>=0);
            }
        }

        //Constraints

        IloExpr cexpr1(env);

        for(t=1; t<=T; t++){
            for(c=1; c<=N; c++){
                //model.add(w[c][t]>=0);
                cexpr1+=w[c][t];
            }
            //model.add(inventP[T]==0);
            model.add(inventP[t]==inventP[t-1]+p[t]-cexpr1);
            //total amount available for delivery in day t is limited by inventory at factory on day t-1
            model.add(cexpr1<=inventP[t-1]);
            //limit the total amount that can be delivered on day t to a fixed percentage of transportation capacity
            model.add(cexpr1<=0.8*Q*K);
        }
        cexpr1.end();

        for(int t1=0; t1<=T-1; t1++){
            model.add(p[t1]<=C*zP[t1]);
        }

        //allows production on day 0 to ensure the demand on day 1 can be met
        IloExpr cexpr2(env);
        for(int i=0; i<=N; i++){
            cexpr2+=dem[i][1]-inventC[i][0];
        }
        model.add(p[0]>=cexpr2);
        cexpr2.end();

        //inventory flow balance equations
        for(int t=1; t<=T; t++){
            for(int i=1; i<=N; i++){
                //model.add(inventC[i][0]==0); //set initial inventory to 0
                //model.add(inventC[i][T]==0);
                //end inventory is 0
                model.add(inventC[i][t]<=inventCmax[i]);
                //model.add(inventC[i][t]>=0);
                model.add(inventC[i][t]==inventC[i][t-1]+w[i][t]-dem[i][t]);
            }
        }

        IloCplex cplex(env);
        cplex.extract(model);
        cplex.solve();
        if(cplex.solve()){
            cout<<"SUCCESS"<<endl;
        }

        if(!cplex.solve()){
            env.error()<<"Failed"<<endl;
            throw(-1);
        }
        cplex.exportModel("allocation_model.lp"); //writing lp file
    /*  std::ofstream out("allocation_model.results"); //output file
        out<<endl<<"Minimum Cost = "<<cplex.getObjValue()<<endl;
        env.out()<<endl<<"Minimum Cost = "<<cplex.getObjValue()<<endl;*/

        inventP.end();
        for(c=0; c<=N; c++) inventC[c].end();
        inventC.end();
        zP.end();
        p.end();
        for(c=0; c<=N; c++) w[c].end();
        w.end();
    }
    catch(IloException& e){
        cerr<<"Error: "<<e<<endl;
    }
    catch(...){
        cerr<<"Error: Unknown exception caught!"<<endl;
    }
    env.end();
    system("pause");
    return 0;
}

asked 19 May '14, 03:34

dickylim's gravatar image

dickylim
93
accept rate: 0%

edited 19 May '14, 04:26

fbahr's gravatar image

fbahr ♦
4.6k716

1

Try printing the unknown exception. That might make it known.

(19 May '14, 16:28) Paul Rubin ♦♦

I don't think people will read your code as is, it's too long and you don't explain what it does or bring your own considerations of possible issues.

Did you use a memory checker (valgrind or similar) on it, to check you don't have memory overwrites ?

Did you print out the model in a readable format and checked the model on paper looks like you had in mind ?

Did you find out which function is throwing ? I would expect cplex not to throw a unexpected exception, they probably have named exception for each type.

I just glanced the code and you have both 0 and 1 based indexing, which is not advisable to mix.

link

answered 19 May '14, 03:42

Bo%20Jensen's gravatar image

Bo Jensen ♦
5.0k2919
accept rate: 14%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×191

Asked: 19 May '14, 03:34

Seen: 1,469 times

Last updated: 19 May '14, 16:28

OR-Exchange! Your site for questions, answers, and announcements about operations research.