MultiAgentDecisionProcess  Release 0.2.1
BGIPSolution.cpp
Go to the documentation of this file.
1 
28 #include "BGIPSolution.h"
29 #include <fstream>
30 #include "JPPVValuePair.h"
31 using namespace std;
32 
33 //Default constructor
36  size_t nrSolutions
37  ) :
38  _m_pu(pu),
39  _m_policy(0),
40  _m_jpolIndex(0),
41  _m_payoff(42),
42  _m_nrSolutions(nrSolutions),
43  _m_q(nrSolutions)
44 {
45 }
46 
47 //Destructor
49 {
50  // delete all the allocated JPPVValuePairs
51  while(!_m_q.empty())
52  {
53  delete _m_q.top();
54  _m_q.pop();
55  }
56  delete _m_policy;
57 }
58 
60 {
61  _m_policy = new JointPolicyPureVector(jpol);
62  //change this to a non-index based solution for now
63  //(LIndex are not long enough...)
64 }
65 
67 {
68  if(_m_policy==0)
70  _m_policy->SetIndex(jpolIndex);
71  _m_jpolIndex=jpolIndex;
72 }
73 
74 void BGIPSolution::Save(string filename) const
75 {
76  ofstream fp(filename.c_str());
77  if(!fp)
78  {
79  stringstream ss;
80  ss << "BGIPSolution::Save: failed to open file " << filename << endl;
81  throw E(ss);
82  }
83 
84  fp.precision(16);
85  fp << _m_jpolIndex << " " << _m_payoff << endl;
86 }
87 
88 void BGIPSolution::Load(string filename)
89 {
90  const int bufsize=65536;
91  char buffer[bufsize];
92 
93  ifstream fp(filename.c_str());
94  if(!fp)
95  {
96  stringstream ss;
97  ss << "BGIPSolution::Load: failed to "
98  << "open file " << filename << endl;
99  throw E(ss);
100  }
101 
102  LIndex index;
103  double payoff;
104 
105  fp.getline(buffer,bufsize);
106  istringstream is(buffer);
107  is >> index;
108  is >> payoff;
109 
110  SetPolicy(index);
111  SetPayoff(payoff);
112 }
113 
115 {
116  stringstream ss;
117  ss << "JPol" << _m_jpolIndex << "Payoff" << _m_payoff;
118  return(ss.str());
119 }
120 
121 #define DEBUG_ADD 0
123  double value)
124 {
125  JPPVValuePair * p = new JPPVValuePair(jp, value );
126 
127  Index pI = p->GetJPPV()->GetIndex();
128 #if DEBUG_ADD
129  double v1 = p->GetValue();
130  cout << "inserting policy# "<< pI << " with value " << v1 << "...";
131 #endif
132  if(_m_jpolIndices.find(pI) != _m_jpolIndices.end() )
133  {
134 #if DEBUG_ADD
135  cout << " NOT inserting (already in the queue)" << endl;
136 #endif
137  delete p;
138  return; //policy is already in the queue
139  }
140  _m_jpolIndices.insert(pI);
141  JPPVValuePair* overflownvp_p = NULL;
142  if( _m_q.insert(p, overflownvp_p) )
143  {
144  Index pI2 = overflownvp_p->GetJPPV()->GetIndex();
145 #if DEBUG_ADD
146  double v2 = overflownvp_p->GetValue();
147  cout << "Removing overflown policy - policy# "<< pI2 <<
148  " (with value "<<v2<<") "<< endl;
149 #endif
150  _m_jpolIndices.erase( pI2 );
151 // delete overflown->GetJPol(); //not nec. destructor of JPPVValuePair handles this
152  delete overflownvp_p;
153  }
154  else
155  {
156 #if DEBUG_ADD
157  cout << "added without overflow."<< endl;
158 #endif
159  }
160 }