CSsharp Example:
using System; using System.Collections.Generic; using Maximal.OptiMax; namespace CirclePack { class MplCirclePack { private string _resultString; private double[] _xValues; private double[] _yValues; private double _radius; public MplCirclePack() { _resultString = ""; _xValues = new double[0]; _yValues = new double[0]; _radius = 0.0; } private ResultType loadModel(Model model, int circleCount) { try { model.IndexSets.Add("i", 1, circleCount); model.IndexSets.AddAlias("j", "i"); Variable varRadius = model.PlainVariables.Add("Radius"); VariableVector varX = model.VariableVectors.Add("x[i]", VariableType.Free); VariableVector varY = model.VariableVectors.Add("y[i]", VariableType.Free); varRadius.UpperBound = 1.0; varX.LowerBound = -1.0; varX.UpperBound = 1.0; varY.LowerBound = -1.0; varY.UpperBound = 1.0; model.Objectives.Add("Radius", ObjectSense.Maximize); model.ConstraintVectors.Add("NoOverlap[i,j]", "2 * Radius <= ((x[i] - x[i:=j])^2 + (y[i] - y[i:=j])^2)^0.5", "WHERE(i < j)"); model.ConstraintVectors.Add("InBoundsA[i]", "x + Radius <= 1"); model.ConstraintVectors.Add("InBoundsB[i]", "x - Radius >= -1"); model.ConstraintVectors.Add("InBoundsC[i]", "y + Radius <= 1"); model.ConstraintVectors.Add("InBoundsD[i]", "y - Radius >= -1"); } catch (Exception ex) { Console.WriteLine(ex.Message); } return model.LastResult; } public bool SolveModel(string solverName, int numCircles) { OptiMax mpl = new OptiMax(); Model model = mpl.Models.Add("CirclePack"); mpl.Options["ModelType"].ValueStr = "Nonlinear"; mpl.Options["ParserType"].ValueStr = "Extended"; Solver solver = mpl.Solvers.Add(solverName); if (solver == null) { _resultString = "Solver " + solverName + " was not found."; return false; } ResultType result = loadModel(model, numCircles); if (result != ResultType.Success) { _resultString = model.Error.ToString(); return false; } result = model.Solve(); if (result != ResultType.Success) { _resultString = "Solver " + solver.Name + ": " + model.ResultString + "\n" + model.Solution.ResultString; return false; } _resultString = model.Solution.ResultString; getSolutionValues(model); return true; } private void getSolutionValues(Model model) { _radius = model.Solution.ObjectValue; List<double> xList = new List<double>(); List<double> yList = new List<double>(); foreach (Variable xVar in model.VariableVectors["x"]) { xList.Add(xVar.Activity); } foreach (Variable yVar in model.VariableVectors["y"]) { yList.Add(yVar.Activity); } _xValues = xList.ToArray(); _yValues = yList.ToArray(); } public string ResultString { get { return _resultString; } } public double Radius { get { return _radius; } } public double[] XValues { get { return _xValues; } } public double[] YValues { get { return _yValues; } } } }