forked from MarcelGosselin/LpSolveDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
311 lines (245 loc) · 12.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Diagnostics;
using System.Threading;
namespace LpSolveDotNet.Demo
{
class Program
{
[STAThread]
public static void Main()
{
Debug.WriteLine(Environment.CurrentDirectory);
LpSolve.Init();
Test();
//TestMultiThreads();
}
/* unsafe is needed to make sure that these function are not relocated in memory by the CLR. If that would happen, a crash occurs */
/* go to the project property page and in “configuration properties>build” set Allow Unsafe Code Blocks to True. */
/* see http://msdn2.microsoft.com/en-US/library/chfa2zb8.aspx and http://msdn2.microsoft.com/en-US/library/t2yzs44b.aspx */
private /* unsafe */ static void logfunc(IntPtr lp, IntPtr userhandle, string Buf)
{
Debug.Write(Buf);
}
private /* unsafe */ static bool ctrlcfunc(IntPtr lp, IntPtr userhandle)
{
// 'If set to true, then solve is aborted and returncode will indicate this.
return (false);
}
private /* unsafe */ static void msgfunc(IntPtr lp, IntPtr userhandle, lpsolve_msgmask message)
{
Debug.WriteLine(message);
}
private static void ThreadProc(object filename)
{
using (var lp = LpSolve.read_LP((string) filename, 0, ""))
{
lpsolve_return ret = lp.solve();
double o = lp.get_objective();
Debug.Assert(ret == lpsolve_return.OPTIMAL && Math.Round(o, 13) == 1779.4810350637485);
}
}
private static void TestMultiThreads()
{
Version version = LpSolve.LpSolveVersion;
for (int i = 1; i <= 5000; i++)
{
Thread myThread = new Thread(ThreadProc);
myThread.Start("ex4.lp");
}
Thread.Sleep(5000);
}
private static void Test()
{
const string NewLine = "\n";
double[] Row;
double[] Lower;
double[] Upper;
double[] Col;
double[] Arry;
using (var lp = LpSolve.make_lp(0, 4))
{
Version version = LpSolve.LpSolveVersion;
/* let's first demonstrate the logfunc callback feature */
lp.put_logfunc(logfunc, IntPtr.Zero);
lp.print_str("lp_solve " + version + " demo" + NewLine + NewLine);
lp.solve(); /* just to see that a message is send via the logfunc routine ... */
/* ok, that is enough, no more callback */
lp.put_logfunc(null, IntPtr.Zero);
/* Now redirect all output to a file */
lp.set_outputfile("result.txt");
/* set an abort function. Again optional */
lp.put_abortfunc(ctrlcfunc, IntPtr.Zero);
/* set a message function. Again optional */
lp.put_msgfunc(msgfunc, IntPtr.Zero, (int)(lpsolve_msgmask.MSG_PRESOLVE | lpsolve_msgmask.MSG_LPFEASIBLE | lpsolve_msgmask.MSG_LPOPTIMAL | lpsolve_msgmask.MSG_MILPEQUAL | lpsolve_msgmask.MSG_MILPFEASIBLE | lpsolve_msgmask.MSG_MILPBETTER));
lp.print_str("lp_solve " + version + " demo" + NewLine + NewLine);
lp.print_str("This demo will show most of the features of lp_solve " + version + NewLine);
lp.print_str(NewLine + "We start by creating a new problem with 4 variables and 0 constraints" + NewLine);
lp.print_str("We use: lp = LpSolve.make_lp(0, 4);" + NewLine);
lp.set_timeout(0);
lp.print_str("We can show the current problem with lp.print_lp();" + NewLine);
lp.print_lp();
lp.print_str("Now we add some constraints" + NewLine);
lp.print_str("lp.add_constraint(Row, lpsolve_constr_types.LE, 4);" + NewLine);
// pay attention to the 1 base and ignored 0 column for constraints
lp.add_constraint(new double[] { 0, 3, 2, 2, 1 }, lpsolve_constr_types.LE, 4);
lp.print_lp();
// check ROW array works
Row = new double[] { 0, 0, 4, 3, 1 };
lp.print_str("lp.add_constraint(Row, lpsolve_constr_types.GE, 3);" + NewLine);
lp.add_constraint(Row, lpsolve_constr_types.GE, 3);
lp.print_lp();
lp.print_str("Set the objective function" + NewLine);
lp.print_str("lp.set_obj_fn(Row);" + NewLine);
lp.set_obj_fn(new double[] { 0, 2, 3, -2, 3 });
lp.print_lp();
lp.print_str("Now solve the problem with lp.solve();" + NewLine);
lp.print_str(lp.solve() + ": " + lp.get_objective() + NewLine);
Col = new double[lp.get_Ncolumns()];
lp.get_variables(Col);
Row = new double[lp.get_Nrows()];
lp.get_constraints(Row);
Arry = new double[lp.get_Ncolumns() + lp.get_Nrows() + 1];
lp.get_dual_solution(Arry);
Arry = new double[lp.get_Ncolumns() + lp.get_Nrows()];
Lower = new double[lp.get_Ncolumns() + lp.get_Nrows()];
Upper = new double[lp.get_Ncolumns() + lp.get_Nrows()];
lp.get_sensitivity_rhs(Arry, Lower, Upper);
Lower = new double[lp.get_Ncolumns() + 1];
Upper = new double[lp.get_Ncolumns() + 1];
lp.get_sensitivity_obj(Lower, Upper);
lp.print_str("The value is 0, this means we found an optimal solution" + NewLine);
lp.print_str("We can display this solution with lp.print_solution();" + NewLine);
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_str("The dual variables of the solution are printed with" + NewLine);
lp.print_str("lp.print_duals();" + NewLine);
lp.print_duals();
lp.print_str("We can change a single element in the matrix with" + NewLine);
lp.print_str("lp.set_mat(2, 1, 0.5);" + NewLine);
lp.set_mat(2, 1, 0.5);
lp.print_lp();
lp.print_str("If we want to maximize the objective function use lp.set_maxim();" + NewLine);
lp.set_maxim();
lp.print_lp();
lp.print_str("after solving this gives us:" + NewLine);
lp.solve();
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_duals();
lp.print_str("Change the value of a rhs element with lp.set_rh(1, 7.45);" + NewLine);
lp.set_rh(1, 7.45);
lp.print_lp();
lp.solve();
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_str("We change C4 to the integer type with" + NewLine);
lp.print_str("lp.set_int(4, true);" + NewLine);
lp.set_int(4, true);
lp.print_lp();
lp.print_str("We set branch & bound debugging on with lp.set_debug(true);" + NewLine);
lp.set_debug(true);
lp.print_str("and solve..." + NewLine);
lp.solve();
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_str("We can set bounds on the variables with" + NewLine);
lp.print_str("lp.set_lowbo(2, 2); & lp.set_upbo(4, 5.3);" + NewLine);
lp.set_lowbo(2, 2);
lp.set_upbo(4, 5.3);
lp.print_lp();
lp.solve();
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_str("Now remove a constraint with lp.del_constraint(1);" + NewLine);
lp.del_constraint(1);
lp.print_lp();
lp.print_str("Add an equality constraint" + NewLine);
Row = new double[] { 0, 1, 2, 1, 4 };
lp.add_constraint(Row, lpsolve_constr_types.EQ, 8);
lp.print_lp();
lp.print_str("A column can be added with:" + NewLine);
lp.print_str("lp.add_column(Col);" + NewLine);
lp.add_column(new double[] { 3, 2, 2 });
lp.print_lp();
lp.print_str("A column can be removed with:" + NewLine);
lp.print_str("lp.del_column(3);" + NewLine);
lp.del_column(3);
lp.print_lp();
lp.print_str("We can use automatic scaling with:" + NewLine);
lp.print_str("lp.set_scaling(lpsolve_scales.SCALE_MEAN);" + NewLine);
lp.set_scaling(lpsolve_scales.SCALE_MEAN);
lp.print_lp();
lp.print_str("The function lp.get_mat(row, column); returns a single" + NewLine);
lp.print_str("matrix element" + NewLine);
lp.print_str("lp.get_mat(2, 3); lp.get_mat(1, 1); gives " + lp.get_mat(2, 3) + ", " + lp.get_mat(1, 1) + NewLine);
lp.print_str("Notice that get_mat returns the value of the original unscaled problem" + NewLine);
lp.print_str("If there are any integer type variables, then only the rows are scaled" + NewLine);
lp.print_str("lp.set_int(3, false);" + NewLine);
lp.set_int(3, false);
lp.print_lp();
lp.solve();
lp.print_str("print_solution gives the solution to the original problem" + NewLine);
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.print_str("Scaling is turned off with lp.unscale();" + NewLine);
lp.unscale();
lp.print_lp();
lp.print_str("Now turn B&B debugging off and simplex tracing on with" + NewLine);
lp.print_str("lp.set_debug(false); lp.set_trace(true); and lp.solve();" + NewLine);
lp.set_debug(false);
lp.set_trace(true);
lp.solve();
lp.print_str("Where possible, lp_solve will start at the last found basis" + NewLine);
lp.print_str("We can reset the problem to the initial basis with" + NewLine);
lp.print_str("default_basis lp. Now solve it again..." + NewLine);
lp.default_basis();
lp.solve();
lp.print_str("It is possible to give variables and constraints names" + NewLine);
lp.print_str("lp.set_row_name(1, \"speed\"); lp.set_col_name(2, \"money\");" + NewLine);
lp.set_row_name(1, "speed");
lp.set_col_name(2, "money");
lp.print_lp();
lp.print_str("As you can see, all column and rows are assigned default names" + NewLine);
lp.print_str("If a column or constraint is deleted, the names shift place also:" + NewLine);
lp.print_str("lp.del_column(1);" + NewLine);
lp.del_column(1);
lp.print_lp();
lp.write_lp("lp.lp");
lp.write_mps("lp.mps");
lp.set_outputfile(null);
}
using (var lp = LpSolve.read_LP("lp.lp", 0, "test"))
{
if (lp == null)
{
Console.Error.WriteLine("Can't find lp.lp, stopping");
return;
}
lp.set_outputfile("result2.txt");
lp.print_str("An lp structure can be created and read from a .lp file" + NewLine);
lp.print_str("lp = LpSolve.read_lp(\"lp.lp\", 0, \"test\");" + NewLine);
lp.print_str("The verbose option is disabled" + NewLine);
lp.print_str("lp is now:" + NewLine);
lp.print_lp();
lp.print_str("solution:" + NewLine);
lp.set_debug(true);
lpsolve_return statuscode = lp.solve();
string status = lp.get_statustext((int)statuscode);
Debug.WriteLine(status);
lp.set_debug(false);
lp.print_objective();
lp.print_solution(1);
lp.print_constraints(1);
lp.write_lp("lp.lp");
lp.write_mps("lp.mps");
lp.set_outputfile(null);
}
} //Test
}
}