32 int run(std::vector<MatchingType> &matchings)
override;
48 goodPrices.resize(this->
colSize, 0);
54 numberOfRounds = noRounds;
66 epsilonDiviserMultiplier = div;
70 goodPrices.resize(prices.size(), 0);
71 for(
unsigned int i = 0; i < prices.size(); ++i)
72 goodPrices[i] = prices[i];
80 int numberOfRounds = -1;
83 double epsilonDiviserMultiplier = 0;
84 double delta_lim = 0.01;
86 dataType lowerBoundCostWeight = 1 + delta_lim;
89 std::vector<int> bidderAssignments{1, -1}, bestBidderAssignments;
90 std::vector<int> goodAssignments{};
91 std::vector<double> goodPrices{};
92 dataType lowerBoundCost;
103 unsigned int const nRows = matrix.size();
104 unsigned int const nCols = matrix[0].size();
105 dataType maxValue = std::numeric_limits<dataType>::lowest();
106 for(
unsigned int i = 0; i < nRows; ++i) {
107 for(
unsigned int j = 0; j < nCols; ++j) {
108 if(not balancedAsgn and i == nRows - 1 and j == nCols - 1)
110 maxValue = (matrix[i][j] > maxValue) ? matrix[i][j] : maxValue;
138 bidderAssignments.clear();
139 goodAssignments.clear();
140 if(this->balancedAssignment) {
141 bidderAssignments.resize(this->rowSize, -1);
142 goodAssignments.resize(this->colSize, -1);
144 bidderAssignments.resize((this->rowSize - 1) + (this->colSize - 1), -1);
145 goodAssignments.resize((this->colSize - 1) + (this->rowSize - 1), -1);
157 std::vector<std::vector<dataType>> &matrix) {
158 unsigned int const nRows = matrix.size();
159 unsigned int const nCols = matrix[0].size();
160 matrix[nRows - 1][nCols - 1] = 0;
163 for(
unsigned int i = 0; i < nCols - 2; ++i) {
164 std::vector<dataType>
const newLine(matrix[nRows - 1]);
165 matrix.push_back(newLine);
168 for(
unsigned int i = 0; i < (nRows - 1) + (nCols - 1); ++i) {
169 for(
unsigned int j = 0; j < nRows - 2; ++j) {
170 matrix[i].push_back(matrix[i][nCols - 1]);
180 std::vector<std::vector<dataType>> &cMatrix) {
181 std::queue<int> unassignedBidders{};
182 for(
unsigned int i = 0; i < bidderAssignments.size(); ++i)
183 unassignedBidders.push(i);
185 while(!unassignedBidders.empty()) {
186 int const bidderId = unassignedBidders.front();
187 unassignedBidders.pop();
190 dataType bestValue = std::numeric_limits<dataType>::lowest();
191 dataType bestSecondValue = std::numeric_limits<dataType>::lowest();
193 for(
unsigned int goodId = 0; goodId < goodPrices.size(); ++goodId) {
194 if(cMatrix[bidderId][goodId] ==
static_cast<dataType
>(-1))
196 dataType goodPrice = goodPrices[goodId];
197 dataType value = -cMatrix[bidderId][goodId] - goodPrice;
198 if(value > bestValue) {
199 bestSecondValue = bestValue;
202 }
else if(value > bestSecondValue)
203 bestSecondValue = value;
207 bidderAssignments[bidderId] = bestGoodId;
208 if(goodAssignments[bestGoodId] != -1)
209 unassignedBidders.push(goodAssignments[bestGoodId]);
210 goodAssignments[bestGoodId] = bidderId;
213 if(bestSecondValue == std::numeric_limits<dataType>::lowest())
214 bestSecondValue = bestValue;
217 double const delta = abs<dataType>(bestValue - bestSecondValue) + epsilon;
218 double newPrice = goodPrices[bestGoodId] + delta;
219 if(newPrice > std::numeric_limits<double>::max() / 2) {
221 newPrice = goodPrices[bestGoodId] + epsilon;
223 goodPrices[bestGoodId] = newPrice;
229 std::vector<dataType> minCol(
230 costMatrix[0].size(), std::numeric_limits<dataType>::max()),
231 minRow(costMatrix.size(), std::numeric_limits<dataType>::max());
232 for(
unsigned int i = 0; i < costMatrix.size(); ++i) {
233 for(
unsigned int j = 0; j < costMatrix[i].size(); ++j) {
234 if(costMatrix[i][j] < minCol[j])
235 minCol[j] = costMatrix[i][j];
236 if(costMatrix[i][j] < minRow[i])
237 minRow[i] = costMatrix[i][j];
240 dataType minColCost = 0, minRowCost = 0;
241 for(
unsigned int i = 0; i < minCol.size(); ++i)
242 minColCost += minCol[i];
243 for(
unsigned int i = 0; i < minRow.size(); ++i)
244 minRowCost += minRow[i];
245 dataType lowerBoundCost = std::max(minColCost, minRowCost);
247 return lowerBoundCost;
253 dataType bestCost = std::numeric_limits<dataType>::max();
256 double const tempPrice
257 = *std::max_element(goodPrices.begin(), goodPrices.end());
258 std::vector<double> savedPrices;
259 for(
unsigned int i = 0; i < goodPrices.size(); ++i) {
260 auto old = goodPrices[i];
262 = goodPrices[i] * epsilon / ((tempPrice == 0) ? 1 : tempPrice);
263 auto t = old - goodPrices[i];
264 savedPrices.push_back(t);
268 if(not this->balancedAssignment)
269 this->makeBalancedMatrix(this->costMatrix);
276 while(not stoppingCriterion(this->costMatrix)) {
277 initBiddersAndGoods();
278 runAuctionRound(this->costMatrix);
280 dataType cost = getMatchingDistance(this->costMatrix);
281 if(cost < bestCost) {
283 bestBidderAssignments = bidderAssignments;
289 if(numberOfRounds != -1 and iter >= numberOfRounds)
292 bidderAssignments = bestBidderAssignments;
295 for(
unsigned int bidderId = 0; bidderId < bidderAssignments.size();
297 int const i = bidderId;
298 int const j = bidderAssignments[bidderId];
299 if(this->balancedAssignment
300 or (not this->balancedAssignment
301 and not(i >= this->rowSize - 1 and j >= this->colSize - 1))) {
302 matchings.push_back(std::make_tuple(i, j, this->costMatrix[i][j]));
307 for(
unsigned int i = 0; i < goodPrices.size(); ++i)
308 goodPrices[i] += savedPrices[i];