ABS Assignment Lab 1 Rasmus Dall - r.dall@sms.ed.ac.uk January 24th 2013 Introduction In this lab you will be introduced to AgentSpeak and the Jason programming language through the InfoSpeak environment. You will explore the environment and learn how to define agents in Jason. The environment you will work with looks similar to the George Square campus area - however it is not a faithful representation. Please note that the environment is an experimental prototype and as such you may come across imperfections and bugs - please report any serious issues to me, I will attempt to fix them quickly if they are gamebreak- ing. Before the lab you should all have read the provided chapter in the book “Programming Multi-Agent Systems in AgentSpeak using Jason” - in the lab this will be assumed and so I will not spend much time on the basics of Jason but rather more time on familiarising you with the environment. Setting up the environment Jason is an interpreter for AgentSpeak programs, and a framework to create environments for the development and testing of multi-agent systems. You will not need to be familiar with the full power of Jason. In your assignments the environment is provided and you need only create simple plan-rules that govern the agent’s behaviour. The website for Jason is http://jason.sourceforge.net and it is the best place to start for finding documentation and example code (also see the link provided below). In particular the documentation section of the site contains links to the manual and a “getting started” tutorial, both of which are useful reference documents. An IDE is provided for the easy editing of AgentSpeak programs, running the environment and debugging. To set it up simply follow the instructions below, taken from http://jason.sourceforge.net/mini-tutorial/getting-started/ (which has more info, and pictures). 1. Download Jason from http://jason.sourceforge.net 2. Extract the archive to a folder in your userspace. Something like tar xzf Jason-1.3.6a.tgz should do the trick. 1 3. Execute Jason by running ./jason.sh from the bin directory. 4. You will probably need to setup the location of your Java JDK. Go to the menu Plugins → Plugins Options→Jason. On a DICE machine, ”Java Home” should be set to “/usr/”. Running a project Let us test if the environment works by first running an example program. • Open → jason-dir/examples/cleaning-robots/mars.mas2j All Jason projects will be started using the .mas2j file, this files defines the environment and agents in it. In this case the MarsEnv and the agents r1 and r2. Each of these will have seperate definition files (MarsEnv.java and r1.asl, r2.asl). • Run the program by pressing the “Run MAS” button in the lower right hand side. A window will pop-up and a simulation of the mars robots you should have encountered in your lectures will run. If it runs your system should be correctly set up. If not let me know so we can fix the problem. The InfoSpeak world The InfoSpeak environment which you will be working with is an example of a grid world agents can operate in. The grid depicts a (putative) map of the university campus where white squares are in-accessible, and agents (coloured ovals) move around the shaded squares. Squares labelled OUT are used to denote leaving the campus, LT buildings are lecture theatre locations, FOR denotes the Informatics Forum, LIB the University Library, LAB a laboratory location, BAR a pub students like to go to, and GYM a gym. The green block of squares in the middle depicts a park. Agents are capable of moving across squares, and they can perceive their locations and find routes to locations. The “Select Agent” menu will display an agent’s position. A right mouse click on a building will block it for access by agents (red color means “blocked”), this can be undone by right-clicking on it again. To set up the environment: 1. Download the code from the course website and decompress it, e.g. with tar xzf abs1.tgz. 2. Start Jason, and load up infoSpeak.mas2j. There is also a variety of other files for the assignment - of these you will be expected to use the infospeak.config file and to define the two agents focusedAgent.asl and chaoticAgent.asl. The provided agent lecturer.asl may be useful to study. • Try running the environment as setup with one lecturer agent. 2 The .config file The simulation environment expects a configuration file infospeak.config which is used to read in agent types, agents, agent groups (which can be defined to share certain elements), buildings, assignments, lecture details, and custom events on startup. Note that the number of agents specified in infospeak.mas2j needs to be identical to those agents specified in the configuration file. The syn- tax of the configuration file is self-explanatory and you will be expected to utilise this in your assignment. Not all the elements of the system that can be configured are necessarily used in the assignments. A Brief Overview of an AgentSpeak Agent This is a very brief overview, for further information you are expected to read the provided chapter in the book. The main components of an agent are beliefs, intentions and plans. Beliefs either come from percepts, and are updated auto- matically each reasoning cycle, or are added from plans. They are predicates, which may be of arity 0. An example of a position belief might be pos(2, 3). Plans are of the following form: triggering-event : context <- plan_step; plan_step. Triggering-event - Defines which events may initiate a plan. It could be internal, from plans, or external - from percepts. It can be addition, denoted by + or removal denoted by -. It may be either a goal or a belief. So for example +!start would trigger on the addition of a start achievement goal and +winter(isComing) would trigger on the addition of a winter(isComing) belief. Context - The context in which a plan applies. A conjunction or disjunction of beliefs, logical formulae and certain built-in special predicates, such as .random(N). An example of a context depending on the position would be pos(X,Y) & (X==10). Note that variables must begin with an uppercase letter, lowercase or numbers are constants. ”_” unifies with anything, and is the ”don’t care” symbol. Plan_step - There may be 0 or more steps, separated by ; and terminated with a full-stop. These can be actions, of the form do(Action), goals to achieve (of the form !goal(_)) or the addition or deletion of beliefs (of the form +winter(isComing) or -winter(isComing)). Goals take the form !goal(_) and like beliefs may have an arity of 0. Percepts Percepts provided by the world are as follows and are automatically added to the agents belief base: 3 pos(Object,X,Y) - The object Object (a building or an agent) is located at square (X, Y). home(X,Y) - The agent’s home location (one of the OUT squares) day(D) - The current day (an integer counting days since the start of the simulation) time(T) - The time of the day (a number between 9 (a.m.) and 24 (12 a.m.) denoting the hour of the day, “nighttime” is simulated as a single time step so the time jumps from midnight to 9 a.m.) week(W) - The current week of the term (an integer) lecture(Coursename,Week,Day,Time,Place,(Priority)) - Denotes that course Coursename takes place every Weekday at time Time with a certain Priority (this specifies which lecture would be preferred in case of clashes and is optional) assignment(Course,N,SWeek,SDay,STime,EWeek,EDay,ETime,Hours) - Denotes that an assignment that requires Hours hours of work runs from STime on SDay in week SWeek until ETime on EDay in week EWeek event(T,SWeek,SDay,STime,EWeek,EDay,ETime,Location,(Priority)) - Custom event of type T (e.g. party, job, lecture, interview) with start and end times/days/weeks, occurring at Location with a certain Priority (the last argument is optional) Actions Besides the standard actions (which are of the form .action(_) - remember the dot) this particular environment has the following additional actions: go_to(X,Y) - Move toward square (X,Y) one_hour(Time) - Spend one hour while remaining in the current location add_goal(Text) - Set the goal of the agent to be displayed to Text in the GUI (does not affect actual goals pursued, only for visual display) For the second assignment a number of additional actions are available: revise(Time) - Revise for one hour in the current location attend_lecture(Time) - Attend a lecture attend_event(Time) - Attend a custom event defined in the config file for 1 hour attend_meeting(Time) - Attend a meeting between agents for 1 hour 4 work_on_ass(Time) - Work for 1 hour on an assignment submit(Time) - Submit an assignment (takes 1 hour) Note that the additional actions for the 2nd assignment are not particularly “in- telligent” they do not e.g. check if the agent has actually finished an assignment when submitting. These are for scoring purposes only and may be used inap- propriately in the 2nd assignment - make sure you only call these at appropriate times in your solution. I.e. you will not receive marks for agents which use the submit action when it has not finished an assignment. Unification One important difference between Jason and other Prolog-like languages (i.e. in logic programming) from conventional programming languages such as Java or C is the concept of unification. In Jason when we write e.g. pos(Object,X,Y) then Object, X and Y are all variables which needs to be instantiated or unified. Where in e.g. Java when calling a method like pos(Object,X,Y) we must provide the method with the values for Object, X and Y - we can in fact in Jason use this to retrieve the various variables. So in the infospeak environment when we write time(Time) then ’Time’ will unify with whatever value the belief time(_) has in the agents belief base such that if the agent has the belief time(9) then the value of ’Time’ will be 9. Should the agent have no such belief the variable will remain uninstantiated and have no value. Consider this plan from the lecturer: +!givelectures(Me) : week(WeekNow) & day(Day) & time(TimeNow) & pos(Place, X, Y) & lecture(Name, Week, Day, Time, Place, Priority) & Time == TimeNow & (Week == 0|Week == WeekNow) <- add_goal(“Going to the next lecture”); !goto(Me,X,Y); .concat(“Attending “, Name, ” lecture”, G); add_goal(G); one_hour(Time); !checkevents(Me). The only variable which is unified from the beginning is Me which is unified with whatever value Me in !givelectures(Me) takes when it is added as a goal. For the rest these are unified along the way, week(_), day(_) and time(_) each unifies with the value of the current week/day/time which can then be used later. pos(Place,X,Y) unifies Place, X and Y to the current position of the agent. Let us say that in this case Day == Monday, once we get to the lec- ture(Name,Week,Day,Time,Place,Priority) this will retrieve any beliefs in which the Day of the lecture is Monday and will then unify the rest of the variables according to the lecture found; so if the student had a lecture “abs” on Monday in week 7 at 11 o’clock in at1 with a priority of 2 the total resolution of lecture would be lecture(abs, 7, monday, 11, at1, 2) and these values can now be used further ahead. In this case it is checked if the current time matches the time of the lecture and the same for the week, if such a lecture is found the plan is then executed. Remember that variables all start with a capital letter and atoms (the actual values) are all lowercase. Atoms can also be used in contexts e.g. in 5 +!gotoforum(Me) : pos(frum1, X, Y) & pos(Me, Xm, Ym) & X == Xm & Y == Ym. The atom ’frum1’ is used which ensures that X and Y will unify with the x and y values of the frum1 building. Playing around with the environment Try playing around with the lecturer agent and the config file try e.g.: • Adding several lecturer agents and assign them differing courses, events etc. • Blocking a building (right-click the building) that an agent wants to enter • Changing the number and names of courses and/or events • Running an agent for a few hours - stopping it and inspect its debug window • Changing the name and location of a few buildings • Making agents go to non-existent buildings • Changing the speed of hours passing • Changing the lecturers priorities of events and courses and make a clash happen • Making the lecturer go home from the forum later/earlier/work in the library instead • Making the lecturer excercise in the gym instead of going to lunch • If you have time you can start to work on the assignment A infospeak.config.BACKUP file is provided which you can use to reset the environment should you need it. I will be available to answer questions and the like for the rest of the lab while you try out various things. Idiosyncrasies to be aware of and a few hints and tips • When working on the assignment you may find the following link useful - http://agentgame.mit.bme.hu/files/Jason/Jason_konyv.pdf • The InfoSpeak environment does not deal well with large numbers of agents (above 5 or 6). There is no need to run more than 4 (or less) in your assignments. 6 • If you have paused the environment in order to debug do not resume the simulation. The environment does not deal well with this. Close the simulation and restart it instead. • The number of agents and names of agents defined in the .mas2j file and .config file must match. • If the context of several plans applies the first plan the interpreter encoun- ters will be executed (ordering is important) • An agent which is executing a plan with several plan_steps will not drop later steps automatically if an earlier step adds additional steps to do - rather it will do the newly added steps first and then resume executing the later steps. If you wish you can reset all agents intentions, desires etc. using the actions .drop_all_intentions or .drop_all_desires (the above provided link may help in clarifying this). • While it may be useful to take a look at the ISEnv.java file it should not be necessary for you to change it, if you feel for some reason this has become necessary - you’re either doing something wrong or there’s a bug I should know about. 7