Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only

Getting error in CreateCplex()

  • 1.  Getting error in CreateCplex()

    Posted 02/01/13 12:34 PM

    Originally posted by: SystemAdmin


    The below is the function which i call from another class.First time it works fine..
    Then second time is claaed again from another place.

    I get the error at cplex = oplF.CreateCplex();
    // try
    {
    oplF = new OplFactory();
    cplex = oplF.CreateCplex();
    OplFactory.DebugMode = false;
    }
    //
    Please help me.I refer the oplal dll from dot net.
    Error message - ILOG.CPLEX.Cppimpl.cplex_wrapPINVOKE.new_IloCplex__SWIG_0(IntPtr jarg1)
    at ILOG.CPLEX.Cppimpl.IloCplex..ctor(IloEnv env)
    at ILOG.CPLEX.Cplex..ctor(IloEnv env)
    at ILOG.OPL.OplFactory.CreateCplex()
    at Nissan_DS.CallingOPL.Main1(String sModelFileName, String sDatFileName) in C:\Nissan_DS\Nissan_DS\CallingOPL.cs:line 435

    *Function:*public void Main1(string sModelFileName, string sDatFileName)
    {

    GC.Collect();

    int iInfeasiblePlant1;
    int iInfeasiblePlant2;
    DataTable dtConstraint;
    DataTable dtOrder;
    string sQuery;
    string sErrorMsg = "";
    int iRtrn;
    int i;

    OplFactory oplF = null;
    OplErrorHandler errHandler = null;
    Cplex cplex = null;

    OplModelSource modelSource;
    OplSettings settings;
    OplModelDefinition oplDef;
    OplModel opl;
    OplDataSource dataSource;

    IIntVarMap infeasibleMap;
    IIntVarMap feasible1Map;
    int[] iConstraintArrayPlant1;
    int[] iConstraintArrayPlant2;
    DataTable dt;
    Single itransCost;
    int[] infeasibleOrderIdArray;
    int iOPLAssign = 0;
    StreamWriter filePointer;
    // Get the maximum production quantities per constraint and store in global arrays.
    sQuery = "SELECT Description, OCF, plant1, plant2 FROM constraint_table ORDER by OCF";
    dtConstraint = Nissan_DS.dbTool.GetTable1(sQuery, ref sErrorMsg);
    if (sErrorMsg != "")
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    iConstraintCount = dtConstraint.Rows.Count;

    constraintMaxQtyPlant1 = new intiConstraintCount;
    constraintMaxQtyPlant2 = new intiConstraintCount;
    for (i = 0; i < iConstraintCount; i++)
    {
    constraintMaxQtyPlant1[i] = Convert.ToInt32(dtConstraint.Rows[i]);
    constraintMaxQtyPlant2[i] = Convert.ToInt32(dtConstraint.Rows[i]);
    }
    // Get the orders.
    sQuery = "SELECT * FROM order_table order by ORDERID";
    dtOrder = Nissan_DS.dbTool.GetTable1(sQuery, ref sErrorMsg);
    if (sErrorMsg != "")
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    // Store the Order Ids in a global array
    iOrderCount = dtOrder.Rows.Count;
    sOrderIDArray = new stringiOrderCount;

    for (i = 0; i < iOrderCount; i++)
    {
    sOrderIDArray[i] = dtOrder.Rows[i].ToString();

    }

    // Store the Constraint names
    sOCFArray = new stringiConstraintCount;
    for (i = 0; i < iConstraintCount; i++)
    {
    sOCFArray[i] = dtOrder.Columnsi + 1.ToString();
    }
    //Write OPL model file
    #region HJX write model 1 file
    filePointer = new StreamWriter("C:\\Nissan_DS\\Nissan_DS\\DS_model" + iter + ".mod", false);
    {
    filePointer.WriteLine("{string} OrderID = ...;");
    filePointer.WriteLine("");
    filePointer.WriteLine("tuple orderType {");
    filePointer.WriteLine("string ID;");
    for (int ii = 0; ii < iConstraintCount; ii++)
    {
    filePointer.WriteLine("int " + sOCFArrayii + ";");
    }
    filePointer.WriteLine("float costplant1;");
    filePointer.WriteLine("float costplant2;");
    filePointer.WriteLine("}");
    filePointer.WriteLine("");
    filePointer.WriteLine("orderType OrderTableOrderID = ...;");
    filePointer.WriteLine("");
    filePointer.WriteLine("dvar boolean feasible1OrderID;");
    filePointer.WriteLine("dvar boolean feasible2OrderID;");
    filePointer.WriteLine("dvar boolean infeasibleOrderID;");
    filePointer.WriteLine("");
    filePointer.WriteLine("minimize");
    filePointer.WriteLine(" sum (p in OrderID) (feasible1[p]*OrderTable[p].costplant1 + feasible2[p]*OrderTable[p].costplant2 + infeasible[p]*" + frmEntInf.fInfeasibleCost + ");");
    filePointer.WriteLine("");
    filePointer.WriteLine("subject to {");
    filePointer.WriteLine("Onedecision:");
    filePointer.WriteLine("forall(p in OrderID)");
    filePointer.WriteLine("feasible1[p] + feasible2[p] + infeasible[p]== 1;");
    filePointer.WriteLine("");
    for (int it = 0; it < iConstraintCount; it++)
    {
    filePointer.WriteLine("sum(p in OrderID) feasible1[p]*OrderTable[p]." + sOCFArrayit + "<= " + constraintMaxQtyPlant1it + ";");
    filePointer.WriteLine("sum(p in OrderID) feasible2[p]*OrderTable[p]." + sOCFArrayit + "<= " + constraintMaxQtyPlant2it + ";");
    }
    filePointer.WriteLine("}");
    filePointer.Flush();
    filePointer.Close();
    }
    #endregion

    //Write to the OPL data file
    #region HJX write data file
    filePointer = new StreamWriter("C:\\Nissan_DS\\Nissan_DS\\DS_model" + iter + ".dat", false);
    {
    //Database connection
    sQuery = "DBconnection db(" + '"' + "odbc" + '"' + "," + '"' + "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\" + "\\";
    sQuery += "Nissan_DS\\" + "\\" + "Nissan_DS\\" + "\\" + "Nissan_DS.mdb//" + '"' + ");";

    //Start to write data file
    filePointer.WriteLine(sQuery);

    //Count all the attributes.
    sQuery = "OrderID,OrderTable from DBread(db," + '"' + "SELECT ORDERID,ORDERID,";
    for (i = 0; i < iConstraintCount; i++)
    {
    sQuery += sOCFArray[i] + ",";
    }
    sQuery = sQuery.Substring(0, sQuery.Length - 1);
    sQuery += ",costplant1,costplant2";
    sQuery += " FROM order_table";
    sQuery += " ORDER BY ORDERID" + '"' + ");";

    filePointer.WriteLine(sQuery);
    filePointer.Flush();
    filePointer.Close();
    }
    #endregion

    //Define an OPL factory class
    try
    {
    oplF = new OplFactory();
    cplex = oplF.CreateCplex();

    OplFactory.DebugMode = false;
    }
    catch (System.Exception ex)
    {
    string errorStack = ex.StackTrace;
    string errorMsg = ex.Message;
    }
    errHandler = oplF.CreateOplErrorHandler(Console.Out);

    modelSource = oplF.CreateOplModelSource(sModelFileName);
    settings = oplF.CreateOplSettings(errHandler);
    oplDef = oplF.CreateOplModelDefinition(modelSource, settings);
    opl = oplF.CreateOplModel(oplDef, cplex);
    dataSource = oplF.CreateOplDataSource(sDatFileName);
    opl.AddDataSource(dataSource);
    opl.Generate();

    infeasibleMap = opl.GetElement("infeasible").AsIntVarMap();
    feasible1Map = opl.GetElement("feasible1").AsIntVarMap();

    sAssignment = new stringiOrderCount;
    iConstraintArrayPlant1 = new intiConstraintCount;
    iConstraintArrayPlant2 = new intiConstraintCount;
    NbInfeasible = 0;
    itransCost = 0;
    if (cplex.Solve()) //OPL solves the Mix-binary model
    {
    MinCost = Convert.ToSingle(opl.Cplex.ObjValue); //record the min cost

    //count the number of infeasible orders
    infeasibleOrderIdArray = new intiOrderCount;

    iInfeasiblePlant1 = 0;
    iInfeasiblePlant2 = 0;

    for (int j = 0; j < iOrderCount; j++)
    {
    iOPLAssign = Convert.ToInt32(opl.Cplex.GetValue(feasible1Map.Get(sOrderIDArray[j])));
    infeasibleOrderIdArray[j] = Convert.ToInt32(opl.Cplex.GetValue(infeasibleMap.Get(sOrderIDArray[j])));

    if (infeasibleOrderIdArray[j] != 0)
    {
    sQuery = "Select costplant1,costplant2 from order_table where ORDERID ='" + sOrderIDArray[j] + "'";

    dt = Nissan_DS.dbTool.GetTable1(sQuery, ref sErrorMsg, true);
    if (sErrorMsg != "")
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    if (Convert.ToSingle(dt.Rows[0]) < Convert.ToSingle(dt.Rows[0]))
    {
    iInfeasiblePlant2++;
    }
    else
    {
    iInfeasiblePlant1++;
    }
    sAssignment[j] = "INF"; //Mark infeasible orders as infeasible
    }
    else
    {
    if (iOPLAssign == 1)
    {
    sAssignment[j] = "CP";
    for (int aj = 0; aj < iConstraintCount; aj++)
    {
    iConstraintArrayPlant1aj += Convert.ToInt32(dtOrder.Rows[j]aj + 1);
    }
    }
    else
    {
    sAssignment[j] = "SP";
    for (int aj = 0; aj < iConstraintCount; aj++)
    {
    iConstraintArrayPlant2aj += Convert.ToInt32(dtOrder.Rows[j]aj + 1);
    }
    }
    }
    NbInfeasible += infeasibleOrderIdArray[j];
    } // END FOR
    itransCost = MinCost - frmEntInf.fInfeasibleCost * NbInfeasible;
    //Read the results from tables and display on the form
    sQuery = "select Description, OCF,plant1, plant2 from constraint_table ORDER by OCF";
    dtConstraint = Nissan_DS.dbTool.GetTable1(sQuery, ref sErrorMsg);

    if (sErrorMsg != "")
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    // Ignore errors on the delete because the primary error is typically if the tables are already empty. If there
    // is a database error that will show on the next database call.
    sQuery = "Delete from result_table";
    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);

    for (i = 0; i < iConstraintCount; i++)
    {
    sQuery = "insert into result_table values ('" + dtConstraint.Rows[i] + "','" + dtConstraint.Rows[i] + "', " + iConstraintArrayPlant1[i];
    sQuery += ", " + dtConstraint.Rows[i] + ", 0, " + iConstraintArrayPlant2[i] + ", ";
    sQuery += dtConstraint.Rows[i] + ", 0)";

    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);
    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }
    }

    // Update Table infeasible with the results
    sQuery = "update infeasible set NofInfeasible = " + iInfeasiblePlant1 + " Where Plant = 'Plant1'";
    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);
    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    sQuery = "update infeasible set NofInfeasible = " + iInfeasiblePlant2 + " Where Plant = 'Plant2'";
    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);
    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    sQuery = "update infeasible set NofInfeasible = " + NbInfeasible + " Where Plant = 'Total'";
    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);
    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }
    } // end if cplex.Solve()
    else
    {
    MessageBox.Show("OPL could not find a feasible solution", "System reminder", MessageBoxButtons.OK);
    return;
    }
    frmMain.iMta = 1;
    iter++;
    cplex.End(); //added JWB 1/3/2013
    oplF.End();

    //Record in the scenario table if not final run
    if (frmScenario.iScenarioSelected != 1)
    {
    sQuery = "Insert into sce1(Totalcost, Transportation, No_of_Inf,ID) values(";
    sQuery += MinCost + ", " + itransCost + ", " + NbInfeasible + ", " + frmResultView.m1count + ")";

    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);
    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }

    for (i = 0; i < iConstraintCount; i++)
    {
    sQuery = "Insert into sce2(ID, PLANT1_LIMIT, PLANT2_LIMIT, OCF_CODE) values(";
    sQuery += frmResultView.m1count + "," + constraintMaxQtyPlant1[i] + ", " + constraintMaxQtyPlant2[i] + ", '" + dtConstraint.Rows[i].ToString() + "')";

    iRtrn = dbTool.doCreate1(sQuery, ref sErrorMsg);

    // If the query returns a zero, an error occurred. Display the error and abort the function.
    if (iRtrn == 0)
    {
    dbTool.displayError("OPL", "Aborting: No records found or an error occurred\nQuery:\n" + sQuery + "\n" + sErrorMsg);
    return;
    }
    }

    //Display the results if not the final run
    frmResultView f1 = new frmResultView();
    f1.Show();
    }
    else
    {
    // Generate the order change table.
    OrderAssign();
    }

    frmResultView.m1count++;

    GC.Collect(); // Added on 2/1/2013
    }
    #DecisionOptimization
    #OPLusingCPLEXOptimizer