MPL OptiMax Samples

 

Microsoft VC++ - OptiMax called using standard COM interface pointers



void SolveModelInterface(void)
{
    USES_CONVERSION;
    IOptiMax*          pMpl          = NULL;
    ISolvers*          pSolvers      = NULL;
    ISolver*           pSolver       = NULL;
    IModels*           pModels       = NULL;
    IModel*            pModel        = NULL;
    IMatrix*           pMatrix       = NULL;
    IVariables*        pVars         = NULL;
    IConstraints*      pCons         = NULL;
    ISolution*         pSol          = NULL;
    IVariableVectors*  pVarVectors   = NULL;
    IVariableVector*   pVarVect      = NULL;
    IVariable*         pVar          = NULL;

    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;

    HRESULT hr;
    int i = 0;
    char *ModelFilename = "c:\\mplwin4\\plan.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);
        goto SOLVER_ADD_FAILED;
    }

    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("plan.mpl")

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

    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);
        goto MODEL_SOLVE_FAILED;
    }

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

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

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

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

    pVarVect->Release();

MODEL_GETVARVECTOR_FAILED:
    pVarVectors->Release();
    pSol->Release();

MODEL_SOLVE_FAILED:
    pVars->Release();
    pCons->Release();
    pMatrix->Release();

MODEL_READMODEL_FAILED:
    pModel->Release();
    pModels->Release();
    pSolver->Release();

SOLVER_ADD_FAILED:
    pSolvers->Release();
    pMpl->Release();
    CoUninitialize();
}