root/ggpa/Table.cpp

Revision 1, 4.0 kB (checked in by pantley2, 4 years ago)

GGPA code from the good old days of SIGART

Line 
1 #include "Table.h"
2 #include <iostream>
3 #include <fstream>
4 #include <map>
5 #define INVALID -.000001
6
7 /*
8  * update
9  * Parameters:   state - a State object that represents a state in the game
10  *               action - an Action object that represents an action in the
11  *                        game
12  *               utility - a double that represents the utility to insert
13  *                         for this State/Action pair
14  * No return value
15  *
16  * Updates the entry for the given State/Action pair.
17  */
18 void Table::update(State &state, Action &action, double utility)
19 {
20     pair<State, Action> thePair(state, action);
21     tableMap[thePair] = utility;
22
23     return;
24 }
25
26 /*
27  * getValue
28  * Parameters:   state - a State object that represents a state in the game
29  *               action - an Action object that represents an action in the
30  *                        game
31  * Return value: a double representing the utility of the given State/
32  *   Action pair.
33  * This function will return the current utility of the given State/Action
34  *   pair.  In other words, it will return a value representing how good
35  *   the action is from the state.
36  */
37 double Table::getValue(State &state, Action &action)
38 {
39     pair<State, Action> thePair(state, action);
40
41     //check to see if the value is present in the map already
42     if (tableMap.find(thePair) != tableMap.end()) {
43         // if so, just return the value
44         return tableMap[thePair];
45     }
46
47     // otherwise, generate a default value (either small random number or 0),
48     // and insert it into the map so that it will be consistent
49     double newUtility = 0;
50     if (!DEFAULT_ZERO) {
51         // create a small random number between NUM_MIN and NUM_MAX
52         // get a random number between 0 and 1
53         double num = (double) rand() / ((double) RAND_MAX + (double) 1);
54         // scale it to the range NUM_MIN to NUM_MAX
55         newUtility = num * (NUM_MAX - NUM_MIN) + NUM_MIN;
56    
57     }
58
59     // set thePair to use copies of state and action
60     //thePair.first = new State(state);
61     //thePair.second = new Action(action);
62         
63     // add the new value to the map
64     tableMap[thePair] = newUtility;
65
66     return newUtility;
67
68     /*
69     map<pair<State*, Action*>, double, StateActionCmp>::iterator findItr = tableMap.begin();
70     while(!(*findItr == tableMap.end()) && (!(findItr->first.first == state)
71                 || !(findItr->first.second == action)))
72
73     {
74         findItr++;
75     }
76     if(findItr != tableMap.end())
77         return findItr->second;
78     else
79         return INVALID;
80     */
81     //return 0;
82 }
83
84 /*
85  * writeToFile
86  *
87  * Parameters:   file - String containing the name of the file to write to.
88  * Return value: none
89  *
90  * Writes the QFunction to the given file.  If the file does not exist, it
91  *   will be created.  If it does exist, then it will be written to the
92  *   end of the file
93  */
94 void Table::writeToFile(string filename)
95 {
96     /*
97     ofstream filename;
98     filename.open ("filename");
99     iterator itr;
100     for(itr = tableMap.begin(); itr != tableMap.end(); itr++)
101     {
102         filename << itr->first.first << "\n";
103         filename << itr->first.second << "\n";
104         filename << itr->second << "\n";
105     }
106     filename << "End.\n";
107     filename.close();
108     */
109 }
110
111 /*
112  * readFromFile
113  *
114  * Parameters:   file - string containing the name of the file to read from
115  * Return value: none
116  *
117  * Reads in a QFunction from a file.  The current QFunction will be set to
118  *    be this saved QFunction
119  */
120 void Table::readFromFile(string filename)
121 {
122     /*
123     ifstream myfile;
124     String state;
125     String action;
126     pair<Action, State> thePair;
127     String utility;
128     myfile.open ("filename");
129     if (myfile.is_open())
130     {
131         state << myfile;
132         while(state != "End.")
133         {
134             action << myfile;
135             utility << myfile;
136             thePair.first = state;
137             thePair.second = action;
138             tableMap.insert(make_pair(thePair, utility));
139             state << filename;
140         }
141
142         }
143         
144         myfile.close();
145     }
146
147   else cout << "Unable to open file";
148   */
149 }
Note: See TracBrowser for help on using the browser.