MPL OptiMax Samples

 

Microsoft J++ - Model results displayed using System.out


import optimax.*;
import com.ms.com.*;
import com.ms.lang.*;

public class OptimaxDemo extends Object {

    public static void main(String[] args) {
        optimax.IOptiMax        MPL;
        optimax.ISolver         cpxSolver;
        optimax.IModel          planModel;
        optimax.IVariableVector varVect;
        optimax.ISubscript      subProduct;
        optimax.ISubscript      subMonth;

        Variant modelName;
        Variant varVectName;
        Variant subscrName1;
        Variant subscrName2;

        try {
            MPL = (optimax.IOptiMax)new optimax.OptiMax();
            MPL.putWorkingDirectory("c:\\mplwin4");
            cpxSolver = MPL.getSolvers().Add("cplex65.dll");

            modelName = new Variant("planning");
            planModel = MPL.getModels().Add(modelName);

            planModel.ReadModel("planning.mpl");
            planModel.Solve(cpxSolver);

            System.out.println("");
            System.out.println("PLANNING.MPL: "
                                       + planModel.getSolution().getResultString());
            System.out.println("");
            System.out.println("Objective value = "
                                       + planModel.getSolution().getObjectValue());
            System.out.println("");

            varVectName = new Variant("Production");
            varVect = planModel.getVariableVectors().getItem(varVectName);
            subscrName1 = new Variant("product");
            subProduct = varVect.getSubscripts().getItem(subscrName1);
            subscrName2 = new Variant("month");
            subMonth = varVect.getSubscripts().getItem(subscrName2);

            System.out.println("  product   month   Produce");
            System.out.println(" ---------------------------");
            varVect.MoveFirstPos();
            while (varVect.getPosValid() != 0) {
                System.out.print("     " + subProduct.getValue());
                System.out.print("       " + subMonth.getValueName().substring(0,3));
                System.out.println("    " + varVect.getVariable().getActivity());
                varVect.MoveNextPos();
            }
            System.out.println(" ---------------------------");
        } catch (Error e) {
            e.printStackTrace();
        }
    }
}