-
Notifications
You must be signed in to change notification settings - Fork 6
/
agente004.pl
157 lines (149 loc) · 7.56 KB
/
agente004.pl
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hunt The Wumpus - World Simulator %
% Copyright (C) 2012 - 2022 Ruben Carlo Benante <rcb at beco dot cc> %
% %
% This program is free software; you can redistribute it and/or modify %
% it under the terms of the GNU General Public License as published by %
% the Free Software Foundation; version 2 of the License. %
% %
% This program is distributed in the hope that it will be useful, %
% but WITHOUT ANY WARRANTY; without even the implied warranty of %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %
% GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License along %
% with this program; if not, write to the Free Software Foundation, Inc., %
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Special thanks to:
% - Gregory Yob
% - Larry Holder
% - Walter Nauber
%
% A Prolog implementation of the Wumpus world invented by Gregory Yob (1972)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To allow an agent to run with the Wumpus Simulator you need to define:
% init_agent :
% It will be called only once, at the start. Put here definitions and
% other start code you need (asserts, retracts, and so on)
% run_agent :
% It will be called each turn by the simulator.
% Input: perceptions from the world.
% Expected output: an action for the agent to perform.
% world_setup([Size, Type, Move, Gold, Pit, Bat, [RandS, RandA]]):
% This is a fact. It will be consulted only once at the beginning,
% even before init_agent. It will configure the world as you say,
% or use a default in case of conflicts or mistakes.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lista de Percepcao: [Stench, Breeze, Glitter, Bump, Scream, Rustle]
% Traducao: [Fedor, Vento, Brilho, Trombada, Grito, Ruido]
% Acoes possiveis (abreviacoes):
% goforward (go) - andar
% turnright (turn, turnr ou tr) - girar sentido horario
% turnleft (turnl ou tl) - girar sentido anti-horario
% grab (gr) - pegar o ouro
% climb (cl) - sair da caverna
% shoot (sh) - atirar a flecha
% sit (si) - sentar (nao faz nada, passa a vez)
%
% Costs (Custos):
% Actions: -1 (Andar/Girar/Pegar/Sair/Atirar/Sentar)
% Die: -1000 (morrer no buraco, wumpus ou de fadiga)
% Killing the Wumpus: +1000 (matar Wumpus)
% Climbing alive with golds: +500 for each gold (sair com ouro)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To run the example, start PROLOG with (rode o exemplo iniciando o prolog com):
% swipl -s agenteXXX.pl
% then do the query (faca a consulta):
% ?- start.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% world_setup([Size, Type, Move, Gold, Pit, Bat, Adv])
%
% Size and Type: - fig62, 4
% - grid, [2-9] (default 4)
% - dodeca, 20
% +--------+-----------+
% | Type | Size |
% +--------+-----------+
% | fig62 | 4 (fixed) |
% | grid | 2 ... 9 |
% | dodeca | 20 (fixed)|
% +--------+-----------+
%
% Configuration:
% 1. Size: 0,2..9,20, where: grid is [2-9] or 0 for random, dodeca is 20, fig62 is 4.
% 2. Type: fig62, grid or dodeca
% 3. Move: stander, walker, runner (wumpus movement)
% 4. Gold: Integer is deterministic number, float from 0.0<G<1.0 is probabilistic
% 5. Pits: Idem, 0 is no pits.
% 6. Bats: Idem, 0 is no bats.
% 7. Adv: a list with advanced configuration in the form [RandS, RandA]:
% - RandS - yes or no, random agent start position
% - RandA - yes or no, random agent start angle of orientation
%
% examples:
% * default:
% world_setup([4, grid, stander, 0.1, 0.2, 0.1, [no, no]]).
% * size 5, 1 gold, 3 pits, some bats prob. 0.1, agent randomly positioned
% world_setup([5, grid, stander, 1, 3, 0.1, [yes]]).
%
% Types of Wumpus Movement
% walker : original: moves when it hears a shoot, or you enter its cave
% runner : go forward and turn left or right on bumps, maybe on pits
% wanderer : arbitrarily choses an action from [sit,turnleft,turnright,goforward]
% spinner : goforward, turnright, repeat.
% hoarder : go to one of the golds and sit
% spelunker : go to a pit and sit
% stander : do not move (default)
% trapper : goes hunting agent as soon as it leaves [1,1]; goes home otherwise
% bulldozer : hunt the agent as soon as it smells him
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% agente004.pl:
%
% Strategy: try to solve the wumpus world without memory
% Performance: it does not go very well as you can imagine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- use_module(wumpus, [start/0]). % agente usa modulo simulador
% Mundo:
% 4x4, quadrado, wumpus anda quando atira ou quando entra na casa dele
% probabilidade de ouro 0.1, de buracos 0.2, um unico morcego
% agente inicia em casa aleatoria
% Maximo de acoes antes de morrer de fome: Size^2x4 = 4x4x4 = 64
world_setup([4, fig62, walker, 0.1, 0.2, 1, [yes]]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Inicie aqui seu programa.
init_agent.
% esta e a funcao chamada pelo simulador. Nao altere a "cabeca" da funcao. Apenas o corpo.
% Funcao recebe Percepcao, uma lista conforme descrito acima.
% Deve retornar uma Acao, dentre as acoes validas descritas acima.
run_agent(Percepcao, Acao) :-
write('percebi: '),
writeln(Percepcao),
acao(Percepcao, Acao).
% Lista de Percepcao: [Fedor,Vento,Brilho,Trombada,Grito]
acao([_,_,yes|_], grab) :- %pega o ouro se sentir o brilho
writeln('acao: pega').
acao([_,_,_,yes|_], turnright) :- %vira para a direita sempre que bater na parede
writeln('acao: direita').
acao([yes,_,_,_,no|_], A):- %atira, andar, girar ou sair em caso de fedor sem grito
random_member(A, [shoot, shoot, shoot, shoot, turnright, turnright, turnright, climb, climb, goforward]),
write('acao aleatoria com fedor, sem grito: '),
writeln(A).
acao([_,no,_,_,yes|_], goforward) :- %anda se escutar grito e sem brisa
writeln('acao com brisa, sem grito: frente').
acao([_,yes,_,_,yes|_], A):- %grito com brisa
random_member(A, [climb, turnright]),
write('acao aleatoria com brisa e grito: '),
writeln(A).
acao([_,yes|_], A):- %brisa
random_member(A, [climb, climb, climb, turnright, turnright, goforward]),
write('acao aleatoria com brisa: '),
writeln(A).
acao(_, A) :- % sorteia default
random_member(A, [turnright, goforward, climb]),
write('acao aleatoria outros casos: '),
writeln(A).
/* ----------------------------------------------------------------------- */
/* vi: set ai et ts=4 sw=4 tw=0 wm=0 fo=croql : PL config for Vim modeline */
/* Template by Dr. Beco <rcb at beco dot cc> Version 20150620.224740 */