-
Notifications
You must be signed in to change notification settings - Fork 50
/
agents.jl
916 lines (751 loc) · 27.6 KB
/
agents.jl
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
using Statistics;
import Base: run;
export AgentProgram,
TableDrivenAgentProgram,
ReflexVacuumAgentProgram,
ModelBasedVacuumAgentProgram,
RandomAgentProgram,
Rule, SimpleReflexAgentProgram,
ModelBasedReflexAgentProgram,
EnvironmentObject, EnvironmentAgent,
Agent, Wumpus, Explorer,
isAlive, setAlive,
Obstacle, Wall,
Dirt, Gold, Pit, Arrow,
execute, rule_match, interpret_input, update_state,
TableDrivenVacuumAgent,
ReflexVacuumAgent,
ModelBasedVacuumAgent,
RandomVacuumAgent,
Environment, TwoDimensionalEnvironment, XYEnvironment,
VacuumEnvironment, TrivialVacuumEnvironment, WumpusEnvironment,
percept, objects_near, get_objects_at, some_objects_at, is_done, step,
run, exogenous_change, default_location, environment_objects, execute_action,
add_object, delete_object, add_walls, move_to,
run_once,
test_agent, compare_agents;
#=
Define a global execute() function to be implemented for each respective
AgentProgram DataType implementation.
=#
"""
execute(ap::T, p::Tuple{Any, Any}) where {T <: AgentProgram}
Return an action using the given agent program 'ap' and percept 'p'.
"""
function execute(ap::T, p::Tuple{Any, Any}) where {T <: AgentProgram} # implement methods for other AgentPrograms later
# comment the following line to reduce verbosity
#println("execute() is not implemented yet for ", typeof(ap), "!");
nothing;
end
#=
TableDrivenAgentProgram is an agent program implementation consisting of a table of
mappings of percepts to actions.
=#
struct TableDrivenAgentProgram <: AgentProgram
isTracing::Bool
percepts::Array{Tuple{Any, Any}, 1}
table::Dict{Any, Any}
function TableDrivenAgentProgram(;table_dict::Union{Nothing, Dict{Any, Any}}=nothing, trace::Bool=false)
if (table_dict == nothing) #no table given, create empty dictionary
return new(Bool(trace), Array{Tuple{Any, Any}, 1}(), Dict{Any, Any}());
else
return new(Bool(trace), Array{Tuple{Any, Any}, 1}(), table_dict);
end
end
end
#=
ReflexVacuumAgentProgram is an agent program implementation of AgentProgram
in the two-state vacuum environment.
=#
struct ReflexVacuumAgentProgram <: AgentProgram
isTracing::Bool
function ReflexVacuumAgentProgram(;trace::Bool=false)
return new(Bool(trace));
end
end
#=
ModelBasedVacuumAgentProgram is an agent program implementation that contains
the model of the vacuum environment.
=#
mutable struct ModelBasedVacuumAgentProgram <: AgentProgram
isTracing::Bool
model::Dict{Any, Any}
function ModelBasedVacuumAgentProgram(;trace::Bool=false, model::Union{Nothing, Dict{Any, Any}}=nothing)
if (typeof(model) <: Dict{Any, Any})
new_ap = new(Bool(trace));
new_ap.model = deepcopy(model);
return new_ap;
else
return new(Bool(trace), Dict{Any, Any}());
end
end
end
#=
RandomAgentProgram is an agent program implementation that contains
all possible actions in the environment of the agent.
=#
struct RandomAgentProgram <: AgentProgram
isTracing::Bool
actions::Array{String, 1}
function RandomAgentProgram(actions::Array{String, 1}; trace::Bool=false)
return new(Bool(trace), deepcopy(actions));
end
end
struct Rule # condition-action rules
condition::String
action::String
function Rule(cond::String, action::String)
return new(cond, action);
end
end
#=
SimpleReflexAgentProgram is an agent program implementation that contains
a set of condition-action rules.
=#
struct SimpleReflexAgentProgram <: AgentProgram
isTracing::Bool
rules::Array{Rule, 1}
function SimpleReflexAgentProgram(rules_array::Array{Rule, 1};trace::Bool=false)
srap = new(Bool(trace));
srap.rules = deepcopy(rules_array);
return rules_array;
end
end
#=
ModelBasedReflexAgentProgram is an agent program implementation that contains
a set of condition-action rules and the model of the environment.
=#
mutable struct ModelBasedReflexAgentProgram <: AgentProgram
isTracing::Bool
state::Dict{Any, Any}
model::Dict{Any, Any}
rules::Array{Rule, 1}
action::String # most recent action, initialized to empty string ""
function ModelBasedReflexAgentProgram(state::Dict{Any, Any}, model::Dict{Any, Any}, rules::Array{Rule, 1}; trace::Bool=false)
mbrap = new(Bool(trace));
mbrap.state = deepcopy(state);
mbrap.model = deepcopy(model);
mbrap.rules = deepcopy(rules);
return mbrap;
end
end
#=
Agents can interact with the environment through percepts and actions.
=#
# Declare EnvironmentObject as a supertype for EnvironmentObject implementations
abstract type EnvironmentObject end;
# The EnvironmentAgent implementations exist in the environment like other EnvironmentObjects such as Gold or Dirt
abstract type EnvironmentAgent <: EnvironmentObject end;
mutable struct Agent <: EnvironmentAgent
alive::Bool
performance::Float64
bump::Bool
heading::Tuple{Any, Any}
holding::Array{Any, 1}
program::AgentProgram
location::Tuple{Any, Any} # initialized when adding agent to environment
function Agent()
return new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}());
end
function Agent(ap::T) where {T <: AgentProgram}
new_agent = new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}()); # program is undefined
new_agent.program = ap;
return new_agent;
end
end
mutable struct Wumpus <: EnvironmentAgent
alive::Bool
performance::Float64
bump::Bool
heading::Tuple{Any, Any}
holding::Array{Any, 1}
program::AgentProgram
location::Tuple{Any, Any} # initialized when adding agent to environment
function Wumpus()
return new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}());
end
function Wumpus(ap::T) where {T <: AgentProgram}
new_agent = new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}()); # program is undefined
new_agent.program = ap;
return new_agent;
end
end
mutable struct Explorer <: EnvironmentAgent
alive::Bool
performance::Float64
bump::Bool
heading::Tuple{Any, Any}
holding::Array{Any, 1}
program::AgentProgram
location::Tuple{Any, Any} # initialized when adding agent to environment
function Explorer()
return new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}());
end
function Explorer(ap::T) where {T <: AgentProgram}
new_agent = new(Bool(true), Float64(0), Bool(false),
rand(RandomDeviceInstance, [(1, 0), (0, 1), (-1, 0), (0, -1)]),
Array{Any, 1}()); # program is undefined
new_agent.program = ap;
return new_agent;
end
end
function isAlive(a::T) where {T <: Agent}
return a.alive;
end
function setAlive(a::T, bv::Bool) where {T <: Agent}
a.alive = bv;
nothing;
end
#=
Obstacle Environment Objects
=#
abstract type Obstacle <: EnvironmentObject end;
mutable struct Wall <: Obstacle
location::Tuple{Any, Any}
function Wall()
return new();
end
end
#=
Vacuum Environment Objects
=#
mutable struct Dirt <: EnvironmentObject
location::Tuple{Any, Any}
function Dirt()
return new();
end
end
#=
Wumpus Environment Objects
=#
mutable struct Gold <: EnvironmentObject
location::Tuple{Any, Any}
function Gold()
return new();
end
end
mutable struct Pit <: EnvironmentObject
location::Tuple{Any, Any}
function Pit()
return new();
end
end
mutable struct Arrow <: EnvironmentObject
location::Tuple{Any, Any}
function Array()
return new();
end
end
#=
Implement execute() methods for implemented AgentPrograms.
=#
loc_A = (0, 0)
loc_B = (1, 0)
"""
execute(ap::TableDrivenAgentProgram, percept::Tuple{Any, Any})
Return an action by using the given table driven agent program 'ap' (Fig. 2.7),
percept 'percept', and table 'ap.table'.
"""
function execute(ap::TableDrivenAgentProgram, percept::Tuple{Any, Any})
push!(ap.percepts, percept);
local action;
if (haskey(ap.table, Tuple((ap.percepts...,))))
action = ap.table[Tuple((ap.percepts...,))] # convert percept sequence to tuple
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(percept), action.name);
end
else
# The table is not complete with all possible percept sequences.
# So, this program should now behave like a ReflexVacuumAgentProgram.
if (percept[1] == loc_A)
action = "Right";
elseif (percept[1] == loc_B)
action = "Left";
end
end
return action;
end
"""
execute(ap::ReflexVacuumAgentProgram, location_status::Tuple{Any, Any})
Return an action by using the given reflex agent program 'ap' (Fig. 2.8) and
percept 'location_status'.
"""
function execute(ap::ReflexVacuumAgentProgram, location_status::Tuple{Any, Any})
local location = location_status[1];
local status = location_status[2];
if (status == "Dirty")
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Suck");
end
return "Suck";
elseif (location == loc_A)
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Right");
end
return "Right";
elseif (location == loc_B)
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Left");
end
return "Left";
end
end
"""
execute(ap::ModelBasedVacuumAgentProgram, location_status::Tuple{Any, Any})
Return an action by using the given model-based agent program 'ap' and
percept 'location_status'.
"""
function execute(ap::ModelBasedVacuumAgentProgram, location_status::Tuple{Any, Any})
local location = location_status[1];
local status = location_status[2];
ap.model[location] = status; # update existing model
if (ap.model[loc_A] == ap.model[loc_B] == "Clean") # return "NoOp" when no work is necessary
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "NoOp");
end
return "NoOp";
elseif (status == "Dirty")
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Suck");
end
return "Suck";
elseif (location == loc_A)
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Right");
end
return "Right";
elseif (location == loc_B)
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(location_status), "Left");
end
return "Left";
end
end
"""
execute(ap::RandomAgentProgram, percept::Tuple{Any, Any})
Return an action by using the given random agent program 'ap' and percept 'percept'.
"""
function execute(ap::RandomAgentProgram, percept::Tuple{Any, Any})
return rand(RandomDeviceInstance, ap.actions);
end
function rule_match(state::String, rules::Array{Rule, 1})
for element in rules
if (state == element.condition)
return element;
end
end
return C_NULL; # the function did not find a matching rule
end
"""
interpret_input(ap::T, percept::Tuple{Any, Any}) where {T <: AgentProgram}
Return a new state for the given agent program 'ap' and percept 'percept'.
"""
function interpret_input(ap::T, percept::Tuple{Any, Any}) where {T <: AgentProgram}
println("interpret_input() is not implemented yet for ", typeof(ap), "!");
nothing;
end
"""
update_state(ap::T, percept::Tuple{Any, Any}) where {T <: AgentProgram}
Return a new state for the given agent program 'ap' and percept 'percept'.
"""
function update_state(ap::T, percept::Tuple{Any, Any}) where {T <: AgentProgram}
println("update_state() is not implemented yet for ", typeof(ap), "!");
nothing;
end
"""
execute(ap::SimpleReflexAgentProgram, percept::Tuple{Any, Any})
Return an action by using the given simple reflex agent program 'ap' (Fig. 2.10),
percept 'percept', and rules 'ap.rules'.
"""
function execute(ap::SimpleReflexAgentProgram, percept::Tuple{Any, Any})
# Generate condition string from given percept
local state = interpret_input(ap, percept);
local rule = rule_match(state, ap.rules);
local action = rule.action;
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(percept), action);
end
return action;
end
"""
execute(ap::ModelBasedReflexAgentProgram, percept::Tuple{Any, Any})
Return the most recent action made by using the given model-based reflex agent
program 'ap' (Fig. 2.12) and percept 'percept'.
"""
function execute(ap::ModelBasedReflexAgentProgram, percept::Tuple{Any, Any})
# Set new state
ap.state = update_state(ap, percept);
local rule = rule_match(ap.state, ap.rules);
ap.action = rule.action;
if (ap.isTracing)
@printf("%s perceives %s and does %s\n", string(typeof(ap)), string(percept), ap.action);
end
return ap.action;
end
#=
Load a implemented AgentProgram into a Agent.
=#
"""
TableDrivenVacuumAgent()
Return an Agent that uses the dictionary representation of a table (Fig. 2.3)
of percept sequences (key) mappings to actions (value).
"""
function TableDrivenVacuumAgent()
local table = Dict{Any, Any}([
Pair(((loc_A, "Clean"),), "Right"),
Pair(((loc_A, "Dirty"),), "Suck"),
Pair(((loc_B, "Clean"),), "Left"),
Pair(((loc_B, "Dirty"),), "Suck"),
Pair(((loc_A, "Clean"), (loc_A, "Clean")), "Right"),
Pair(((loc_A, "Clean"), (loc_A, "Dirty")), "Suck"),
# ...
Pair(((loc_A, "Clean"), (loc_A, "Clean"), (loc_A, "Clean")), "Right"),
Pair(((loc_A, "Clean"), (loc_A, "Clean"), (loc_A, "Dirty")), "Suck"),
# ...
]);
return Agent(TableDrivenAgentProgram(table_dict=table));
end
"""
ReflexVacuumAgent()
Return an agent for the two-state vacuum environment (Fig. 2.8).
"""
function ReflexVacuumAgent()
return Agent(ReflexVacuumAgentProgram());
end
"""
ModelBasedVacuumAgent()
Return an agent that tracks statuses of clean and dirty locations.
"""
function ModelBasedVacuumAgent()
return Agent(ModelBasedVacuumAgentProgram(model=Dict{Any, Any}([
Pair(loc_A, Nothing),
Pair(loc_B, Nothing),
])));
end
"""
RandomVacuumAgent()
Return an agent that randomly chooses one of the possible actions in the
vacuum environment.
"""
function RandomVacuumAgent()
return Agent(RandomAgentProgram(["Right", "Left", "Suck", "NoOp"]));
end
# Declare Environment as a supertype for Environment implementations
abstract type Environment end;
abstract type TwoDimensionalEnvironment <: Environment end;
#=
XYEnvironment is a 2-dimensional Environment implementation with obstacles.
Agents perceive their location as a tuple of objects within perceptible_distance radius.
This environment does not update agent performance measures.
=#
mutable struct XYEnvironment <: TwoDimensionalEnvironment
objects::Array{EnvironmentObject, 1}
agents::Array{Agent, 1} #agents found in this field should also be found in the objects field
width::Float64
height::Float64
perceptible_distance::Float64
function XYEnvironment()
local xy = new(Array{EnvironmentObject, 1}(), Array{Agent, 1}(), Float64(10), Float64(10), Float64(1));
add_walls(xy);
return xy;
end
end
#=
VacuumEnvironment is a 2-dimensional Environment implementation with obstacles.
Agents can perceive their location as "Dirty" or "Clean". Agent performance measures
are updated when Dirt is removed or a non-NoOp action is executed.
=#
mutable struct VacuumEnvironment <: TwoDimensionalEnvironment
objects::Array{EnvironmentObject, 1}
agents::Array{Agent, 1} #agents found in this field should also be found in the objects field
width::Float64
height::Float64
perceptible_distance::Float64
function VacuumEnvironment()
local ve = new(Array{EnvironmentObject, 1}(), Array{Agent, 1}(), Float64(10), Float64(10), Float64(1));
add_walls(ve);
return ve;
end
end
#=
TrivialVacuumEnvironment has 2 possible locations: loc_A and loc_B.
The status of those locations can be either "Dirty" or "Clean".
=#
mutable struct TrivialVacuumEnvironment <: Environment
objects::Array{EnvironmentObject, 1}
agents::Array{Agent, 1}
status::Dict{Tuple{Any, Any}, String}
perceptible_distance::Float64
function TrivialVacuumEnvironment()
local tve = new(
Array{EnvironmentObject, 1}(),
Array{Agent, 1}(),
Dict{Tuple{Any, Any}, String}([
Pair(loc_A, rand(RandomDeviceInstance, ["Clean", "Dirty"])),
Pair(loc_B, rand(RandomDeviceInstance, ["Clean", "Dirty"])),
]), Float64(1));
return tve;
end
end
#=
WumpusEnvironment is a 2-dimensional Environment implementation of the Wumpus World.
=#
mutable struct WumpusEnvironment <: TwoDimensionalEnvironment
objects::Array{EnvironmentObject, 1}
agents::Array{Array, 1}
width::Float64
height::Float64
perceptible_distance::Float64
function WumpusEnvironment()
local we = new(Array{EnvironmentObject, 1}(), Array{Agent, 1}(), Float64(10), Float64(10), Float64(1));
add_walls(we);
return we;
end
end
"""
percept(e, agent, act)
Returns a percept representing what the agent perceives in the enviroment.
"""
function percept(e::T1, a::T2, act::T3) where {T1 <: Environment, T2 <: EnvironmentAgent, T3 <: String} #implement this later
println("percept() is not implemented yet for ", typeof(e), "!");
nothing;
end
function percept(e::VacuumEnvironment, a::Agent)
local status = if_(some_objects_at(a.location, Dirt), "Dirty", "Clean");
local bump = if_(a.bump, "Bump", "None");
return (status, bump)
end
"""
objects_near(e, location)
objects_near(e, location, radius)
Return a list of EnvironmentObjects within the radius of a given location.
"""
function objects_near(e::XYEnvironment, loc::Tuple{Any, Any}; radius::Union{Nothing, Float64}=nothing)
if (typeof(radius) <: Nothing)
radius = e.perceptible_distance;
end
sq_radius = radius * radius;
return [obj for obj in e.objects if (utils.distance2(loc, obj.location) <= sq_radius)];
end
function percept(e::XYEnvironment, a::Agent)
# this percept might not consist of exactly 2 elements
return Tuple(([string(typeof(obj)) for obj in objects_near(a.location)]...,));
end
function percept(e::TrivialVacuumEnvironment, a::Agent)
return (a.location, e.status[a.location]);
end
function get_objects_at(e::T, loc::Tuple{Any, Any}, objType::DataType) where {T <: Environment}
if (objType <: EnvironmentObject)
return [obj for obj in e.objects if (typeof(obj) <: objType && obj.location == loc)];
else
error(@sprintf("InvalidEnvironmentObjectError: %s is not a subtype of EnvironmentObject!", string(typeof(objType))));
end
end
function some_objects_at(e::T, loc::Tuple{Any, Any}, objType::DataType) where {T <: Environment}
object_array = get_objects_at(e, loc, objType);
if (length(object_array) == 0)
return false;
else
return true;
end
end
function is_done(e::T) where {T <: Environment}
for a in e.agents
if (a.alive)
return false;
end
end
return true;
end
function step(e::T) where {T <: Environment}
if (!is_done(e))
local actions = [execute(agent.program, percept(e, agent)) for agent in e.agents];
for t in zip(e.agents, actions)
local agent = t[1];
local action = t[2];
execute_action(e, agent, action);
end
exogenous_change(e);
end
end
function run(e::T; steps::Int64=1000) where {T <: Environment}
for i in range(0, stop=(steps - 1))
if (is_done(e))
break;
end
step(e);
end
end
function exogenous_change(e::T) where {T <: Environment} # implement this later
# comment the following line to reduce verbosity
#println("exogenous_change() not yet implemented for ", typeof(e), "!");
nothing;
end
function default_location(e::T1, obj::T2) where {T1 <: Environment, T2 <: EnvironmentObject} #implement this later
return false;
end
function default_location(e::TrivialVacuumEnvironment, obj::T) where {T <: EnvironmentObject}
return rand(RandomDeviceInstance, [loc_A, loc_B]);
end
function default_location(e::XYEnvironment, obj::T) where {T <: EnvironmentObject}
return (rand(RandomDeviceInstance, range(0, stop=(e.width - 1))), rand(RandomDeviceInstance, range(0, stop=(e.height - 1))));
end
function environment_objects(e::T) where {T <: Environment}
return [];
end
function environment_objects(e::VacuumEnvironment)
# ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, and ModelBasedVacuumAgent
# are functions that generate new agents with their respective AgentPrograms
return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, ModelBasedVacuumAgent];
end
function environment_objects(e::TrivialVacuumEnvironment)
# ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, and ModelBasedVacuumAgent
# are functions that generate new agents with their respective AgentPrograms
return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, ModelBasedVacuumAgent];
end
function environment_objects(e::WumpusEnvironment)
return [Wall, Gold, Pit, Arrow, Wumpus, Explorer];
end
function execute_action(e::T1, a::T2, act::String) where {T1 <: Environment, T2 <: EnvironmentAgent} #implement this later
println("execute_action() is not implemented yet for ", string(typeof(e)), "!");
nothing;
end
function execute_action(e::XYEnvironment, a::EnvironmentAgent, act::String)
a.bump = false;
if (act == "TurnRight")
a.heading = utils.turn_heading(a.heading, -1);
elseif (act == "TurnLeft")
a.heading = utils.turn_heading(a.heading, 1);
elseif (act == "Foward")
move_to(e, a, utils.vector_add_tuples(a.heading, a.location));
elseif (act == "Release")
if (length(a.holding) > 0)
pop!(a.holding);
end
end
nothing;
end
function execute_action(e::VacuumEnvironment, a::EnvironmentAgent, act::String)
if (act == "Suck")
local dirt_array = get_objects_at(e, a.location, Dirt);
if (length(dirt_array) > 0)
local dirt = pop!(dirt);
delete_object(e, dirt);
a.performance = a.performance + 100;
end
else
a.bump = false;
if (act == "TurnRight")
a.heading = utils.turn_heading(a.heading, -1);
elseif (act == "TurnLeft")
a.heading = utils.turn_heading(a.heading, 1);
elseif (act == "Foward")
move_to(e, a, utils.vector_add_tuples(a.heading, a.location));
elseif (act == "Release")
if (length(a.holding) > 0)
pop!(a.holding);
end
end
end
if (act != "NoOp")
a.performance = a.performance - 1;
end
nothing;
end
function execute_action(e::TrivialVacuumEnvironment, a::EnvironmentAgent, act::String)
if (act == "Right")
a.location = loc_B;
a.performance = a.performance - 1;
elseif (act == "Left")
a.location = loc_A;
a.performance = a.performance - 1;
elseif (act == "Suck")
if (e.status[a.location] == "Dirty")
a.performance = a.performance + 10;
end
e.status[a.location] = "Clean";
end
nothing;
end
function add_object(e::T1, obj::T2; location::Union{Nothing, Tuple{Any, Any}}=nothing) where {T1 <: Environment, T2 <: EnvironmentObject}
if (!(obj in e.objects))
if (!(typeof(location) <: Nothing))
obj.location = location;
else
obj.location = default_location(e, obj);
end
push!(e.objects, obj);
if (typeof(obj) <: EnvironmentAgent)
obj.performance = Float64(0);
push!(e.agents, obj);
end
else
println("add_object(): object already exists in environment!");
end
nothing;
end
function delete_object(e::T1, obj::T2) where {T1 <: Environment, T2 <: EnvironmentObject}
local i = utils.index(e.objects, obj);
if (i > -1)
deleteat!(e.objects, i);
end
i = utils.index(e.agents, obj);
if (i > -1)
deleteat!(e.agents, i);
end
nothing;
end
function add_walls(e::T) where {T <: TwoDimensionalEnvironment}
for x in range(0, stop=(e.width - 1))
add_object(Wall(), location=(x, 0));
add_object(Wall(), location=(x, e.height - 1));
end
for y in range(0, stop=(e.height - 1))
add_object(Wall(), location=(0, y));
add_object(Wall(), location=(e.width - 1, 0));
end
nothing;
end
"""
move_to(e, obj, dest)
Move the EnvironmentObject to the destination given.
"""
function move_to(e::T, obj::EnvironmentObject, destination::Tuple{Any, Any}) where {T <: TwoDimensionalEnvironment}
obj.bump = some_objects_at(e, destination, Wall); # Wall is a subtype of Obstacle, not an alias
if (!obj.bump)
obj.location = destination;
end
nothing;
end
function run_once(e::Environment, AgentGen::Function, step_count::Int)
local agent = AgentGen();
add_object(e, agent);
run(e, steps=step_count);
return agent.performance;
end
"""
test_agent(agentgeneratorfunction, steps, envs)
Calculates the average of the scores of running the given Agent in each of the environments.
"""
function test_agent(AgentGenerator::Function, steps::Int, envs::Array{T, 1}) where {T <: Environment}
return mean([run_once(envs[i], AgentGenerator, steps) for i in 1:length(envs)]);
end
"""
compare_agents()
Creates an array of 'n' Environments and runs each Agent in each separate copy of the Environment
array for 'steps' times. Then, return a list of (agent, average_score) tuples.
"""
function compare_agents(EnvironmentGenerator::DataType, AgentGenerators::Array{Function, 1}; n::Int64=10, steps::Int64=1000)
local envs = [EnvironmentGenerator() for i in range(0, stop=(n - 1))];
return [(string(typeof(A)), test_agent(A, steps, deepcopy(envs))) for A in AgentGenerators];
end