CSsharp Example:
using System; using Maximal.OptiMax; namespace Sudoku { class MplSudoku { private int[] _solValues; private string _resultString; public MplSudoku() { _solValues = null; _resultString = ""; } private static int[] setupDataBoxAssign(int size) { double box = Math.Sqrt(size); int[] BoxAssign = new int[size * size]; for (int i = 0; i < size; i++) { int row = (int)(box * Math.Floor(Convert.ToDouble(i) / box)); for (int j = 0; j < size; j++) { int col = (int)Math.Ceiling(Convert.ToDouble(j + 1) / box); BoxAssign[i * size + j] = row + col; } } return BoxAssign; } private ResultType loadModel(Model model, int size, int[] fixedValues) { ResultType result; try { IndexSet idxRow = model.IndexSets.Add("row", 1, size); model.IndexSets.Add("coln", idxRow); model.IndexSets.Add("num", 1, size); model.IndexSets.Add("box", 1, size); model.DataVectors.Add("FixedValues[row,coln]", fixedValues); model.DataVectors.Add("BoxAssign[row,coln]", setupDataBoxAssign(size)); model.IndexSets.Add("BoxComp[row,coln,box] WHERE (BoxAssign = box)"); model.VariableVectors.Add("Assign[row,coln,num]", VariableType.Binary); string mplSource = "MODEL\n" + "MIN TotalAssign = SUM(row,coln,num: Assign);\n" + "SUBJECT TO\n" + "OneValue[row,coln]: SUM(num: Assign) = 1;\n" + "OneValueOnY[row,num]: SUM(coln: Assign) = 1;\n" + "OneValueOnX[coln,num]: SUM(row: Assign) = 1;\n" + "FixValue[row,coln,num] WHERE (FixedValues = num): num*Assign = FixedValues;\n" + "OneValueBox[box,num]: SUM(row,coln IN BoxComp: Assign) = 1;\n" + "END\n"; result = model.Parse(mplSource); } catch { result = model.LastResult; } return result; } public bool SolveModel(string solverName, int size, int[] fixedValues) { OptiMax mpl = new OptiMax(); Solver solver = mpl.Solvers.Add(solverName); if (solver == null) { _resultString = "Solver " + solverName + " was not found."; return false; } Model model = mpl.Models.Add("Sudoku"); model.UseExceptions = true; ResultType result = loadModel(model, size, fixedValues); if (result != ResultType.Success) { _resultString = model.Error.ToString(); model.ClearModel(); return false; } model.UseExceptions = false; result = model.Solve(solver); if (result != ResultType.Success) { _resultString = "Solver " + solver.Name + ": " + model.ResultString + "\n" + model.Solution.ResultString; model.ClearModel(); return false; } _resultString = model.Solution.ResultString; _solValues = getSolutionValues(model, size); model.ClearModel(); return true; } private int[] getSolutionValues(Model model, int size) { int[] solValues = new int[size * size]; VariableVector assignVarVect = model.VariableVectors["Assign"]; foreach (Variable var in assignVarVect.NonzeroVariables) { int row = assignVarVect.Subscripts[1].Value; int col = assignVarVect.Subscripts[2].Value; int val = assignVarVect.Subscripts[3].Value; int nr = ((row - 1) * size) + (col - 1); solValues[nr] = val; } return solValues; } public int[] SolValues { get { return _solValues; } } public string ResultString { get { return _resultString; } } } }