Debian Codegraph @

ot/lp/EMD_wrapper.cpp

raw symbols SCIP
1 /* This file is a c++ wrapper function for computing the transportation cost
2 * between two vectors given a cost matrix.
3 *
4 * It was written by Antoine Rolet (2014) and mainly consists of a wrapper
5 * of the code written by Nicolas Bonneel available on this page
6 * http://people.seas.harvard.edu/~nbonneel/FastTransport/
7 *
8 * It was then modified to make it more amenable to python inline calling
9 *
10 * Please give relevant credit to the original author (Nicolas Bonneel) if
11 * you use this code for a publication.
12 *
13 */
14
15
16 #include "network_simplex_simple.h"
17 #include "network_simplex_simple_omp.h"
18 #include "EMD.h"
19 #include <cstdint>
20
21
22 int EMD_wrap(int n1, int n2, double *X, double *Y, double *D, double *G,
23 double* alpha, double* beta, double *cost, uint64_t maxIter) {
24 // beware M and C are stored in row major C style!!!
25
26 using namespace lemon;
27 uint64_t n, m, cur;
28
29 typedef FullBipartiteDigraph Digraph;
30 DIGRAPH_TYPEDEFS(Digraph);
31
32 // Get the number of non zero coordinates for r and c
33 n=0;
34 for (int i=0; i<n1; i++) {
35 double val=*(X+i);
36 if (val>0) {
37 n++;
38 }else if(val<0){
39 return INFEASIBLE;
40 }
41 }
42 m=0;
43 for (int i=0; i<n2; i++) {
44 double val=*(Y+i);
45 if (val>0) {
46 m++;
47 }else if(val<0){
48 return INFEASIBLE;
49 }
50 }
51
52 // Define the graph
53
54 std::vector<uint64_t> indI(n), indJ(m);
55 std::vector<double> weights1(n), weights2(m);
56 Digraph di(n, m);
57 NetworkSimplexSimple<Digraph,double,double, node_id_type> net(di, true, (int) (n + m), n * m, maxIter);
58
59 // Set supply and demand, don't account for 0 values (faster)
60
61 cur=0;
62 for (uint64_t i=0; i<n1; i++) {
63 double val=*(X+i);
64 if (val>0) {
65 weights1[ cur ] = val;
66 indI[cur++]=i;
67 }
68 }
69
70 // Demand is actually negative supply...
71
72 cur=0;
73 for (uint64_t i=0; i<n2; i++) {
74 double val=*(Y+i);
75 if (val>0) {
76 weights2[ cur ] = -val;
77 indJ[cur++]=i;
78 }
79 }
80
81
82 net.supplyMap(&weights1[0], (int) n, &weights2[0], (int) m);
83
84 // Set the cost of each edge
85 int64_t idarc = 0;
86 for (uint64_t i=0; i<n; i++) {
87 for (uint64_t j=0; j<m; j++) {
88 double val=*(D+indI[i]*n2+indJ[j]);
89 net.setCost(di.arcFromId(idarc), val);
90 ++idarc;
91 }
92 }
93
94
95 // Solve the problem with the network simplex algorithm
96
97 int ret=net.run();
98 uint64_t i, j;
99 if (ret==(int)net.OPTIMAL || ret==(int)net.MAX_ITER_REACHED) {
100 *cost = 0;
101 Arc a; di.first(a);
102 for (; a != INVALID; di.next(a)) {
103 i = di.source(a);
104 j = di.target(a);
105 double flow = net.flow(a);
106 *cost += flow * (*(D+indI[i]*n2+indJ[j-n]));
107 *(G+indI[i]*n2+indJ[j-n]) = flow;
108 *(alpha + indI[i]) = -net.potential(i);
109 *(beta + indJ[j-n]) = net.potential(j);
110 }
111
112 }
113
114
115 return ret;
116 }
117
118
119
120
121
122
123
124 int EMD_wrap_omp(int n1, int n2, double *X, double *Y, double *D, double *G,
125 double* alpha, double* beta, double *cost, uint64_t maxIter, int numThreads) {
126 // beware M and C are stored in row major C style!!!
127
128 using namespace lemon_omp;
129 uint64_t n, m, cur;
130
131 typedef FullBipartiteDigraph Digraph;
132 DIGRAPH_TYPEDEFS(Digraph);
133
134 // Get the number of non zero coordinates for r and c
135 n=0;
136 for (int i=0; i<n1; i++) {
137 double val=*(X+i);
138 if (val>0) {
139 n++;
140 }else if(val<0){
141 return INFEASIBLE;
142 }
143 }
144 m=0;
145 for (int i=0; i<n2; i++) {
146 double val=*(Y+i);
147 if (val>0) {
148 m++;
149 }else if(val<0){
150 return INFEASIBLE;
151 }
152 }
153
154 // Define the graph
155
156 std::vector<uint64_t> indI(n), indJ(m);
157 std::vector<double> weights1(n), weights2(m);
158 Digraph di(n, m);
159 NetworkSimplexSimple<Digraph,double,double, node_id_type> net(di, true, (int) (n + m), n * m, maxIter, numThreads);
160
161 // Set supply and demand, don't account for 0 values (faster)
162
163 cur=0;
164 for (uint64_t i=0; i<n1; i++) {
165 double val=*(X+i);
166 if (val>0) {
167 weights1[ cur ] = val;
168 indI[cur++]=i;
169 }
170 }
171
172 // Demand is actually negative supply...
173
174 cur=0;
175 for (uint64_t i=0; i<n2; i++) {
176 double val=*(Y+i);
177 if (val>0) {
178 weights2[ cur ] = -val;
179 indJ[cur++]=i;
180 }
181 }
182
183
184 net.supplyMap(&weights1[0], (int) n, &weights2[0], (int) m);
185
186 // Set the cost of each edge
187 int64_t idarc = 0;
188 for (uint64_t i=0; i<n; i++) {
189 for (uint64_t j=0; j<m; j++) {
190 double val=*(D+indI[i]*n2+indJ[j]);
191 net.setCost(di.arcFromId(idarc), val);
192 ++idarc;
193 }
194 }
195
196
197 // Solve the problem with the network simplex algorithm
198
199 int ret=net.run();
200 uint64_t i, j;
201 if (ret==(int)net.OPTIMAL || ret==(int)net.MAX_ITER_REACHED) {
202 *cost = 0;
203 Arc a; di.first(a);
204 for (; a != INVALID; di.next(a)) {
205 i = di.source(a);
206 j = di.target(a);
207 double flow = net.flow(a);
208 *cost += flow * (*(D+indI[i]*n2+indJ[j-n]));
209 *(G+indI[i]*n2+indJ[j-n]) = flow;
210 *(alpha + indI[i]) = -net.potential(i);
211 *(beta + indJ[j-n]) = net.potential(j);
212 }
213
214 }
215
216
217 return ret;
218 }