forked from bonetblai/mini-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cc
499 lines (445 loc) · 13 KB
/
client.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "global.h"
#include "client.h"
#include "domains.h"
#include "exceptions.h"
#include "hash.h"
#include "planners.h"
#include "strxml.h"
#include "states.h"
#include <assert.h>
static int
sessionRequestInfo( XMLNodePtr node, std::ostream &os,
int& rounds, float& time, int& turns )
{
XMLNodePtr settingNode = node->getChild( "setting" );
if( !settingNode ) return( 0 );
std::string s;
if( !dissectNode( settingNode, "rounds", s ) ) return( 0 );
rounds = atoi( s.c_str() );
if( !dissectNode( settingNode, "allowed-time", s ) ) return( 0 );
time = atof( s.c_str() );
if( !dissectNode( settingNode, "allowed-turns", s ) ) return( 0 );
turns = atoi( s.c_str() );
if( gpt::verbosity > 0 )
{
os << "<session>: start: rounds = " << rounds << std::endl;
os << "<session>: start: allowed time = " << time << std::endl;
os << "<session>: start: turns = " << turns << std::endl;
}
return( 1 );
}
static void
sessionEndInfo( XMLNodePtr node, std::ostream &os, unsigned total_steps )
{
if( !node || (node->getName() != "end-session") )
return;
else
{
assert( node->size() == 4 );
XMLNodePtr id = node->getChild( 0 );
XMLNodePtr rounds = node->getChild( 1 );
XMLNodePtr goals = node->getChild( 2 );
XMLNodePtr failed = goals->getChild( 0 );
XMLNodePtr reached = goals->getChild( 1 );
XMLNodePtr success = reached->getChild( 0 );
XMLNodePtr time = reached->getChild( 1 );
XMLNodePtr metric = node->getChild( 3 );
unsigned number_success = atoi( success->getText().c_str() );
os << "<session>: id = " << id->getText() << std::endl;
os << "<session>: rounds = " << rounds->getText() << std::endl;
os << "<session>: failed = " << failed->getText() << std::endl;
if( time )
{
os << "<session>: successes = " << number_success << std::endl;
os << "<session>: avg. time = " << time->getText() << std::endl;
}
else
os << "<session>: successes = " << number_success << std::endl;
os << "<session>: avg. steps in successful rounds = "
<< (number_success==0?0:total_steps/number_success) << std::endl;
os << "<session>: metric-average = " << metric->getText() << std::endl;
}
}
static void
roundEndInfo( XMLNodePtr node, std::ostream &os, unsigned &total_steps )
{
if( !node || (node->getName() != "end-round") )
return;
else
{
XMLNodePtr round, time, turns;
assert( (node->size() == 5) || (node->size() == 6) );
round = node->getChild( 0 );
if( node->size() == 6 )
{
time = node->getChild( 4 );
turns = node->getChild( 5 );
total_steps += atoi( turns->getText().c_str() );
if( gpt::verbosity >= 50 )
os << "<round>: goal reached!!!" << std::endl;
}
else
{
time = node->getChild( 3 );
turns = node->getChild( 4 );
}
if( gpt::verbosity >= 50 )
{
os << "<round>: round = " << round->getText() << std::endl;
os << "<round>: time = " << time->getText() << std::endl;
os << "<round>: turns = " << turns->getText() << std::endl;
}
}
}
/*******************************************************************************
*
* XML client
*
******************************************************************************/
XMLClient_t::XMLClient_t( planner_t *planner, const problem_t *problem,
std::string name, std::istream& is, std::ostream& os )
: problem_(problem), planner_(planner)
{
const state_t *state;
const action_t *action;
// create display
display_ = new std::pair<state_t*,Rational>[DISP_INT_SIZE];
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
display_[i].first = new state_t;
// initialize random planner if necessary
planner_t *random = 0;
heuristic_t *zero = 0;
if( gpt::noise )
{
zero = new zeroHeuristic_t( *problem_ );
random = new plannerRANDOM_t( *problem_, *zero );
}
// initiate sesssion
os << "<session-request>"
<< "<name>" << name << "</name>"
<< "<problem>" << problem_->name() << "</problem>"
<< "</session-request>" << std::endl;
// read session parameters
XMLNodePtr sessionInitNode = 0;
is >> sessionInitNode;
if( !sessionRequestInfo( sessionInitNode, std::cout,
total_rounds_, round_time_, round_turns_ ) )
{
std::cout << "<client>: ERROR: session-request response: " << sessionInitNode << std::endl;
return;
}
delete sessionInitNode;
unsigned total_steps = 0;
for( int rounds_left = total_rounds_; rounds_left > 0; --rounds_left )
{
// initiate round
os << "<round-request/>" << std::endl;
// read round parameters
XMLNodePtr roundInitNode = 0;
is >> roundInitNode;
if( !roundInitNode || (roundInitNode->getName() != "round-init") )
{
std::cout << "<client>: ERROR: round-request response: " << roundInitNode << std::endl;
delete roundInitNode;
return;
}
delete roundInitNode;
#ifndef NDEBUG
problem_->initial_states( display_ );
for( size_t i = 0; display_[i].second != Rational( -1 ); ++i )
{
if( gpt::verbosity >= 500 )
{
std::cout << "<initial-state>: ";
display_[i].first->print( std::cout );
std::cout << std::endl;
}
if( !display_[i].first->make_check() ||
!(display_[i].first->digest() == display_[i].first->hash_value()) )
throw Exception( "inconsistency found in initial state" );
}
#endif
// init rount within planner
planner_->initRound();
XMLNodePtr response = 0;
while( true )
{
// read server
is >> response;
if( !response )
{
std::cout << "<client>: ERROR: invalid state response: " << response << std::endl;
return;
}
// check if end of round or session
if( (response->getName() == "end-round") ||
(response->getName() == "end-session") )
break;
// read state
if( !(state = getState( response )) )
{
std::cout << "<client>: ERROR: invalid state response: " << response << std::endl;
return;
}
// check for dead-end and/or goal
if( planner_->dead_end( *state ) )
{
sendDone( os );
std::cout << " +(</done>)" << std::endl;
std::cout << "<client>: state ";
state->print( std::cout );
std::cout << " is dead-end!!!" << std::endl;
continue;
}
if( problem_->goal().holds( *state ) )
{
std::cout << "<client>: state ";
state->print( std::cout );
std::cout << " is goal!!!" << std::endl;
}
#ifndef NDEBUG
{
size_t i = 0;
for( ; display_[i].second != Rational( -1 ); ++i )
if( *display_[i].first == *state ) break;
if( display_[i].second == Rational( -1 ) )
{
std::cout << "state = ";
state->full_print( std::cout, problem_ );
std::cout << std::endl;
std::cout << std::endl;
for( size_t i = 0; display_[i].second != Rational( -1 ); ++i )
{
display_[i].first->full_print( std::cout, problem_ );
std::cout << std::endl;
}
throw Exception( "unexpected state 1111" );
}
}
#endif
// generate and send action
if( gpt::noise && (drand48() < gpt::noise_level) )
action = random->decideAction( *state );
else
action = planner_->decideAction( *state );
if( action == NULL )
sendDone( os );
else
sendAction( os, action );
if( gpt::verbosity >= 100 )
{
if( gpt::verbosity >= 150 )
{
std::cout << "<state >: ";
state->full_print( std::cout, problem_ );
std::cout << std::endl;
std::cout << "<action>: ";
}
if( action )
{
std::cout << " +";
planner_->print( std::cout, *action );
std::cout << std::endl;
}
else
std::cout << " +(</done>)" << std::endl;
}
#ifndef NDEBUG
if( action )
{
problem_->expand( *action, *state, display_ );
for( size_t i = 0; display_[i].second != Rational( -1 ); ++i )
{
if( gpt::verbosity >= 500 )
{
std::cout << "<expansion>: ";
display_[i].first->print( std::cout );
std::cout << std::endl;
}
if( !display_[i].first->make_check() ||
!(display_[i].first->digest() == display_[i].first->hash_value()) )
throw Exception( "inconsistency found in state" );
}
}
#endif
delete state;
}
planner_->endRound();
if( response )
{
if( response->getName() == "end-round" )
roundEndInfo( response, std::cout, total_steps );
else if(response->getName() == "end-session" )
{
delete response;
break;
}
}
delete response;
}
XMLNodePtr endSessionNode = 0;
is >> endSessionNode;
if( endSessionNode )
{
sessionEndInfo( endSessionNode, std::cout, total_steps );
delete endSessionNode;
}
// clean random planner
if( gpt::noise )
{
delete zero;
delete random;
}
}
XMLClient_t::~XMLClient_t()
{
for( size_t i = 0; i < DISP_INT_SIZE; ++i )
delete display_[i].first;
delete[] display_;
}
const state_t*
XMLClient_t::getState( XMLNodePtr stateNode )
{
if( !stateNode || (stateNode->getName() != "state") )
return( NULL );
else
{
state_t *s = new state_t;
for( int i = 0; i < stateNode->size(); ++i )
{
XMLNodePtr cn = stateNode->getChild( i );
if( cn->getName() == "atom" )
{
const Atom *a = getAtom( cn );
s->add( *a );
StateFormula::unregister_use( a );
}
else if( cn->getName() == "fluent" )
{
#ifdef FULL_STATES
const Application *a = getApplication( cn );
std::string value_str;
if( !dissectNode( cn, "value", value_str ) )
return( NULL );
s->add( *a, Rational( value_str.c_str() ) );
#else
if( gpt::warning_level > 0 )
{
std::cout << "<client>: WARNING: fluent `";
cn->print( std::cout );
std::cout << "' received; ignoring." << std::endl;
}
#endif
}
}
problem_->complete_state( *s );
s->make_digest();
return( s );
}
}
const Application*
XMLClient_t::getApplication( XMLNodePtr appNode )
{
if( !appNode || (appNode->getName() != "fluent") )
return( NULL );
else
{
// get function name
std::string function_name;
if( !dissectNode( appNode, "function", function_name ) )
return( NULL );
const Domain& domain = problem_->domain();
std::pair<Function,bool> p = domain.functions().find_function( function_name );
if( !p.second ) return( NULL );
Function function = p.first;
// get terms
TermList terms;
int argIndex = 0;
for( int i = 0; i < appNode->size(); ++i )
{
XMLNodePtr termNode = appNode->getChild( i );
if( !termNode || (termNode->getName() != "term") )
continue;
Type ptype = domain.functions().parameter( function, argIndex++ );
std::string term_name = termNode->getText();
std::pair<Object,bool> o = problem_->terms().find_object( term_name );
if( o.second )
{
Type otype = problem_->terms().type( o.first );
if( !domain.types().subtype( otype, ptype ) )
return( NULL );
}
else
{
o = domain.terms().find_object( term_name );
Type otype = problem_->terms().type( o.first );
if( !domain.types().subtype( otype, ptype ) )
return( NULL );
}
terms.push_back(o.first);
}
if( domain.functions().arity( function ) != terms.size() )
return( NULL );
const Application& a = Application::make_application( function, terms );
return( &a );
}
}
const Atom*
XMLClient_t::getAtom( XMLNodePtr atomNode )
{
if( !atomNode || (atomNode->getName() != "atom") )
return( NULL );
else
{
// get predicate name
std::string predicate_name;
if( !dissectNode( atomNode, "predicate", predicate_name ) )
return( NULL );
const Domain& domain = problem_->domain();
std::pair<Predicate,bool> p = domain.predicates().find_predicate( predicate_name );
if( !p.second ) return( NULL );
Predicate predicate = p.first;
// get terms
TermList terms;
int argIndex = 0;
for( int i = 0; i < atomNode->size(); ++i )
{
XMLNodePtr termNode = atomNode->getChild( i );
if( !termNode || (termNode->getName() != "term") )
continue;
Type ptype = domain.predicates().parameter( predicate, argIndex++ );
std::string term_name = termNode->getText();
std::pair<Object,bool> o = problem_->terms().find_object( term_name );
if( o.second )
{
Type otype = problem_->terms().type( o.first );
if( !domain.types().subtype( otype, ptype ) )
return( NULL );
}
else
{
o = domain.terms().find_object( term_name );
Type otype = problem_->terms().type( o.first );
if( !domain.types().subtype( otype, ptype ) )
return( NULL );
}
terms.push_back( o.first );
}
if( domain.predicates().arity( predicate ) != terms.size() )
return( NULL );
const Atom& a = Atom::make_atom( predicate, terms );
return( &a );
}
}
void
XMLClient_t::sendDone( std::ostream& os ) const
{
os << "<done></done>" << std::endl;
}
void
XMLClient_t::sendAction( std::ostream& os, const action_t *a ) const
{
os << "<act>";
a->printXML( os );
os << "</act>" << std::endl;
}