MPL OptiMax Samples

 

Microsoft VC++ - OptiMax called using the type library



#import "OptiMax.tlb"
using namespace MPLLib;


void SolveModelTypeLib(void)
{
    char *ModelFilename = "c:\\mplwin4\\planning.mpl";

    CoInitialize(NULL);
    try {
        IOptiMaxPtr    pMpl( __uuidof(MPLLib::OptiMax));

        ISolverPtr            pSolver;
        IModelPtr             pModel;
        IMatrixPtr            pMatrix;
        IVariablesPtr         pVars;
        IConstraintsPtr       pCons;
        ISolutionPtr          pSol;
        IVariableVectorPtr    pVarVector;
        IVariablePtr          pVar;

        long       result;

        pSolver = pMpl->Solvers->Add("c:\\mplwin4\\CPLEX65.dll");    // Set pSolver = MPL.Solvers.Add "CPLEX65.DLL"

        pModel = pMpl->Models->Add("Model1");                        // Set pModel = MPL.Models.Add("Model1")

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

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

        pMatrix = pModel->Matrix;                                    // Set pMatrix = pModel.Matrix
        pVars = pMatrix->Variables;                                  // Set pVars = pMatrix.Variables
        pCons = pMatrix->Constraints;                                // Set pCons = pMatrix.Constraints

        printf("MODEL: vars=%d, cons=%d, nz=%d, int=%d\n",
                        pVars->Count, pCons->Count, pMatrix->NonZeroCount, pVars->IntegerCount);

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

        pSol = pModel->Solution;                                     // Set pSol = pModel.Solution

        printf("SOLVE: obj=%.10lg, iter=%d, nodes=%d, result='%s'\n",
                         pSol->ObjectValue, pSol->IterationCount,
                         pSol->NodeCount, (LPCSTR)pSol->ResultString);

        pVarVector  = pModel->VariableVectors->GetItem("Produce");    // pVarVector = pModel.VariableVectors("Production")
        int i = 0;
        printf("PROD: ");
        pVar = pVarVector->MoveFirstPos();
        while (pVarVector->PosValid) {
            i++;
            printf(" [%d]=%.10lg,", i, pVar->Activity);              // pVar.Activity
            pVar = pVarVector->MoveNextPos();
        }
        printf("\n\n");
    }
    catch (const _com_error& Err) {
        printf("Error: %s  (0x%x)\n\n%s\n",
                (LPCSTR)Err.ErrorMessage(), Err.Error(), (LPCSTR)Err.Description());
    }
    CoUninitialize();
}