1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    2% FILE: Wumpus/main_ecl.pl
    3%
    4%  AUTHOR    : Stavros Vassos & Sebastian Sardina
    5%  email     : {stavros,ssardina}@cs.toronto.edu
    6%  WWW       : www.cs.toronto.edu/cogrobo
    7%  TESTED    : ECLIPSE PROLOG
    8%  TYPE CODE : system dependent predicates
    9%
   10% DESCRIPTION: This file is the main file of an IndiGolog application
   11%              of the Wumpus World.
   12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   13%                             June 15, 2000
   14%
   15% This software was developed by the Cognitive Robotics Group under the
   16% direction of Hector Levesque and Ray Reiter.
   17%
   18%        Do not distribute without permission.
   19%        Include this notice in any copy made.
   20%
   21%
   22%         Copyright (c) 2000 by The University of Toronto,
   23%                        Toronto, Ontario, Canada.
   24%
   25%                          All Rights Reserved
   26%
   27% Permission to use, copy, and modify, this software and its
   28% documentation for non-commercial research purpose is hereby granted
   29% without fee, provided that the above copyright notice appears in all
   30% copies and that both the copyright notice and this permission notice
   31% appear in supporting documentation, and that the name of The University
   32% of Toronto not be used in advertising or publicity pertaining to
   33% distribution of the software without specific, written prior
   34% permission.  The University of Toronto makes no representations about
   35% the suitability of this software for any purpose.  It is provided "as
   36% is" without express or implied warranty.
   37% 
   38% THE UNIVERSITY OF TORONTO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
   39% SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
   40% FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TORONTO BE LIABLE FOR ANY
   41% SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
   42% RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
   43% CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   44% CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   45% 
   46%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   47% This is the top-level file for a Legolog application program.
   48% It consults all the necessary Legolog prolog files.
   49% In particular, the following is loaded:
   50%
   51%  (1) Load all libraries required. This includes the system dependant
   52%      ones for the specific Prolog plus general libraries
   53%  (2) Load the IndiGolog interpreter and the projector used
   54%  (3) Load the application code itself containing the background theory
   55%      of action plus the high-level program
   56%  (4) Specify which environments should be loaded and how 
   57%  (5) Specify how each action should be executed and how to translate
   58%      exogenous actions
   59%
   60% Moreover, the following is provided:
   61%
   62% -- main: Collects all the procedures named 'mainControl(N)' where
   63%          N is the number representing the N-th controller.
   64%          The user can select which controller to execute and the 
   65%          IndiGolog executor will be run on such controller
   66%
   67%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   69
   70
   71%%%%%%%%%%%%%%
   72% SET GLOBAL PARAMETERS AND GLOBAL VARIABLES/CONSTANTS USED
   73%  
   74%  These may be options to improve performance and variables/constants used
   75%  around the whole arquitecture
   76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   77
   78/* ECL
   79:- set_flag(debug_compile, off).   
   80:- set_flag(variable_names, off).  % To speed up execution
   81type_prolog(ecl).    % Type of Prolog being used
   82*/
   83
   84:- ensure_loaded('lib/logicmoo_workarounds').   85
   86
   87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   88% (1) LOAD/COMPILE/IMPORT LIBRARIES, MODULES, ETC that may be required.
   89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   90/* ECL
   91:- lib(scattered).   % Clauses can be not consecutive
   92:- lib(fd).          % Load finite-domain constraint library
   93:- lib(fd_search).   % Load extra finite-domain search algorithms
   94%:- lib(lists).	     % Used for shuffle/2
   95
   96:- use_module(library(tools_ecl)).       % General tools for Eclipse
   97*/
   98
   99:- ensure_loaded('lib/systemvar').              % Common facts (device_manager/4)
  100
  101
  102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  103% (2,3) CONSULT NECESSARY FILES
  104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  105
  106% 1 - Consult the IndiGolog system: top-level and evaluator
  107:- ensure_loaded('Interpreters/indigolog').  108
  109:- ensure_loaded('Interpreters/flux/flux').  110
  111% 2 - Consult environment manager 
  112:- ensure_loaded(['Env/env_man.pl']).         % Load environment manager
  113
  114% 3 - Consult projector
  115
  116% 4 - Consult application
  117:- ensure_loaded(flux_wumpus).                          % Application code in Flux
  118
  119
  120
  121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122% (4,5) ENVIRONMENTS TO LOAD
  123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  124:- dynamic wumpus_config/5.  125
  126% Port of environment manager has to be fixed in SWI
  127server_port(_).
  128server_host('localhost').
  129
  130wumpus_config(flux(default),8,10,1,random).  % Default conf: 
  131
  132% 8x8 size, 10/100 prob of pit and 4 golds in grid
  133% This requires a file wumpustestbed.pl with all the conf info
  134%wumpus_config(rerun(82),8,10,1,nmar05test(9)).  
  135
  136% Load simulator, RCX and internet environments
  137:- ensure_loaded('Env/dev_managers').              % Common facts (device_manager/4)
  138load_device(Env, Command, Address) :- 
  139%       member((Env,Type), [(virtual_wumpus_silent, swi)]),
  140       member((Env,Type), [(virtual_wumpus, swi)]),
  141        (var(Address) -> 
  142             Host=null, Port=null ; 
  143             Address = [Host, Port]
  144        ),
  145        device_manager(Env, Type, Command, [Host, Port]).
  146
  147           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  148           % HOW TO EXECUTE ACTIONS: Environment + low-level Code %
  149           %        how_to_execute(Action, Environment, Code)     %
  150           %        						  %
  151	   % Anything else is executed in the simulator           %
  152           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  153
  154%how_to_execute(Action, virtual_wumpus_silent, Action).
  155how_to_execute(Action, virtual_wumpus, Action).
  156
  157           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  158           %   EXOGENOUS ACTION AND SENSING OUTCOME TRANSLATION   %
  159           %          translateExogAction(Code, Action)           %
  160           %          translateSensing(Action, Outcome, Value)    %
  161           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  162translateExogAction(Action, Action).
  163
  164
  165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  166% MAIN PREDICATE - evaluate this to run demo
  167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  168main:- 	init, 
  169	writeln('Starting to execute WUMPUS with FLUX'),
  170	(main_wumpus -> true ; true), 
  171	writeln('Finishing to execute WUMPUS with FLUX'),
  172	fin.
  173initializeDB.
  174finalizeDB.
  175
  176
  177full_test :-
  178	member(Size,[8]),
  179	member(PPits,[20,30,40]),
  180	member(NoGolds,[1,2,4]),
  181	writeln('=================================================='),
  182	write('TEST WUMPUS: '), write((Size,PPits,NoGolds)), nl,
  183	once(retract(wumpus_config(_,_,_,_,_))),
  184	once(retract(gridsize(_))),
  185	assert(gridsize(Size)),
  186		% Set up Size, PPits and NoGolds (only)
  187	assert(wumpus_config(test,Size,PPits,NoGolds,scenario)),
  188	test(301,1),
  189	fail.
  190full_test :- 
  191	writeln('=================================================='),
  192	writeln('DONE!').
  193
  194
  195% test Wumpus Max number of times repetitively
  196test(Max,Max) :-!, 
  197	writeln('FINISHED TESTING....').
  198test(Max,N) :-
  199	write('TESTING INSTANCE: '), write(N),
  200	write(' (Out of '), write(Max), write(' runs)'), nl,
  201	tell('/dev/null'),
  202	retract(wumpus_config(_,Size,PPits,NoGolds,_)),
  203		% Assert the type of execution it is going to be done next!
  204	assert(wumpus_config(nmar05testFlux(N),Size,PPits,NoGolds,nmar05test(N))),
  205%	assert(wumpus_config(fluxtest(N),Size,PPits,NoGolds,random)),
  206	main,	% RUN FLUX PROGRAM!
  207	sleep(1),
  208	tell(user),
  209	N2 is N+1,
  210	test(Max, N2).	
  211	
  212
  213
  214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  215% PREDICATES WITH SYSTEM DEPENDENT CODE
  216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  217
  218:- set_option(debug_level,2).  219:- set_option(wait_step,0).  220:- set_option(type_em,signal).  221%:- set_option(type_em,eventafter).
  222
  223
  224
  225
  226
  227
  228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  229% EXECUTION OF ACTIONS
  230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  231
  232%:- dynamic now/1.
  233perform(A, S) :-
  234	now(H),
  235	perform(A,H,CS),	% CS is a list of sensing codes (0/1)
  236	maplist(translateSensing,CS,S),
  237	update_now([o(A,S)|H]).
  238
  239perform(sense, H, [S2,S1,S3]) :-
  240	execute_action(smell, H, S1), S1\=failed,
  241	execute_action(senseBreeze, H, S2), S2\=failed,
  242	execute_action(senseGold, H, S3), S3\=failed.
  243perform(turn, H, []) :-
  244	execute_action(turn, H, S), S\=failed.
  245perform(enter, H, S2) :-
  246	execute_action(enter, H, S), S\=failed,
  247	perform(sense, H, S2).
  248perform(exit, H, []) :-
  249	execute_action(climb, H, S), S\=failed.
  250perform(shoot, H, [S]) :-
  251	execute_action(shoot, H, S), S\=failed.
  252perform(go, H, S2) :-
  253	execute_action(moveFwd, H, S), S\=failed,
  254	perform(sense, H, S2).
  255perform(grab, H, []) :-
  256	execute_action(pickGold, H, S), S\=failed.
  257
  258translateSensing(0, false).
  259translateSensing(1, true).
  260
  261% interface to the execute_action/5 in the EM (env_man.pl)
  262execute_action(Action, H, Outcome) :-
  263	execute_action(Action, H, _, _, Outcome).
  264
  265
  266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  267% EOF:  Wumpus/main_ecl.pl
  268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%