+ All Categories
Home > Documents > Advanced Functional Programming

Advanced Functional Programming

Date post: 22-Feb-2016
Category:
Upload: milos
View: 21 times
Download: 0 times
Share this document with a friend
Description:
PASTURE. Advanced Functional Programming. Daniel Strebel Eduardo Hernández Marquina. The Problem. The board: A grid where the different elements are going to interact. The Problem Cont. Elements Fences : Static elements Grass : Procreate. The Problem Cont. Elements Cont : - PowerPoint PPT Presentation
17
Daniel Strebel Eduardo Hernández Marquina Advanced Functional Programming PASTURE
Transcript
Page 1: Advanced Functional Programming

Daniel StrebelEduardo Hernández Marquina

Advanced Functional Programming

PASTURE

Page 2: Advanced Functional Programming

The board:

A grid where the different elements are going to interact.

The Problem

Page 3: Advanced Functional Programming

Elements

»Fences:• Static elements

»Grass:• Procreate

The Problem Cont.

Page 4: Advanced Functional Programming

Elements Cont:

Abilities:

- Move

- Eat

- Procreate

The Problem Cont.

Page 5: Advanced Functional Programming

Design

GameBoard

Page 6: Advanced Functional Programming

Design cont.

Fox_1 Rabbit_1

Grass_1

Rabbit_2

{4,3}, rabbit_

1

{3,6}, fox_2

{1,4}, grass_2

{4,8}, grass_3

{7,3}, grass_1

{5,8}, rabbit_

2{8,5}, fox_1

GameBoard

Todo

Position-PID

Page 7: Advanced Functional Programming

Design cont.GameBoard

Initialization

Todo > 0

Build new Todo Wait for

Message

NO

YES

Page 8: Advanced Functional Programming

Design cont.GameBoard

GameBoard

time

{start}

{move, 3,2 }{ok}

{req_neighbors}{neighbors, [{5,4,

fox},.]}

Page 9: Advanced Functional Programming

Design cont.GameBoard

GameBoard

time

{start}

{move, 3,2 }{conflict, [{5,4,

fox},.]}

{req_neighbors}{neighbors, [{5,4,

fox},.]}

{eat, 4,4}

Request not valid!

{ok}

Page 10: Advanced Functional Programming

Demo

Page 11: Advanced Functional Programming

• Missing AI- Now action choices are random.

• How to add simple AI?- Need to know the positions around.- Develop action priorities.

1.- Stay alive: scape from predators or eat.2.- To find food: move away from borders.

Limitations

Page 12: Advanced Functional Programming

Algorithms

Atoms for element types

Code sharing between animals

Used libraries

Page 13: Advanced Functional Programming

Algorithms cont

animal_loop(Position, States, Default_States, Types) -> receive

{die, _} -> urks;{start, Gameboard_PID} -> Gameboard_PID ! {request_neighbors, Position, self()}, receive

{die, _} -> urks;{neighbors, Neighbor_List} -> decide_animal(Neighbor_List, Gameboard_PID,

Position, States, Default_States, Types)

end end.

{Eat_timer, Procreation_timer, Starvation_timer, Move_timer}

{Own_Type, Prey_Type}

Page 14: Advanced Functional Programming

Algorithms contdecide_animal(Neighbor_List, Gameboard_PID, Position, States, Default_States, Types) -> {Own_Type, Prey_Type} = Types, {Eat_timer, Procreation_timer, Starvation_timer, Move_timer} = States, Prey_Cells = [{X_cell, Y_cell} || {_, Type, X_cell, Y_cell}<-Neighbor_List, Type==Prey_Type], Free_Cells = [{X_cell, Y_cell} || {_, Type, X_cell, Y_cell}<-Neighbor_List, Type==none],

case {Free_Cells, Prey_Cells, Move_timer, Eat_timer, Procreation_timer} of

{_, [_|_], _, 0, 0} -> % eat timer and procreation timer are zero and there is at least

one neighboring prey cell [...]{[_|_], _, 0, _, _} -> % move timer is zero and there is at least one neighboring empty

cell [...]{_, _, _, _, _} -> % no possible actions [...]

end.

Page 15: Advanced Functional Programming

Why this produces correct results

Gameboard in charge -> consistentElements can always select a possible actions -> no dead locks, single turns will terminateOrder of execution is not guaranteed ->Non-deterministic behaviour

Page 16: Advanced Functional Programming

Difficult cases

Find all possible cases of inconsistencyWhat should be done in conflict situations?

Why the heck is there a dead lock???

Page 17: Advanced Functional Programming

??!! *?#~!

http://code.google.com/p/pasture-uu-ht11/


Recommended