public override object Solve(object solver) /* La construction de la matrice des données du problème n'est pas prise en charge par le solveur Coin */ { /* Celle-ci se fait au fur et à mesure qu'on ajoute des données par le biais du code de la fonction Solve() */ int ind = 0; double objectConst = 0.0; int NZCount = 0; int result; UpperBounds = new double[NUM_COLS]; LowerBounds = new double [NUM_COLS]; MatrixBegin = new int [NUM_COLS + 1]; MatrixCount = new int [NUM_COLS]; for (int i = 0; i < NUM_COLS; i++) { UpperBounds[i] = Double.PositiveInfinity; LowerBounds[i] = Double.NegativeInfinity; MatrixBegin[i] = i * 2; } MatrixBegin[NUM_COLS] = NUM_COLS * 2; for (int i = 0; i < NUM_COLS; i++) { int count = 0; for (int j = 0; j < NUM_ROWS; j++) { if (Matrix[j, i] != 0) { NZCount++; count++; } } MatrixCount[i] = count; } MatrixValues = new double[NZCount]; MatrixIndex = new int[NZCount]; for (int i = 0; i < NUM_COLS; i++) { for (int j = 0; j < NUM_ROWS; j++) { if (Matrix[j, i] != 0) { MatrixValues[ind] = Matrix[j, i]; MatrixIndex[ind] = j; ind++; } } } CoinMP.CoinLoadProblem((IntPtr)solver, NUM_COLS, NUM_ROWS, NZCount, 0, CoinMP.SOLV_OBJSENS_MAX, objectConst, ObjectCoeffs, LowerBounds, UpperBounds, RowTypes, RhsValues, RangeValues, MatrixBegin, MatrixCount, MatrixIndex, MatrixValues, ColNames, RowNames, ObjName); result = CoinMP.CoinOptimizeProblem((IntPtr)solver); result = CoinMP.CoinWriteFile((IntPtr)solver, CoinMP.SOLV_FILE_MPS, "Result_CoinMP"); return(CoinMP.CoinGetSolutionTextIntPtr((IntPtr)solver)); }
public override bool SetOutputFile(object solver, string filename) { if (CoinMP.CoinWriteFile((IntPtr)solver, CoinMP.SOLV_FILE_MPS, filename) == 0) { return(false); } return(true); }
public void GetAndCheckSolution(double optimalValue, IntPtr hProb) { int solutionStatus; String solutionText; double objectValue; int i; int colCount; double[] xValues; int result; int length; String colName; String problemName; logMsg.WriteLine("---------------------------------------------------------------"); problemName = CoinMP.CoinGetProblemName(hProb); solutionStatus = CoinMP.CoinGetSolutionStatus(hProb); solutionText = CoinMP.CoinGetSolutionText(hProb); objectValue = CoinMP.CoinGetObjectValue(hProb); logTxt.WriteLine("Problem Name: " + problemName); logTxt.WriteLine("Solution Result: " + solutionText); logTxt.WriteLine("Solution Status: " + solutionStatus); logTxt.WriteLine("Optimal Value: " + objectValue + " (" + optimalValue + ")"); logTxt.WriteLine("---------------------------------------------------------------"); colCount = CoinMP.CoinGetColCount(hProb); xValues = new double[colCount]; result = CoinMP.CoinGetSolutionValues(hProb, xValues, null, null, null); for (i = 0; i < colCount; i++) { if (xValues[i] != 0.0) { colName = CoinMP.CoinGetColName(hProb, i); logTxt.WriteLine(colName + " = " + xValues[i]); } } logTxt.WriteLine("---------------------------------------------------------------"); logTxt.NewLine(); if (solutionStatus != 0) { MessageBox.Show("status=" + solutionStatus, problemName); } if (optimalValue != 0.0) { if (Math.Abs(objectValue - optimalValue) >= 0.001) { MessageBox.Show("Obj=" + objectValue + " <> " + optimalValue, problemName); } } }
// Fonctions Solveurs// public override bool Init() { if (CoinMP.CoinInitSolver("") == 0) { return(false); } else { return(true); } }
public void RunProblem(string problemName, double optimalValue, int colCount, int rowCount, int nonZeroCount, int rangeCount, int objectSense, double objectConst, double[] objectCoeffs, double[] lowerBounds, double[] upperBounds, char[] rowType, double[] rhsValues, double[] rangeValues, int[] matrixBegin, int[] matrixCount, int[] matrixIndex, double[] matrixValues, string[] colNames, string[] rowNames, string objectName, double[] initValues, char[] colType) { IntPtr hProb; int result; logTxt.NewLine(); logTxt.WriteLine("Solve Problem: " + problemName + " (obj=" + optimalValue + ")"); logTxt.WriteLine("---------------------------------------------------------------"); hProb = CoinMP.CoinCreateProblem(problemName); result = CoinMP.CoinLoadMatrix(hProb, colCount, rowCount, nonZeroCount, rangeCount, objectSense, objectConst, objectCoeffs, lowerBounds, upperBounds, rowType, rhsValues, rangeValues, matrixBegin, matrixCount, matrixIndex, matrixValues); result = CoinMP.CoinLoadNames(hProb, colNames, rowNames, objectName); if (result != 0) { logTxt.WriteLine("CoinLoadProblem failed"); } if (colType != null) { result = CoinMP.CoinLoadInteger(hProb, colType); if (result != 0) { logTxt.WriteLine("CoinLoadInteger failed"); } } result = CoinMP.CoinCheckProblem(hProb); if (result != 0) { logTxt.WriteLine("Check Problem failed (result = " + result + ")"); } CoinMP.MsgLogDelegate MsgLogDelegate = new CoinMP.MsgLogDelegate(MsgLogCallback); result = CoinMP.CoinSetMsgLogCallback(hProb, MsgLogDelegate); result = CoinMP.CoinOptimizeProblem(hProb); result = CoinMP.CoinWriteFile(hProb, CoinMP.SOLV_FILE_MPS, problemName); GetAndCheckSolution(optimalValue, hProb); result = CoinMP.CoinUnloadProblem(hProb); }
public formCoinMP() { int result; int length; double version; StringBuilder solverName = new StringBuilder(100); InitializeComponent(); result = CoinMP.CoinInitSolver(""); length = CoinMP.CoinGetSolverNameBuf(solverName, solverName.Capacity); version = CoinMP.CoinGetVersion(); LogHandler.EmptyText(txtLog); LogHandler.WriteLine(txtLog, "Solver: " + solverName); LogHandler.WriteLine(txtLog, "Version: " + version); }
public override object CreateProblem(int columns) { NUM_COLS = columns; NUM_ROWS = 0; ObjectCoeffs = new double[columns]; Matrix = new double[1, columns]; RowTypes = new char[1]; RhsValues = new double[1]; RangeValues = new double[1]; ColNames = new string[NUM_COLS]; RowNames = new string[1]; SosType = new int[1]; SosPrior = new int[1]; SosBegin = new int[1]; SosIndex = new int[1]; for (int i = 0; i < NUM_COLS; i++) { ColNames[i] = "C" + i; } return((IntPtr)CoinMP.CoinCreateProblem("Problem_CoinMP")); }
public override double GetObjective(object solver) { return(CoinMP.CoinGetObjectValue((IntPtr)solver)); }
public override int DeleteLp(object solver) { return(CoinMP.CoinFreeSolver()); }