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
|
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/optimization.h>
#include <dlib/rand.h>
#include "tester.h"
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
logger dlog("test.find_max_factor_graph_viterbi");
// ----------------------------------------------------------------------------------------
dlib::rand rnd;
// ----------------------------------------------------------------------------------------
template <
unsigned long O,
unsigned long NS,
unsigned long num_nodes,
bool all_negative
>
class map_problem
{
public:
unsigned long order() const { return O; }
unsigned long num_states() const { return NS; }
map_problem()
{
data = randm(number_of_nodes(),(long)std::pow(num_states(),(double)order()+1), rnd);
if (all_negative)
data = -data;
}
unsigned long number_of_nodes (
) const
{
return num_nodes;
}
template <
typename EXP
>
double factor_value (
unsigned long node_id,
const matrix_exp<EXP>& node_states
) const
{
if (node_states.size() == 1)
return data(node_id, node_states(0));
else if (node_states.size() == 2)
return data(node_id, node_states(0) + node_states(1)*NS);
else if (node_states.size() == 3)
return data(node_id, (node_states(0) + node_states(1)*NS)*NS + node_states(2));
else
return data(node_id, ((node_states(0) + node_states(1)*NS)*NS + node_states(2))*NS + node_states(3));
}
matrix<double> data;
};
// ----------------------------------------------------------------------------------------
template <
typename map_problem
>
void brute_force_find_max_factor_graph_viterbi (
const map_problem& prob,
std::vector<unsigned long>& map_assignment
)
{
using namespace dlib::impl;
const int order = prob.order();
const int num_states = prob.num_states();
map_assignment.resize(prob.number_of_nodes());
double best_score = -std::numeric_limits<double>::infinity();
matrix<unsigned long,1,0> node_states;
node_states.set_size(prob.number_of_nodes());
node_states = 0;
do
{
double score = 0;
for (unsigned long i = 0; i < prob.number_of_nodes(); ++i)
{
score += prob.factor_value(i, (colm(node_states,range(i,i-std::min<int>(order,i)))));
}
if (score > best_score)
{
for (unsigned long i = 0; i < map_assignment.size(); ++i)
map_assignment[i] = node_states(i);
best_score = score;
}
} while(advance_state(node_states,num_states));
}
// ----------------------------------------------------------------------------------------
template <
unsigned long order,
unsigned long num_states,
unsigned long num_nodes,
bool all_negative
>
void do_test_()
{
dlog << LINFO << "order: "<< order
<< " num_states: " << num_states
<< " num_nodes: " << num_nodes
<< " all_negative: " << all_negative;
for (int i = 0; i < 25; ++i)
{
print_spinner();
map_problem<order,num_states,num_nodes,all_negative> prob;
std::vector<unsigned long> assign, assign2;
brute_force_find_max_factor_graph_viterbi(prob, assign);
find_max_factor_graph_viterbi(prob, assign2);
DLIB_TEST_MSG(vector_to_matrix(assign) == vector_to_matrix(assign2),
trans(vector_to_matrix(assign))
<< trans(vector_to_matrix(assign2))
);
}
}
template <
unsigned long order,
unsigned long num_states,
unsigned long num_nodes
>
void do_test()
{
do_test_<order,num_states,num_nodes,false>();
}
template <
unsigned long order,
unsigned long num_states,
unsigned long num_nodes
>
void do_test_negative()
{
do_test_<order,num_states,num_nodes,true>();
}
// ----------------------------------------------------------------------------------------
class test_find_max_factor_graph_viterbi : public tester
{
public:
test_find_max_factor_graph_viterbi (
) :
tester ("test_find_max_factor_graph_viterbi",
"Runs tests on the find_max_factor_graph_viterbi routine.")
{}
void perform_test (
)
{
do_test<1,3,0>();
do_test<1,3,1>();
do_test<1,3,2>();
do_test<0,3,2>();
do_test_negative<0,3,2>();
do_test<1,3,8>();
do_test<2,3,7>();
do_test_negative<2,3,7>();
do_test<3,3,8>();
do_test<4,3,8>();
do_test_negative<4,3,8>();
do_test<0,3,8>();
do_test<4,3,1>();
do_test<4,3,0>();
do_test<3,2,1>();
do_test<3,2,0>();
do_test<3,2,2>();
do_test<2,2,1>();
do_test_negative<3,2,1>();
do_test_negative<3,2,0>();
do_test_negative<3,2,2>();
do_test_negative<2,2,1>();
do_test<0,3,0>();
do_test<1,2,8>();
do_test<2,2,7>();
do_test<3,2,8>();
do_test<0,2,8>();
do_test<1,1,8>();
do_test<2,1,8>();
do_test<3,1,8>();
do_test<0,1,8>();
}
} a;
}
|