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
|
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <dlib/optimization.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "../rand.h"
#include "tester.h"
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
logger dlog("test.max_cost_assignment");
// ----------------------------------------------------------------------------------------
std::vector<std::vector<long> > permutations (
matrix<long,1,0> vals
)
{
if (vals.size() == 0)
{
return std::vector<std::vector<long> >();
}
else if (vals.size() == 1)
{
return std::vector<std::vector<long> >(1,std::vector<long>(1,vals(0)));
}
std::vector<std::vector<long> > temp;
for (long i = 0; i < vals.size(); ++i)
{
const std::vector<std::vector<long> >& res = permutations(remove_col(vals,i));
for (unsigned long j = 0; j < res.size(); ++j)
{
temp.resize(temp.size()+1);
std::vector<long>& part = temp.back();
part.push_back(vals(i));
part.insert(part.end(), res[j].begin(), res[j].end());
}
}
return temp;
}
// ----------------------------------------------------------------------------------------
template <typename T>
std::vector<long> brute_force_max_cost_assignment (
matrix<T> cost
)
{
if (cost.size() == 0)
return std::vector<long>();
const std::vector<std::vector<long> >& perms = permutations(range(0,cost.nc()-1));
T best_cost = std::numeric_limits<T>::min();
unsigned long best_idx = 0;
for (unsigned long i = 0; i < perms.size(); ++i)
{
const T temp = assignment_cost(cost, perms[i]);
if (temp > best_cost)
{
best_idx = i;
best_cost = temp;
}
}
return perms[best_idx];
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class test_max_cost_assignment : public tester
{
public:
test_max_cost_assignment (
) :
tester ("test_max_cost_assignment",
"Runs tests on the max_cost_assignment function.")
{}
dlib::rand rnd;
template <typename T>
void test_hungarian()
{
long size = rnd.get_random_32bit_number()%7;
long range = rnd.get_random_32bit_number()%100;
matrix<T> cost = matrix_cast<T>(randm(size,size,rnd)*range) - range/2;
// use a uniform cost matrix sometimes
if ((rnd.get_random_32bit_number()%100) == 0)
cost = rnd.get_random_32bit_number()%100;
// negate the cost matrix every now and then
if ((rnd.get_random_32bit_number()%100) == 0)
cost = -cost;
std::vector<long> assign = brute_force_max_cost_assignment(cost);
T true_eval = assignment_cost(cost, assign);
assign = max_cost_assignment(cost);
DLIB_TEST(assignment_cost(cost,assign) == true_eval);
assign = max_cost_assignment(matrix_cast<char>(cost));
DLIB_TEST(assignment_cost(cost,assign) == true_eval);
cost = matrix_cast<T>(randm(size,size,rnd)*range);
assign = brute_force_max_cost_assignment(cost);
true_eval = assignment_cost(cost, assign);
assign = max_cost_assignment(cost);
DLIB_TEST(assignment_cost(cost,assign) == true_eval);
assign = max_cost_assignment(matrix_cast<unsigned char>(cost));
DLIB_TEST(assignment_cost(cost,assign) == true_eval);
assign = max_cost_assignment(matrix_cast<typename unsigned_type<T>::type>(cost));
DLIB_TEST(assignment_cost(cost,assign) == true_eval);
}
void perform_test (
)
{
for (long i = 0; i < 1000; ++i)
{
if ((i%100) == 0)
print_spinner();
test_hungarian<short>();
test_hungarian<int>();
test_hungarian<long>();
test_hungarian<int64>();
}
}
} a;
}
|