Skip to content

Commit 41c3586

Browse files
committed
added a working farmer example
1 parent fcc3d95 commit 41c3586

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

examples/cpp/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
CXX=g++
4+
INCDIR=../../build/include
5+
6+
all: farmer_*.cpp
7+
$(CXX) -o farmer farmer.cpp
8+
$(CXX) -o farmer_dec farmer_dec.cpp

examples/cpp/farmer_dec.cpp

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <dlfcn.h>
4+
5+
/*
6+
Farmer example: This is based on a well-known stochastic programming problem.
7+
8+
minimize 1/3 * (150 x11 + 230 x21 + 260 x31 + 238 y11 + 210 y21 - 170 w11 - 150 w21 - 36 w31 - 10 w41)
9+
+ 1/3 * (150 x12 + 230 x22 + 260 x32 + 238 y12 + 210 y22 - 170 w12 - 150 w22 - 36 w32 - 10 w42)
10+
+ 1/3 * (150 x13 + 230 x23 + 260 x33 + 238 y13 + 210 y23 - 170 w13 - 150 w23 - 36 w33 - 10 w43)
11+
subject to
12+
x11 - x12 == 0
13+
x21 - x22 == 0
14+
x31 - x32 == 0
15+
x12 - x13 == 0
16+
x22 - x23 == 0
17+
x32 - x33 == 0
18+
19+
Each block has the following constraint matrix and the common row bound vectors:
20+
21+
A1 = [1 1 1 ]
22+
[3.0 1.0 -1.0 ]
23+
[ 3.6 1.0 -1.0 ]
24+
[ 24 -1.0 -1.0]
25+
[ 1.0 ]
26+
27+
A2 = [1 1 1 ]
28+
[2.5 1.0 -1.0 ]
29+
[ 3.0 1.0 -1.0 ]
30+
[ 20 -1.0 -1.0]
31+
[ 1.0 ]
32+
33+
A3 = [1 1 1 ]
34+
[2.0 1.0 -1.0 ]
35+
[ 2.4 1.0 -1.0 ]
36+
[ 16 -1.0 -1.0]
37+
[ 1.0 ]
38+
39+
bl = [-Inf 200 240 0.0 -Inf]
40+
bu = [500 Inf Inf Inf 6000]
41+
*/
42+
43+
int main(int argc, char **argv)
44+
{
45+
void *handle;
46+
char *error;
47+
48+
// Load the library
49+
handle = dlopen("libDsp.dylib", RTLD_LAZY);
50+
if (!handle) {
51+
fputs (dlerror(), stderr);
52+
exit(1);
53+
}
54+
55+
void* env = NULL;
56+
env = ((void *(*)())dlsym(handle, "createEnv"))();
57+
if (env == NULL) {
58+
printf("Failed to create DSP environment.\n");
59+
} else {
60+
printf("Successfully created DSP environment.\n");
61+
}
62+
63+
// Problem Data
64+
int nvars = 27;
65+
int ncons0 = 6;
66+
int ncons1 = 5;
67+
double obj[] = {150/3, 230/3, 260/3, 238/3, 210/3, -170/3, -150/3, -36/3, -10/3};
68+
double rlbd0[] = {0, 0, 0, 0, 0, 0};
69+
double rubd0[] = {0, 0, 0, 0, 0, 0};
70+
double rlbd1[] = {-1e+20, 200, 240, 0, -1e+20};
71+
double rubd1[] = {500, 1e+20, 1e+20, 1e+20, 6000};
72+
int start0[] = {0, 2, 4, 6, 8, 10, 12};
73+
int index0[] = {0, 9, 1, 10, 2, 11, 9, 18, 10, 19, 11, 20};
74+
double value0[] = {1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1};
75+
int start1[] = {0, 3, 6, 9, 12, 13};
76+
int index1[] = {0, 1, 2, 0, 3, 5, 1, 4, 6, 2, 7, 8, 7};
77+
double value1[] = {1, 1, 1, 3, 1, -1, 3.6, 1, -1, 24, -1, -1, 1};
78+
double value2[] = {1, 1, 1, 2.5, 1, -1, 3, 1, -1, 20, -1, -1, 1};
79+
double value3[] = {1, 1, 1, 2, 1, -1, 2.4, 1, -1, 16, -1, -1, 1};
80+
81+
double *obj0 = new double [nvars];
82+
double *clbd0 = new double [nvars];
83+
double *cubd0 = new double [nvars];
84+
char *ctype0 = new char [nvars];
85+
for (int j = 0; j < nvars; ++j) {
86+
obj0[j] = obj[j % 9];
87+
clbd0[j] = 0.0;
88+
cubd0[j] = 1e+20;
89+
ctype0[j] = j % 9 < 3 ? 'I' : 'C';
90+
}
91+
92+
((void (*)(
93+
void *,
94+
int,
95+
int,
96+
int,
97+
int,
98+
int*,
99+
int*,
100+
double*,
101+
double*,
102+
double*,
103+
char*,
104+
double*,
105+
double*,
106+
double*
107+
))dlsym(handle, "loadBlockProblem"))(
108+
env,
109+
0,
110+
nvars,
111+
ncons0,
112+
start0[ncons0],
113+
start0,
114+
index0,
115+
value0,
116+
clbd0,
117+
cubd0,
118+
ctype0,
119+
obj0,
120+
rlbd0,
121+
rubd0
122+
);
123+
124+
int *index_ = new int [start1[ncons1]];
125+
double *value_ = new double [start1[ncons1]];
126+
127+
// create each block
128+
for (int block = 0; block < 3; block++) {
129+
for (int j = 0; j < start1[ncons1]; ++j) {
130+
index_[j] = index1[j] + block * 9;
131+
if (block == 0) value_[j] = value1[j];
132+
else if (block == 1) value_[j] = value2[j];
133+
else if (block == 2) value_[j] = value3[j];
134+
}
135+
((void (*)(
136+
void *,
137+
int,
138+
int,
139+
int,
140+
int,
141+
int*,
142+
int*,
143+
double*,
144+
double*,
145+
double*,
146+
char*,
147+
double*,
148+
double*,
149+
double*
150+
))dlsym(handle, "loadBlockProblem"))(
151+
env,
152+
block + 1,
153+
nvars,
154+
ncons1,
155+
start1[ncons1],
156+
start1,
157+
index_,
158+
value_,
159+
clbd0,
160+
cubd0,
161+
ctype0,
162+
obj0,
163+
rlbd1,
164+
rubd1
165+
);
166+
}
167+
168+
// Important to call this!
169+
((void (*)(void*))dlsym(handle, "updateBlocks"))(env);
170+
171+
172+
// ((void (*)(void*))dlsym(handle, "solveDe"))(env); // solve the deterministic equivalent problem
173+
((void (*)(void*))dlsym(handle, "solveDw"))(env); // solve the Dantzig-Wolfe decomposition
174+
175+
double primal_bound = ((double (*)(void*))dlsym(handle, "getPrimalBound"))(env);
176+
printf("Primal bound = %e\n", primal_bound);
177+
178+
double *solution = new double [nvars];
179+
((void (*)(void*, int, double*))dlsym(handle, "getPrimalSolution"))(env, nvars, solution);
180+
for (int j = 0; j < nvars; ++j) {
181+
printf("var%2d = %f\n", j, solution[j]);
182+
}
183+
// ((void (*)(void*, const char*))dlsym(handle, "writeMps"))(env, "farmer");
184+
185+
if (env != NULL) {
186+
((void (*)(void*&))dlsym(handle, "freeEnv"))(env);
187+
env = NULL;
188+
printf("Successfully freed DSP environment.\n");
189+
}
190+
dlclose(handle);
191+
192+
delete [] obj0;
193+
delete [] clbd0;
194+
delete [] cubd0;
195+
delete [] ctype0;
196+
delete [] index_;
197+
delete [] value_;
198+
delete [] solution;
199+
200+
return 0;
201+
}

0 commit comments

Comments
 (0)