MultiAgentDecisionProcess
Release 0.2.1
Main Page
Namespaces
Classes
Files
File List
File Members
BGIP_SolverBruteForceSearch.h
Go to the documentation of this file.
1
28
/* Only include this header file once. */
29
#ifndef _BGIP_SOLVERBRUTEFORCESEARCH_H_
30
#define _BGIP_SOLVERBRUTEFORCESEARCH_H_ 1
31
32
/* the include directives */
33
#include <iostream>
34
#include "
Globals.h
"
35
#include "
JointPolicyPureVector.h
"
36
#include "
BayesianGameIdenticalPayoffSolver.h
"
37
#include <float.h>
38
#include "
TimeTools.h
"
39
#include "
Referrer.h
"
40
41
#define DEBUG_BGIP_SOLVER_BFS 0
42
#define DEBUG_BGIP_SOLVER_BFS_PRINTOUTPROGRESS 0
43
44
51
template
<
class
JP>
52
class
BGIP_SolverBruteForceSearch
:
public
BayesianGameIdenticalPayoffSolver
<JP>
53
{
54
private
:
56
size_t
_m_verbosity
;
58
size_t
_m_nrSolutions
;
59
60
protected
:
61
62
public
:
63
// Constructor, destructor and copy assignment.
64
// (default) Constructor
65
//BGIP_SolverBruteForceSearch();
69
BGIP_SolverBruteForceSearch
(
const
BayesianGameIdenticalPayoffInterface
& bg,
70
size_t
verbose=0,
size_t
nrSolutions=1) :
71
BayesianGameIdenticalPayoffSolver
<JP>(bg),
72
_m_verbosity
(verbose),
73
_m_nrSolutions
(nrSolutions)
74
{}
75
76
double
Solve
()
77
{
78
struct
timeval start_time, cur_time;
79
if
(gettimeofday(&start_time, NULL) != 0)
80
throw
"Error with gettimeofday"
;
81
82
bool
round =
false
;
83
JP* jpol =
BayesianGameIdenticalPayoffSolver<JP>::GetNewJpol
();
//new JP(*BayesianGameIdenticalPayoffSolver<JP>::GetReferred());
84
JP best(*jpol);
85
double
v_best = -DBL_MAX;
86
double
v = 0.0;
87
88
int
i = 0;
89
if
(
DEBUG_BGIP_SOLVER_BFS
)
90
std::cout<<
"Starting Bruteforce search - v_best is init to "
<<v_best<<std::endl;
91
#if DEBUG_BGIP_SOLVER_BFS_PRINTOUTPROGRESS
92
LIndex
nrJPols =
GetReferred
()->
GetNrJointPolicies
();
93
#endif
94
95
while
(!round)
96
{
97
if
(
DEBUG_BGIP_SOLVER_BFS
){std::cout <<
"Jpol#"
<< i <<
" - "
;}
98
#if DEBUG_BGIP_SOLVER_BFS_PRINTOUTPROGRESS
99
if
(i % 1000 == 0&& i > 1000)
100
{
101
std::cout <<
"Jpol #"
<< i <<
" of "
<< nrJPols<<
" - "
;
102
printf(
"%.4f"
, ((
double
)i / nrJPols) * 100 );
103
std::cout <<
"%"
<< std::endl;
104
}
105
#endif
106
i++;
107
//v = jpol->CalculateV();
108
/*ValueFunctionDecPOMDPDiscrete vf(this, jpol);
109
v = vf.CalculateV0Recursively(true);//set caching to true!*/
110
111
size_t
nrJT =
BayesianGameIdenticalPayoffSolver<JP>::GetReferred
()->
GetNrJointTypes
();
112
//size_t nrJA = GetNrJointActions() const {return _m_nrJA;}
113
v = 0.0;
114
115
for
(
Index
jt = 0; jt < nrJT; jt++)
116
{
117
double
P_jt =
BayesianGameIdenticalPayoffSolver<JP>::GetReferred
()->
GetProbability
(jt);
118
Index
ja = jpol->GetJointActionIndex(jt);
119
v += P_jt *
BayesianGameIdenticalPayoffSolver<JP>::GetReferred
()->
GetUtility
(jt, ja);
120
}
121
122
if
(
DEBUG_BGIP_SOLVER_BFS
) std::cout <<
"Expected value = "
<< v;
123
if
(v > v_best)
124
{
125
if
(
DEBUG_BGIP_SOLVER_BFS
) std::cout <<
" -> new best policy!!!"
;
126
v_best = v;
127
best = *jpol;
128
if
(
BayesianGameIdenticalPayoffSolver<JP>::_m_writeAnyTimeResults
){
129
double
delta =
TimeTools::GetDeltaTimeDouble
(start_time, cur_time);
130
(*
BayesianGameIdenticalPayoffSolver<JP>::_m_results_f
) << v_best <<
"\t"
;
131
(*
BayesianGameIdenticalPayoffSolver<JP>::_m_timings_f
) << delta <<
"\t"
;
132
}
133
}
134
135
// if we want more than just the single best solution, try to add all to the solution
136
if
(
_m_nrSolutions
>1)
137
BayesianGameIdenticalPayoffSolver<JP>::_m_solution
.AddSolution(*(
new
JP(*jpol)), v );
138
139
if
(
DEBUG_BGIP_SOLVER_BFS
) {
140
std::cout << std::endl <<
"Incrementing joint policy..."
<<std::endl;
141
//std::cout << "jpol: "<<std::endl;
142
//jpol->Print();
143
//std::cout << "best: "<<std::endl;
144
//best.Print(); //use this to see if best remains
145
//unaffected...
146
}
147
round = ++(*jpol);
//also seems not to be the leak ?
148
}
149
//end the line in the results file
150
if
(
BayesianGameIdenticalPayoffSolver<JP>::_m_writeAnyTimeResults
){
151
(*
BayesianGameIdenticalPayoffSolver<JP>::_m_results_f
) << std::endl;
152
(*
BayesianGameIdenticalPayoffSolver<JP>::_m_timings_f
) << std::endl;
153
}
154
delete
jpol;
155
BayesianGameIdenticalPayoffSolver<JP>::_m_solution
.SetPolicy(best);
//best.Print();
156
BayesianGameIdenticalPayoffSolver<JP>::_m_solution
.SetPayoff(v_best);
157
158
// also store the solution in the priority queue
159
BayesianGameIdenticalPayoffSolver<JP>::_m_solution
.AddSolution(*(
new
JP(best)), v_best );
160
161
return
(v_best);
162
}
163
164
};
165
166
167
#endif
/* !_BGIP_SOLVERBRUTEFORCESEARCH_H_ */
168
169
// Local Variables: ***
170
// mode:c++ ***
171
// End: ***
src
planning
BGIP_SolverBruteForceSearch.h
Generated on Mon Sep 23 2013 14:50:05 for MultiAgentDecisionProcess by
1.8.1.2