-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction.cpp
230 lines (204 loc) · 7.74 KB
/
auction.cpp
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
/*
* MATLAB Wrapper for Transport Problem Auction Algorithm
*
* by Gerhard Kurz
*
* original auction code by Joseph D Walsh III <[email protected]>
*/
#include "mex.h"
#include <cmath>
#include <iostream>
#include <chrono>
#include <numeric> //accumulate
#include "glob.hpp" // mfloat, mfvec, mint, muint
#include "object.hpp" // objlist, voblist
#include "gamap.hpp" // GAmap
mfloat gEPS = std::sqrt(std::numeric_limits<mfloat>::epsilon());
mfloat gINF = std::numeric_limits<mfloat>::infinity();
muint gVBS = 0; // verbosity (0, 1, or 2)
/** --- GArun ------------------------------------------------------------------
**/
void GArun (const mfvec& DWT, const mfvec& SWT, const voblist& A,
const mfloat MX, const mfloat MN, const mfloat ST,
objlist& T, mfvec& PR) {
GAmap gslv (DWT, SWT, A, MX, MN, ST);
gslv.Solve (T, PR);
return;
}
/** --- Dual -------------------------------------------------------------------
**/
mfloat Dual (const mfvec& DWT, const mfvec& SWT, const voblist& ARX,
const mfvec& PR) {
mfloat dcst = 0.0;
mfvec M;
for (muint i = 0; i < DWT.size(); i++) {
M.clear();
for (muint j = 0; j < ARX[i].size(); j++) {
M.push_back (ARX[i][j].c - PR[muint (ARX[i][j].j)]);
if (gVBS > 1) {
printf (" Exp %lu -> %lu : %f - %f = %f\n", i, j,
ARX[i][j].c, PR[muint (ARX[i][j].j)], M.back());
}
}
dcst += DWT[i] * *std::max_element (M.begin(), M.end());
}
if (gVBS > 1) {
printf("---------------\n");
}
for (muint it = 0; it < SWT.size(); it++) {
dcst += SWT[it] * PR[it];
if (gVBS > 1) {
printf (" Price %lu : %f\n", it, PR[it]);
}
}
if (gVBS > 1) {
printf("---------------\n");
}
return dcst;
}
/** --- Primal -----------------------------------------------------------------
**/
mfloat Primal (const char* aname, const voblist& ARX, const objlist& T) {
mfloat pcst = 0.0;
muint tst;
for (muint it = 0; it < T.size(); it++) {
tst = 0;
while ( (tst < ARX[muint (T[it].i)].size())
&& (ARX[muint (T[it].i)][tst].j != T[it].j)) {
tst++;
}
if (tst < ARX[muint (T[it].i)].size()) {
pcst += ARX[muint (T[it].i)][tst].c * T[it].c;
if (gVBS > 1) {
printf (" %3ld -> %3ld : %f @ %f \n", T[it].j, T[it].i,
T[it].c, ARX[muint (T[it].i)][tst].c);
}
} else {
printf("Overflow in %s cost calculation\n", aname);
}
}
return pcst;
}
void mexFunction(int numOutputs, mxArray *outputs[],
int numInputs, const mxArray *inputs[])
{
try {
/* Check for proper number of arguments */
if (numInputs != 3) {
mexErrMsgIdAndTxt("Auction:inputs",
"Three inputs are required.");
}
/*
* if (numOutputs != 3) {
* throw std::invalid_argument("Three outputs are required.");
* }
*
* if (sampleWeights.cols() != numSamples) {
* throw std::invalid_argument("Number of weights has to match the number of samples.");
* }
*
* const unsigned int n = *mxGetPr(inputs[2]);
*
* if (numSamples < n) {
* throw std::invalid_argument("Need more samples than Gaussian components.");
* } */
if(mxGetN(inputs[0]) != 1) {
mexErrMsgIdAndTxt("Auction:inputs",
"First Input must be a column vector.");
}
if(mxGetN(inputs[1]) != 1) {
mexErrMsgIdAndTxt("Auction:inputs",
"Second Input must be a column vector.");
}
size_t nSources = mxGetM(inputs[0]);
size_t nSinks = mxGetM(inputs[1]);
if(mxGetM(inputs[2]) != nSources) {
mexErrMsgIdAndTxt("Auction:inputs",
"Rows of cost matrix need to match number of sources.");
}
if(mxGetN(inputs[2]) != nSinks) {
mexErrMsgIdAndTxt("Auction:inputs",
"Columns of cost matrix need to match number of sinks.");
}
//todo there is an assumption N>=M, otherwise soruces and sinks have to be swapped
mfvec DWT; //demand
DWT.assign(mxGetPr(inputs[0]), mxGetPr(inputs[0]) + nSources);
mfvec SWT; //supply
SWT.assign(mxGetPr(inputs[1]), mxGetPr(inputs[1]) + nSinks);
if(!equal(std::accumulate(DWT.begin(), DWT.end(), 0), std::accumulate(SWT.begin(), SWT.end(), 0))){
mexErrMsgIdAndTxt("Auction:inputs",
"Sum of demand and sum of supply nust match");
}
voblist ARX; //arcs in the graph
ARX.resize(nSources);
for(int i=0; i<nSources; i++){
ARX[i].resize(nSinks);
for(int j=0; j<nSinks; j++){
Object o;
o.c = *(mxGetPr(inputs[2]) + j*nSources + i);
o.i=i;
o.j=j;
ARX[i][j] = o; //todo matrix may be transposed?
}
}
mfloat eps = -1.0;
mfloat stp = 0.25;
mfloat min = -1.0;
objlist T; // transport plan
mfvec PR; // price vector
mfloat pcst; // primal cost
mfloat dcst; // dual cost
mfloat C = 0; // maximum cost
muint ar = 0; // number of arcs in graph
for (muint i = 0; i < ARX.size(); i++) {
for (muint j = 0; j < ARX[i].size(); j++) {
ar++;
if (C < std::abs(ARX[i][j].c)) {
C = std::abs(ARX[i][j].c);
}
}
}
if (eps < gEPS) {
eps = C / 5.0;
}
if (min < gEPS) {
min = 1.0 / mfloat (SWT.size());
}
printf(" GRAPH: %lu sinks, %lu sources, %lu arcs\n", DWT.size(),
SWT.size(), ar);
printf(" EPS : %f starting, %e minimum\n", eps, min);
std::chrono::high_resolution_clock::time_point t1, t2;
std::chrono::duration <mfloat> dur;
t1 = std::chrono::high_resolution_clock::now();
GArun (DWT, SWT, ARX, eps, min, stp, T, PR);
t2 = std::chrono::high_resolution_clock::now();
dur = std::chrono::duration_cast <std::chrono::duration <mfloat> >
(t2 - t1);
if (T.empty()) {
printf (" General auction NOT SOLVED\n");
} else {
pcst = Primal ("GA", ARX, T);
printf (" General auction primal cost : %25.15f\n", pcst);
dcst = Dual (DWT, SWT, ARX, PR);
printf (" General auction dual cost : %25.15f\n", dcst);
printf (" General auction diff : %25.15f\n",
dcst-pcst);
}
printf (" General auction time : %13.3f sec\n",
dur.count() );
//print plan
/*
for(int i=0; i<T.size(); i++){
printf("%f %i %i\n", T[i].c, T[i].i, T[i].j);
}*/
if(numOutputs >= 1){
outputs[0] = mxCreateDoubleMatrix(nSources,nSinks,mxREAL);
for(int i=0; i<T.size(); i++){
*(mxGetPr(outputs[0]) + T[i].j*nSources +T[i].i) = T[i].c;
}
}
} catch (std::exception& ex) {
//usage();
mexErrMsgTxt(ex.what());
}
}