forked from holderlb/wumpus-world-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wumpsim.cc
162 lines (150 loc) · 3.45 KB
/
wumpsim.cc
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
// wumpsim.cc
//
// Main Wumpus Simulator procedure.
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include "Percept.h"
#include "Action.h"
#include "WumpusWorld.h"
#include "wumpsim.h"
#ifdef PYTHON
#include <Python.h>
#include "PyAgent.h"
#else
#include "Agent.h"
#endif
using namespace std;
int main (int argc, char *argv[])
{
int worldSize = 4;
int numTrials = 1;
int numTries = 1;
unsigned seed;
char* worldFile;
bool seedSet = false;
bool worldSet = false;
#ifdef PYTHON
PyObject *pName, *pModule;
Py_Initialize();
#if PY_MAJOR_VERSION >= 3
pName = PyUnicode_FromString("PyAgent");
#else
pName = PyString_FromString("PyAgent");
#endif
pModule = PyImport_Import(pName);
if (pModule == NULL) {
cout << "pModule = NULL\n";
PyErr_Print();
exit(1);
}
Py_DECREF(pName);
#endif
// Process command-line options
int i = 1;
while (i < argc)
{
if (strcmp (argv[i], "-size") == 0)
{
i++;
worldSize = atoi (argv[i]);
if (worldSize < 2)
{
worldSize = 2;
}
} else if (strcmp (argv[i], "-trials") == 0)
{
i++;
numTrials = atoi (argv[i]);
} else if (strcmp (argv[i], "-tries") == 0)
{
i++;
numTries = atoi (argv[i]);
} else if (strcmp (argv[i], "-seed") == 0)
{
i++;
seed = atoi (argv[i]);
seedSet = true;
} else if (strcmp (argv[i], "-world") == 0)
{
i++;
worldFile = argv[i];
worldSet = true;
} else {
cout << "unknown option " << argv[i] << endl;
exit (1);
}
i++;
}
// Set random number generator seed
if (! seedSet)
{
seed = (unsigned) time (0);
}
srand (seed);
// Print welcome
cout << "Welcome to the Wumpus World Simulator v";
cout << WUMPSIM_VERSION << ". Happy hunting!" << endl << endl;
// Run trials
WumpusWorld* wumpusWorld = NULL;
Agent* agent = NULL;
Percept percept;
Action action;
int score;
int trialScore;
int totalScore = 0;
float averageScore;
int numMoves;
for (int trial = 1; trial <= numTrials; trial++)
{
if (worldSet)
{
wumpusWorld = new WumpusWorld (worldFile);
} else {
wumpusWorld = new WumpusWorld (worldSize);
}
//wumpusWorld->Write (".world");
#ifdef PYTHON
agent = new Agent (pModule);
#else
agent = new Agent ();
#endif
trialScore = 0;
for (int tries = 1; tries <= numTries; tries++)
{
wumpusWorld->Initialize();
agent->Initialize ();
numMoves = 0;
cout << "Trial " << trial << ", Try " << tries << " begin" << endl << endl;
while ((! wumpusWorld->GameOver()) && (numMoves < MAX_MOVES_PER_GAME))
{
wumpusWorld->Print();
percept = wumpusWorld->GetPercept();
action = agent->Process (percept);
cout << "Action = ";
PrintAction (action);
cout << endl << endl;
wumpusWorld->ExecuteAction (action);
numMoves++;
}
score = wumpusWorld->GetScore();
agent->GameOver (score);
trialScore = trialScore + score;
cout << "Trial " << trial << ", Try " << tries << " complete: Score = " << score << endl << endl;
}
delete agent;
delete wumpusWorld;
averageScore = ((float) trialScore) / ((float) numTries);
cout << "Trial " << trial << " complete: Average score for trial = " << averageScore << endl << endl;
totalScore = totalScore + trialScore;
}
averageScore = ((float) totalScore) / ((float) (numTrials * numTries));
cout << "All trials completed: Average score for all trials = " << averageScore << endl;
cout << "Thanks for playing!" << endl << endl;
#ifdef PYTHON
//Py_DECREF(pModule);
Py_Finalize();
#endif
return 0;
}