Creating final code

You can see in the folder perkun/initial_code the initial specifications for Dorban (dorban_general.perkun), Pregor (pregor_general.perkun) and Thragos (thragos_general.perkun). The initial code contains only values and variables. The payoff function and the model are empty! For example perkun/initial_code/dorban_general.perkun is:


values
{
	value false, true; # logical values
	value place_Wyzima,place_Shadizar,place_Novigrad; # places
	value do_nothing,escape,fight,goto_Wyzima,goto_Shadizar,goto_Novigrad; # actions
}

variables
{
	input variable where_is_Dorban:{place_Wyzima,place_Shadizar,place_Novigrad}; # where am I
	input variable do_I_see_vampire:{false, true};
	output variable action:{do_nothing,goto_Wyzima,goto_Shadizar,goto_Novigrad}; # actions
	hidden variable where_is_vampire:{place_Wyzima,place_Shadizar,place_Novigrad};
}


payoff
{
}

model
{
}

cout << prolog generator << eol;

We have two input variables, one output variable and one hidden variable here. This file is used by Perkun to create a Prolog generator. The Prolog generator for Dorban (see the folder perkun/prolog) is initially dorban_general.prolog, but later is enhanced with certain rules (by the Perl script perkun/perl/inject_dorban_general_rules.pl) and the final version for Dorban is perkun/prolog/dorban_general_final.prolog. This Prolog code can be used to produce perkun/final_code/dorban_general.perkun! See the perkun/Makefile.am to trace the dependencies. You will need SWI Prolog to run these Prolog codes.

So initially we have Perkun code with just values and variables, then we produce a Prolog generator, enhance it with certain rules and execute, to produce the final Perkun code. Take a look at the file perkun/perl/inject_dorban_general_rules.pl. The script is looking in your Prolog generator for the comments like:
% PLEASE INSERT YOUR CODE HERE-get_probability
and replaces them with some rules. The same is done for
% PLEASE INSERT YOUR CODE HERE-write_model_impossible_if_necessary
% PLEASE INSERT YOUR CODE HERE-write_model_action_illegal_if_necessary
and
% PLEASE INSERT YOUR CODE HERE-get_payoff.

get_payoff

Take a look at perkun/prolog/dorban_general.prolog. Just before the comment
% PLEASE INSERT YOUR CODE HERE-get_payoff
you will find a comment:
% get_payoff(INPUT_where_is_Dorban,INPUT_do_I_see_vampire, PAYOFF)
This is intended to help you to formulate the get_payoff rules. The script inject_dorban_general_rules.pl replaces the comment with the fact "get_payoff(_,true, 100.0)":


s/% PLEASE INSERT YOUR CODE HERE-get_payoff/

get_payoff(_,true, 100.0). % Dorban is hunting the vampires
/g;

This means that no matter where Dorban is whenever INPUT_do_I_see_vampire is true the PAYOFF equals 100.0. The final code for Dorban contains the payoff generated by these rules:


payoff
{
set({where_is_Dorban=>place_Wyzima, do_I_see_vampire=>false}, 0.0);
set({where_is_Dorban=>place_Wyzima, do_I_see_vampire=>true}, 100.0);
set({where_is_Dorban=>place_Shadizar, do_I_see_vampire=>false}, 0.0);
set({where_is_Dorban=>place_Shadizar, do_I_see_vampire=>true}, 100.0);
set({where_is_Dorban=>place_Novigrad, do_I_see_vampire=>false}, 0.0);
set({where_is_Dorban=>place_Novigrad, do_I_see_vampire=>true}, 100.0);
}

get_probability

In the file perkun/prolog/dorban_general.prolog just before the comment
% PLEASE INSERT YOUR CODE HERE-get_probability
there is the comment
% get_probability(INITIAL_where_is_Dorban, INITIAL_do_I_see_vampire, INITIAL_where_is_vampire, ACTION_action, TERMINAL_where_is_Dorban, TERMINAL_do_I_see_vampire, TERMINAL_where_is_vampire, PROBABILITY).
The placeholders here are:

  • INITIAL_where_is_Dorban
  • INITIAL_do_I_see_vampire
  • INITIAL_where_is_vampire
  • ACTION_action
  • TERMINAL_where_is_Dorban
  • TERMINAL_do_I_see_vampire
  • TERMINAL_where_is_vampire
  • PROBABILITY

Take a look at one of the rules we inserted here:


get_probability(INITIAL_where_is_Dorban, _, INITIAL_where_is_vampire, goto_Wyzima, place_Wyzima, TERMINAL_do_I_see_vampire, INITIAL_where_is_vampire, 1.0):-
	not(INITIAL_where_is_Dorban = place_Wyzima),
	see_variable_consistent_with_place(TERMINAL_do_I_see_vampire,INITIAL_where_is_vampire,place_Wyzima),
	write('# Dorban is going to Wyzima'), nl.

It means that if Dorban is not in Wyzima (initially) and tries to get there (goto_Wyzima) then with 100% probability he will get there and will see the vampire or not, depending on where the vampire currently is. Note that we use INITIAL_where_is_vampire also to match TERMINAL_where_is_vampire, because the action goto_Wyzima does not change the location where the vampire is. The predicate see_variable_consistent_with_place is an auxilliary predicate added to check whether the vampire is in Wyzima or not.