summaryrefslogtreecommitdiff
path: root/geom_matching/wasserstein/src/auction_runner_gs.cpp
blob: 83d88c52792d8ba82750cb79794d36b16ab51676 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 
Copyright (c) 2016, M. Kerber, D. Morozov, A. Nigmetov
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
(Enhancements) to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to copyright holder,
without imposing a separate written license agreement for such Enhancements,
then you hereby grant the following license: a  non-exclusive, royalty-free
perpetual license to install, use, modify, prepare derivative works, incorporate
into other computer software, distribute, and sublicense such enhancements or
derivative works thereof, in binary and source code form.

  */


#include <assert.h>
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <iterator>
#include <chrono>

#include "def_debug_ws.h"
#include "auction_runner_gs.h"
#include "wasserstein.h"

#ifdef FOR_R_TDA
#include "Rcpp.h"
#endif

//#define PRINT_DETAILED_TIMING

namespace geom_ws {

// *****************************
// AuctionRunnerGS 
// *****************************

AuctionRunnerGS::AuctionRunnerGS(const std::vector<DiagramPoint>& A, const std::vector<DiagramPoint>& B, const double q, const double _delta, const double _internal_p, const double _initialEpsilon, const double _epsFactor) :
    bidders(A),
    items(B),
    numBidders(A.size()),
    numItems(A.size()),
    itemsToBidders(A.size(), -1),
    biddersToItems(A.size(), -1),
    wassersteinPower(q),
    delta(_delta),
    internal_p(_internal_p),
    initialEpsilon(_initialEpsilon),
    epsilonCommonRatio(_epsFactor == 0.0 ? 5.0 : _epsFactor) 
{
    assert(initialEpsilon >= 0.0 );
    assert(epsilonCommonRatio >= 0.0 );
    assert(A.size() == B.size());
    oracle = std::unique_ptr<AuctionOracle>(new AuctionOracle(bidders, items, wassersteinPower, internal_p));
}

void AuctionRunnerGS::assignItemToBidder(IdxType itemIdx, IdxType bidderIdx)
{
    numRounds++;
    //sanityCheck();
    // only unassigned bidders should submit bids and get items
    assert(biddersToItems[bidderIdx] == -1);
    IdxType oldItemOwner = itemsToBidders[itemIdx];

    // set new owner
    biddersToItems[bidderIdx] = itemIdx;
    itemsToBidders[itemIdx] = bidderIdx;
    // remove bidder from the list of unassigned bidders
#ifdef KEEP_UNASSIGNED_ORDERED
    unassignedBidders.erase(std::make_pair(bidderIdx, bidders[bidderIdx]));
#else
    unassignedBidders.erase(bidderIdx);
#endif

    // old owner becomes unassigned
    if (oldItemOwner != -1) {
        biddersToItems[oldItemOwner] = -1;
#ifdef KEEP_UNASSIGNED_ORDERED
        unassignedBidders.insert(std::make_pair(oldItemOwner, bidders[oldItemOwner]));
#else
        unassignedBidders.insert(oldItemOwner);
#endif
    }
}


void AuctionRunnerGS::flushAssignment(void)
{
    for(auto& b2i : biddersToItems) {
        b2i = -1;
    }
    for(auto& i2b : itemsToBidders) {
        i2b = -1;
    }
    // we must flush assignment only after we got perfect matching
    assert(unassignedBidders.empty());
    // all bidders become unassigned
    for(size_t bidderIdx = 0; bidderIdx < numBidders; ++bidderIdx) {
#ifdef KEEP_UNASSIGNED_ORDERED
        unassignedBidders.insert(std::make_pair(bidderIdx, bidders[bidderIdx]));
#else
        unassignedBidders.insert(bidderIdx);
#endif
    }
    assert(unassignedBidders.size() == bidders.size());
    //oracle->adjustPrices();
}

void AuctionRunnerGS::runAuction(void)
{
    relativeError = std::numeric_limits<double>::max();
#ifdef PRINT_DETAILED_TIMING
    std::chrono::high_resolution_clock hrClock;
    std::chrono::time_point<std::chrono::high_resolution_clock> startMoment;
    startMoment = hrClock.now();
    std::vector<double> iterResults;
    std::vector<double> iterEstRelErrors;
    std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>> iterTimes;
#endif
    // choose some initial epsilon
    if (initialEpsilon == 0.0)
        oracle->setEpsilon(oracle->maxVal / 4.0);
    else 
        oracle->setEpsilon(initialEpsilon);
    assert( oracle->getEpsilon() > 0 );
    int iterNum { 0 };
    bool notDone { false };
    double currentResult;
    do {
        flushAssignment();
        runAuctionPhase();
        iterNum++;
        //std::cout << "Iteration " << iterNum << " completed. " << std::endl; 
        // result is d^q
        currentResult = getDistanceToQthPowerInternal();
        double denominator = currentResult - numBidders * oracle->getEpsilon();
        currentResult = pow(currentResult, 1.0 / wassersteinPower);
#ifdef PRINT_DETAILED_TIMING
#ifndef FOR_R_TDA
        iterResults.push_back(currentResult);
        iterTimes.push_back(hrClock.now());
        std::cout << "Iteration " << iterNum << " finished. ";
        std::cout << "Current result is " << currentResult  << ", epsilon = " << oracle->getEpsilon() << std::endl;
        std::cout << "Number of rounds (cumulative): " << numRounds << std::endl;
#endif
#endif
        if ( denominator <= 0 ) {
            //std::cout << "Epsilon is too big." << std::endl;
            notDone = true;
        } else {
            denominator = pow(denominator, 1.0 / wassersteinPower);
            double numerator = currentResult - denominator;
#ifdef PRINT_DETAILED_TIMING
#ifndef FOR_R_TDA
            std::cout << " numerator: " << numerator << " denominator: " << denominator;
            std::cout << "; error bound: " << numerator / denominator << std::endl;
#endif
#endif
            relativeError = numerator / denominator;
            // if relative error is greater than delta, continue
            notDone = ( numerator / denominator > delta );
        }
        // decrease epsilon for the next iteration
        oracle->setEpsilon( oracle->getEpsilon() / epsilonCommonRatio );
        if (iterNum > maxIterNum) {
#ifndef FOR_R_TDA
            std::cerr << "Maximum iteration number exceeded, exiting. Current result is:"; 
            std::cerr << wassersteinDistance << std::endl;
#endif
            throw std::runtime_error("Maximum iteration number exceeded");
        }
    } while ( notDone );
    //printMatching();
#ifdef PRINT_DETAILED_TIMING
#ifndef FOR_R_TDA
    for(size_t iterIdx = 0; iterIdx < iterResults.size(); ++iterIdx) {
        double trueRelError = ( iterResults.at(iterIdx) - currentResult ) / currentResult;
        auto iterCumulativeTime = iterTimes.at(iterIdx) - startMoment;
        std::chrono::duration<double, std::milli> iterTime =  ( iterIdx > 0) ? iterTimes[iterIdx] - iterTimes[iterIdx - 1] : iterTimes[iterIdx] - startMoment; 
        std::cout << "iteration " << iterIdx << ", true rel. error " <<
                    trueRelError << ", elapsed time " << 
                    std::chrono::duration<double, std::milli>(iterCumulativeTime).count() << 
                    ", iteration time " << iterTime.count() << std::endl;
    }
#endif
#endif
}

void AuctionRunnerGS::runAuctionPhase(void)
{
    //std::cout << "Entered runAuctionPhase" << std::endl;
    do {
#ifdef KEEP_UNASSIGNED_ORDERED
        size_t bidderIdx = unassignedBidders.begin()->first;
#else
        size_t bidderIdx = *unassignedBidders.begin();
#endif
        auto optimalBid = oracle->getOptimalBid(bidderIdx);
        auto optimalItemIdx = optimalBid.first;
        auto bidValue = optimalBid.second;
        assignItemToBidder(optimalBid.first, bidderIdx);
        oracle->setPrice(optimalItemIdx, bidValue);
        //printDebug();
#ifdef FOR_R_TDA
        if ( numRounds % 10000 == 0 ) {
            Rcpp::checkUserInterrupt();
        }
#endif
    } while (not unassignedBidders.empty());
    //std::cout << "runAuctionPhase finished" << std::endl;

#ifdef DEBUG_AUCTION
    for(size_t bidderIdx = 0; bidderIdx < numBidders; ++bidderIdx) {
        if ( biddersToItems[bidderIdx] < 0) {
#ifndef FOR_R_TDA
            std::cerr << "After auction terminated bidder " << bidderIdx;
            std::cerr << " has no items assigned" << std::endl;
#endif
            throw std::runtime_error("Auction did not give a perfect matching");
        }
    }
#endif

}
 
double AuctionRunnerGS::getDistanceToQthPowerInternal(void)
{
    sanityCheck();
    double result = 0.0;
    //std::cout << "-------------------------------------------------------------------------\n";
    for(size_t bIdx = 0; bIdx < numBidders; ++bIdx) {
        auto pA = bidders[bIdx];
        assert( 0 <= biddersToItems[bIdx] and biddersToItems[bIdx] < static_cast<int>(items.size()) );
        auto pB = items[biddersToItems[bIdx]];
        //std::cout << "pA = " << pA << ", pB = " << pB << ", pow(distLp(pA, pB, internal_p), wassersteinPower) = " << pow(distLp(pA, pB, internal_p), wassersteinPower) << ", dist = " << distLp(pA, pB, internal_p) << std::endl;
        result += pow(distLp(pA, pB, internal_p), wassersteinPower);
    }
    //std::cout << "-------------------------------------------------------------------------\n";
    wassersteinCost = result;
    wassersteinDistance = pow(result, 1.0 / wassersteinPower);
    return result;
}

double AuctionRunnerGS::getWassersteinDistance(void)
{
    runAuction();
    return wassersteinDistance;
}

double AuctionRunnerGS::getWassersteinCost(void)
{
    runAuction();
    return wassersteinCost;
}



// Debug routines

void AuctionRunnerGS::printDebug(void)
{
#ifdef DEBUG_AUCTION
#ifndef FOR_R_TDA
    sanityCheck();
    std::cout << "**********************" << std::endl;
    std::cout << "Current assignment:" << std::endl;
    for(size_t idx = 0; idx < biddersToItems.size(); ++idx) {
        std::cout << idx << " <--> " << biddersToItems[idx] << std::endl;
    }
    std::cout << "Weights: " << std::endl;
    //for(size_t i = 0; i < numBidders; ++i) {
        //for(size_t j = 0; j < numItems; ++j) {
            //std::cout << oracle->weightMatrix[i][j] << " ";
        //}
        //std::cout << std::endl;
    //}
    std::cout << "Prices: " << std::endl;
    for(const auto price : oracle->getPrices()) {
        std::cout << price << std::endl;
    }
    std::cout << "**********************" << std::endl;
#endif
#endif
}


void AuctionRunnerGS::sanityCheck(void)
{
#ifdef DEBUG_AUCTION
    if (biddersToItems.size() != numBidders) {
#ifndef FOR_R_TDA
        std::cerr << "Wrong size of biddersToItems, must be " << numBidders << ", is " << biddersToItems.size() << std::endl;
#endif
        throw std::runtime_error("Wrong size of biddersToItems");
    }

    if (itemsToBidders.size() != numBidders) {
#ifndef FOR_R_TDA
        std::cerr << "Wrong size of itemsToBidders, must be " << numBidders << ", is " << itemsToBidders.size() << std::endl;
#endif
        throw std::runtime_error("Wrong size of itemsToBidders");
    }

    for(size_t bidderIdx = 0; bidderIdx < numBidders; ++bidderIdx) {
        if ( biddersToItems[bidderIdx] >= 0) {

            if ( std::count(biddersToItems.begin(),
                        biddersToItems.end(),
                        biddersToItems[bidderIdx]) > 1 ) {
#ifndef FOR_R_TDA
                std::cerr << "Item " << biddersToItems[bidderIdx];
                std::cerr << " appears in biddersToItems more than once" << std::endl;
#endif
                throw std::runtime_error("Duplicate in biddersToItems");
            }

            if (itemsToBidders.at(biddersToItems[bidderIdx]) != static_cast<int>(bidderIdx)) {
#ifndef FOR_R_TDA
                std::cerr << "Inconsitency: bidderIdx = " << bidderIdx;
                std::cerr << ", itemIdx in biddersToItems = ";
                std::cerr << biddersToItems[bidderIdx];
                std::cerr << ", bidderIdx in itemsToBidders = ";
                std::cerr << itemsToBidders[biddersToItems[bidderIdx]] << std::endl;
#endif
                throw std::runtime_error("inconsistent mapping");
            }
        }
    }

    for(IdxType itemIdx = 0; itemIdx < static_cast<IdxType>(numBidders); ++itemIdx) {
        if ( itemsToBidders[itemIdx] >= 0) {

            // check for uniqueness
            if ( std::count(itemsToBidders.begin(),
                        itemsToBidders.end(),
                        itemsToBidders[itemIdx]) > 1 ) {
#ifndef FOR_R_TDA
                std::cerr << "Bidder " << itemsToBidders[itemIdx];
                std::cerr << " appears in itemsToBidders more than once" << std::endl;
#endif
                throw std::runtime_error("Duplicate in itemsToBidders");
            }
            // check for consistency
            if (biddersToItems.at(itemsToBidders[itemIdx]) != static_cast<int>(itemIdx)) {
#ifndef FOR_R_TDA
                std::cerr << "Inconsitency: itemIdx = " << itemIdx;
                std::cerr << ", bidderIdx in itemsToBidders = ";
                std::cerr << itemsToBidders[itemIdx];
                std::cerr << ", itemIdx in biddersToItems= ";
                std::cerr << biddersToItems[itemsToBidders[itemIdx]] << std::endl;
#endif
                throw std::runtime_error("inconsistent mapping");
            }
        }
    }
#endif
}

void AuctionRunnerGS::printMatching(void)
{
//#ifdef DEBUG_AUCTION
#ifndef FOR_R_TDA
    sanityCheck();
    for(size_t bIdx = 0; bIdx < biddersToItems.size(); ++bIdx) {
        if (biddersToItems[bIdx] >= 0) {
            auto pA = bidders[bIdx];
            auto pB = items[biddersToItems[bIdx]];
            std::cout <<  pA << " <-> " << pB << "+" << pow(distLp(pA, pB, internal_p), wassersteinPower) << std::endl;
        } else {
            assert(false);
        }
    }
#endif
//#endif
}

} // end of namespace geom_ws