MPL OptiMax Samples

 

Microsoft VC++ - OptiMax called using smart pointers



void SolveModelSmartPtr(void)
{
    USES_CONVERSION;
    CComPtr<IOptiMax>      pMpl;
    CComPtr<ISolvers>      pSolvers;
    CComPtr<ISolver>       pSolver;
    CComPtr<IModels>       pModels;
    CComPtr<IModel>        pModel;
    CComPtr<IMatrix>       pMatrix;
    CComPtr<IVariables>    pVars;
    CComPtr<IConstraints>  pCons;
    CComPtr<ISolution>     pSol;
    CComPtr<IVariableVectors>  pVarVectors;
    CComPtr<IVariableVector>   pVarVect;
    CComPtr<IVariable>     pVar;
    HRESULT hr;

    CComBSTR     bStr;
    CComVariant  vtModelName;
    CComVariant  vtVarName;

    long       result;
    long       varCount;
    long       conCount;
    long       nzCount;
    long       intCount;

    char       resultStr[128];
    double     objectValue;
    long       iterCount;
    long       nodeCount;

    BOOL posValid;
    double varValue;
    char *ModelFilename = "c:\\mplwin4\\Tutorial\\planning4.mpl";

    CoInitialize(NULL);
    hr = CoCreateInstance(CLSID_OptiMax, NULL, CLSCTX_ALL, IID_IOptiMax, (void **)&pMpl);
    if (!SUCCEEDED(hr)) {
        printf("Initialize OptiMax failed (hr=%d)\n\n", hr);
        return;
    }
    bStr = "c:\\mplwin4\\CPLEX65.dll";
    pMpl->get_Solvers(&pSolvers);               // Set pSolvers = MPL.Solvers
    pSolvers->Add(bStr, &pSolver);              // Set pSolver = pSolvers.Add "CPLEX"

    if (!pSolver) {
        printf("Solvers.Add(\"%ls\") failed, solver not found!\n\n", bStr);
        return;
    }

    vtModelName = "Model1";
    pMpl->get_Models(&pModels);                 // Set pModels = MPL.Models
    pModels->Add(vtModelName, &pModel);         // Set pModel = pModels.Add("Model1")

    printf("READ:  '%s'\n", ModelFilename);
    bStr = ModelFilename;
    pModel->ReadModel(bStr, &result);    // Set result = pModel.ReadModel("capri.mpl")

    if (result) {
        printf("ReadModel(%s) failed (result=%d)\n\n", ModelFilename, result);
        return;
    }

    pModel->get_Matrix(&pMatrix);               // Set pMatrix = pModel.Matrix

    pMatrix->get_Variables(&pVars);             // Set pVars    = pMatrix.Variables
    pMatrix->get_Constraints(&pCons);           // Set pCons    = pMatrix.Constraints

    pVars->get_Count(&varCount);                // Set varCount = pVars.Count
    pCons->get_Count(&conCount);                // Set conCount = pCons.Count
    pMatrix->get_NonZeroCount(&nzCount);        // Set nzCount  = pMatrix.NonZeroCount
    pVars->get_IntegerCount(&intCount);         // Set intCount = pVars.IntegerCount

    printf("MODEL: vars=%d, cons=%d, nz=%d, int=%d\n",
                  varCount, conCount, nzCount, intCount);

    pModel->Solve(pSolver, &result);           // Set result = pModel.Solve(pSolver)
    if (result) {
        printf("Solve() failed (result=%d)\n\n", result);
        return;
    }

    pModel->get_Solution(&pSol);              // Set pSol        = pModel.Solution

    pSol->get_ObjectValue(&objectValue);      // Set objectValue = pSolution.ObjectValue
    pSol->get_IterationCount(&iterCount);     // set iterCount   = pSolution.IterationCount
    pSol->get_NodeCount(&nodeCount);          // Set nodeCount   = pSolution.NodeCount
    pSol->get_ResultString(&bStr);            // Set ResultStr   = pSolution.ResultString
    strcpy(resultStr, W2CA(bStr));

    printf("SOLVE: obj=%.10lg, iter=%d, nodes=%d, result='%s'\n",
                   objectValue, iterCount, nodeCount, resultStr);

   vtVarName = "Produce";
    pModel->get_VariableVectors(&pVarVectors);
    pVarVectors->get_Item(vtVarName, &pVarVect);
    if (!pVarVect) {
        printf("Could not find variable named '%s' in '%s'\n\n", "Produce", ModelFilename);
        return;
    }
    int i = 0;
    printf("PROD: ");
    pVar = NULL;
    pVarVect->MoveFirstPos(&pVar);
    pVarVect->get_PosValid(&posValid);
    while (posValid) {
        i++;
        pVar->get_Activity(&varValue);
        printf(" [%d]=%.10lg,", i, varValue);
        pVar = NULL;
        pVarVect->MoveNextPos(&pVar);
        pVarVect->get_PosValid(&posValid);
    }
    printf("\n\n");
    CoUninitialize();
}