1 #include "ICoinMP.hpp"
2 #include <stdio.h>
3 
4 PCOIN global_pCoin;
5 
IterCallback(int IterCount,double ObjectValue,int IsFeasible,double InfeasValue)6 int SOLVCALL IterCallback(int    IterCount,
7 			double ObjectValue,
8 			int    IsFeasible,
9 			double InfeasValue)
10 {
11 	fprintf(stdout, "ITER: iter=%d, obj=%lg, feas=%d, infeas=%lg\n",
12 		IterCount, ObjectValue, IsFeasible, InfeasValue);
13 	return 0;
14 }
15 
MipNodeCallback(int IterCount,int MipNodeCount,double BestBound,double BestInteger,int IsMipImproved)16 int SOLVCALL MipNodeCallback(int    IterCount,
17 				int	  MipNodeCount,
18 				double BestBound,
19 				double BestInteger,
20 				int    IsMipImproved)
21 {
22 	fprintf(stdout, "NODE: iter=%d, node=%d, bound=%lg, best=%lg, %s\n",
23 		IterCount, MipNodeCount, BestBound, BestInteger, IsMipImproved ? "Improved" : "");
24 	return 0;
25 }
26 
CoinCreateProblem(const char * ProblemName,IClpSimplex * clpmodel)27 SOLVAPI HPROB SOLVCALL CoinCreateProblem(const char* ProblemName, IClpSimplex* clpmodel)
28 {
29     PCOIN pCoin;
30 
31     pCoin = (PCOIN) malloc(sizeof(COININFO));
32     global_pCoin = pCoin;
33     pCoin->clp = clpmodel; //new ClpSimplex();
34     pCoin->clp_presolve = new ClpSolve();
35     pCoin->osi = new OsiClpSolverInterface(pCoin->clp);
36     pCoin->cbc = NULL;  /* ERRORFIX 2/22/05: Crashes if not NULL when trying to set message handler */
37     pCoin->CbcMain0Already = 0;
38 
39     pCoin->msghandler = NULL;
40     pCoin->iterhandler = NULL;
41     pCoin->nodehandler = NULL;
42 
43     pCoin->LoadNamesType = SOLV_LOADNAMES_LIST;
44 
45     strcpy(pCoin->ProblemName, ProblemName);
46 
47     pCoin->ColCount    = 0;
48     pCoin->RowCount    = 0;
49     pCoin->NZCount     = 0;
50     pCoin->RangeCount  = 0;
51     pCoin->ObjectSense = 0;
52     pCoin->ObjectConst = 0.0;
53 
54     pCoin->lenColNamesBuf   = 0;
55     pCoin->lenRowNamesBuf   = 0;
56     pCoin->lenObjNameBuf = 0;
57 
58     pCoin->ObjectCoeffs = NULL;
59     pCoin->RHSValues    = NULL;
60     pCoin->RangeValues  = NULL;
61     pCoin->RowType      = NULL;
62     pCoin->MatrixBegin  = NULL;
63     pCoin->MatrixCount  = NULL;
64     pCoin->MatrixIndex  = NULL;
65     pCoin->MatrixValues = NULL;
66     pCoin->LowerBounds  = NULL;
67     pCoin->UpperBounds  = NULL;
68     pCoin->ColNamesBuf  = NULL;
69     pCoin->RowNamesBuf  = NULL;
70     pCoin->ColNamesList = NULL;
71     pCoin->RowNamesList = NULL;
72     pCoin->ObjectName   = NULL;
73 
74     pCoin->InitValues   = NULL;
75 
76     pCoin->RowLower     = NULL;
77     pCoin->RowUpper     = NULL;
78 
79     pCoin->ColType      = NULL;
80 
81     pCoin->SolveAsMIP   = 0;
82     pCoin->IntCount     = 0;
83     pCoin->BinCount     = 0;
84     pCoin->numInts      = 0;
85     pCoin->IsInt        = NULL;
86 
87     pCoin->SosCount     = 0;
88     pCoin->SosNZCount   = 0;
89     pCoin->SosType      = NULL;
90     pCoin->SosPrior     = NULL;
91     pCoin->SosBegin     = NULL;
92     pCoin->SosIndex     = NULL;
93     pCoin->SosRef       = NULL;
94 
95     pCoin->PriorCount   = 0;
96     pCoin->PriorIndex   = NULL;
97     pCoin->PriorValues  = NULL;
98     pCoin->BranchDir    = NULL;
99 
100     pCoin->SolutionStatus = 0;
101     strcpy(pCoin->SolutionText, "");
102 
103     pCoin->MessageLogCallback = NULL;
104     pCoin->IterationCallback = NULL;
105     pCoin->MipNodeCallback = NULL;
106 
107     return (HPROB)pCoin;
108 }
109 
110 //void RunTestProblem(char* problemName, double optimalValue, int colCount, int rowCount,
111 //	  int nonZeroCount, int rangeCount, int objectSense, double objectConst, double* objectCoeffs,
112 //	  double* lowerBounds, double* upperBounds, char* rowType, double* rhsValues, double* rangeValues,
113 //	  int* matrixBegin, int* matrixCount, int* matrixIndex, double* matrixValues, char** colNames,
114 //	  char** rowNames, char* objectName, double* initValues, char* columnType, int LoadNamesType)
SolveMIP(char * problemName,char * columnType,IClpSimplex * clpmodel)115 void SolveMIP(char* problemName, char* columnType, IClpSimplex* clpmodel)
116 {
117 	HPROB hProb;
118 	int result;
119 	char filename[260];
120     std::cout << "col0 = " << columnType[0] << std::endl;
121     std::cout << "col1 = " << columnType[1] << std::endl;
122     std::cout << "col2 = " << columnType[2] << std::endl;
123     std::cout << "col3 = " << columnType[3] << std::endl;
124 
125 	fprintf(stdout, "Solve Problem: %s\n", problemName);
126 	hProb = CoinCreateProblem(problemName, clpmodel);
127 //	if (LoadNamesType > 0) {
128 //		result = CoinSetLoadNamesType(hProb, LoadNamesType);
129 //	}
130 //	result = CoinLoadProblem(hProb, colCount, rowCount, nonZeroCount, rangeCount,
131 //					objectSense, objectConst, objectCoeffs, lowerBounds, upperBounds,
132 //					rowType, rhsValues, rangeValues, matrixBegin, matrixCount,
133 //					matrixIndex, matrixValues, colNames, rowNames, objectName);
134 	if (columnType) {
135 		result = CoinLoadInteger(hProb, columnType);
136 	}
137     result = CoinCheckProblem(hProb);
138     if (result != SOLV_CALL_SUCCESS) {
139 		fprintf(stdout, "Check Problem failed (result = %d)\n", result);
140 	}
141 	//result = CoinSetMsgLogCallback(hProb, &MsgLogCallback);
142 	if (!columnType)
143 		result = CoinSetIterCallback(hProb, &IterCallback);
144 	else {
145 		result = CoinSetMipNodeCallback(hProb, &MipNodeCallback);
146 	}
147 	result = CoinOptimizeProblem(hProb, 0);
148 	//strcpy(filename, problemName);
149 	//strcat(filename, ".mps");
150 	//result = CoinWriteFile(hProb, SOLV_FILE_MPS, filename);
151 	//GetAndCheckSolution(optimalValue, hProb);
152 	CoinUnloadProblem(hProb);
153 }
154 
155