mplCutStock.cs

CSsharp Example:


using System;
using System.Collections.Generic;
using Maximal.OptiMax;


namespace CutStock
{
   class MplCutStock
   {

      private string _resultString;
      private double _totalCost;
      private int _sheetsCut;
      private int _totalSheets;
      private int _totalWaste;
      private int[] _solPatternCount;
      private string[] _solPatternNames;


      public MplCutStock()
      {
         _resultString = "";
         _totalCost = 0.0;
         _sheetsCut = 0;
         _totalSheets = 0;
         _totalWaste = 0;
         _solPatternCount = new int[0];
         _solPatternNames = new string[0];
      }



      private ResultType loadModel(Model model, double sheetPrice, int sheetsAvail,
                            int numWidths, int[] cutWidths, int[] cutDemands,
                            int numPatterns, int[] patternWaste, int[,] patternTable)
      {
         try
         {
            model.IndexSets.Add("patterns", "p", 1, numPatterns);
            model.IndexSets.Add("cuts", "w", 1, numWidths);

            model.DataConstants.Add("PriceSheet", sheetPrice);
            model.DataConstants.Add("SheetsAvail", sheetsAvail);

            model.DataVectors.Add("CutWidths[cuts]", cutWidths);
            model.DataVectors.Add("CutDemand[cuts]", cutDemands);
            model.DataVectors.Add("PatWaste[patterns]", patternWaste);
            model.DataVectors.Add("CutsInPattern[patterns, cuts]", patternTable);

            model.PlainVariables.Add("SheetsCut", "-> T1");
            model.PlainVariables.Add("TotalCost", "-> TC");

            model.VariableVectors.Add("PatternCount[patterns]", "-> \"\"");
            model.VariableVectors.Add("ExcessCuts[cuts]", "-> X");

            model.Objectives.Add("Obj", ObjectSense.Minimize, "TotalCost");

            model.PlainConstraints.Add("TotCost", "TotalCost = PriceSheet*SheetsCut");
            model.PlainConstraints.Add("RawAvail", "SheetsCut < SheetsAvail");
            model.PlainConstraints.Add("Sheets", "SheetsCut = SUM(patterns: PatternCount[patterns])");

            model.ConstraintVectors.Add("CutReq[cuts]",
               "SUM(patterns: CutsInPattern[patterns, cuts] * PatternCount[patterns]) = CutDemand[cuts] + ExcessCuts[cuts]");
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
         }
         return model.LastResult;
      }



      public bool SolveModel(string solverName, double priceSheet, int sheetsAvail,
         int numWidths, int[] cutWidths, int[] cutDemands,
         int numPatterns, int[] patternWaste, int[,] patternTable)
      {
         OptiMax mpl = new OptiMax();
         Model model = mpl.Models.Add("CutStock");
         Solver solver = mpl.Solvers.Add(solverName);
         if (solver == null)
         {
            _resultString = "Solver " + solverName + " was not found.";
            return false;
         }
         model.WorkingDirectory = mpl.ModelDirectory;
         ResultType result = loadModel(model, priceSheet, sheetsAvail, numWidths, cutWidths,
                                cutDemands, numPatterns, patternWaste, patternTable);
         if (result != ResultType.Success)
         {
            _resultString = model.Error.ToString();
            return false;
         }
         result = model.Solve(solver);
         if (result != ResultType.Success)
         {
            _resultString = "Solver " + solver.Name + ": " + model.ResultString + "\n" + model.Solution.ResultString;
            return false;
         }
         _resultString = model.Solution.ResultString;
         getSolutionValues(model);
         computeSolutionTotals(_solPatternCount, patternWaste);
         return true;
      }


      private void getSolutionValues(Model model)
      {
         _totalCost = model.Solution.ObjectValue;
         Variable sheetsCutVar = model.PlainVariables["SheetsCut"];
         _sheetsCut = Convert.ToInt32(sheetsCutVar.Activity);

         List<string> solPatternNames = new List<string>();
         List<int> solPatterns = new List<int>();
         VariableVector patternCountVect = model.VariableVectors["PatternCount"];
         foreach (Variable var in patternCountVect)
         {
            solPatternNames.Add(patternCountVect.Subscripts[1].ValueStr);
            solPatterns.Add(Convert.ToInt32(var.Activity));
         }
         _solPatternNames = solPatternNames.ToArray();
         _solPatternCount = solPatterns.ToArray();
      }


      private void computeSolutionTotals(int[] patternCount, int[] patternWaste)
      {
         _totalSheets = 0;
         _totalWaste = 0;
         for (int p = 0; p < patternCount.Length; p++)
         {
            if (patternCount[p] > 0)
            {
               _totalSheets += patternCount[p];
               _totalWaste += patternWaste[p] * patternCount[p];
            }
         }
      }


      public string ResultString
      {
         get { return _resultString; }
      }

      public double TotalCost
      {
         get { return _totalCost; }
      }

      public int SheetsCut
      {
         get { return _sheetsCut; }
      }

      public int TotalSheets
      {
         get { return _totalSheets; }
      }

      public int TotalWaste
      {
         get { return _totalWaste; }
      }

      public int[] SolPatternCount
      {
         get { return _solPatternCount; }
      }

      public string[] SolPatternNames
      {
         get { return _solPatternNames; }
      }

   }
}


        

Back To Top | Maximal Home Page | List of Samples | Previous Page | Next Page