GOAP and integer state variables instead of booleans

Started by
0 comments, last by alexzzzz 10 years, 11 months ago

I'm writing a village simulator where virtual people live their virtual lives. Eat, sleep, hunt, gather resources, etc. I'd like to use a goal-oriented action planner to give some brains to my sims, so they could think ahead and plan their actions.

I wrote a planner that uses boolean values to represent the world states, and uses regressive search to form the plans. The rules it uses look like these

rule 1: "go to the forest"

condition: "the sim's location != the forest"

effect: "the sim's location := the forest"

rule 2: "get wood"

conditions: "the sim's location == any forest", "the sim has no wood"

effects: "the sim has wood"

rule 3: "craft fishing rod"

conditions: "the sim has wood"

effects: "the sim has no wood", "the sim has a fishing rod"

rule 4: "go to the lake"

conditions: "the sim's location != the lake"

effects: "the sim's location := the lake"

rule 5: "get water"

conditions: "the sim's location == any lake", 'the sim has no water"

effects: "the sim has water"

rule 6: "get fish"

conditions: "the sim's location == any lake", "the sim has a fishing rod", "the sim has no fish"

effects: "the sim has a fish"

etc

If I give a sim a goal "to have some water", he goes to a nearest lake and takes some water. If a give him a goal "there's a fish in the forest", he goes to a nearest forest, takes a piece of wood, crafts a fishing rod, goes to a nearest lake, catches a fish, goes to the forest that I specified in the goal, and drops the fish there. Works excellent!

But I have no idea how to modify the planner the way it could work with integer values. What if I want a sim to be able to have one, two or more pieces of wood?

Let's make a rule

rule "get an extra piece of wood"

conditions: "the sim's location == any forest", "there's a room in the sim's inventory"

effects "increment amount of wood in the sim's inventory", "decrement amount of wood in the forest"

then set amount of wood in the sim's inventory to zero, and set a goal "the sim has two pieces of wood".

The planner looks for a rule that has an effect "has two pieces of wood" (since it is our goal and the first condition to satisfy), and founds nothing. The rule we've made would fit only if the sim already had one piece of wood, but he doesn't.

Advertisement
The easiest possible solution I came up with, is to automatically unroll any condition like "to have N items of something" into a set on conditions:
- to have N items,
- to have N-1 items,
...
- to have 1 item.

Will try it later to see if there are any pitfalls.


PS

It works!

When I get a condition like "to have 3 items" I convert it to three conditions "to have 1 item", "to have 2 items", "to have 3 items". And when I get a sequence of actions "take 1 item" -> "take 1 item" -> "take 1 item" I convert it back to "take 3 items".
NUMBER OF RULES: 37

GOAL CONDITIONS:
    1) Forest has 1 Fish
    2) Forest has 2 Fish
    3) Forest has 3 Fish


-----------------------------------------------------------------------------------------------
1 - | Cost 0 + penalty 3000000 = 3000000

    2 - drop 1 Fish at Forest | Cost 1 + penalty 2200000 = 2200001

-----------------------------------------------------------------------------------------------
2 - drop 1 Fish at Forest | Cost 1 + penalty 2200000 = 220000

    3 - goto Forest -> drop 1 Fish at Forest | Cost 51 + penalty 2100000 = 2100051
    4 - catch 1 fish -> drop 1 Fish at Forest | Cost 2 + penalty 2200000 = 2200002
    5 - drop 2 Fish at Forest | Cost 1 + penalty 1300000 = 1300001

-----------------------------------------------------------------------------------------------
5 - drop 2 Fish at Forest | Cost 1 + penalty 1300000 = 1300001

    6 - goto Forest -> drop 2 Fish at Forest | Cost 51 + penalty 1200000 = 1200051
    7 - catch 1 fish -> drop 2 Fish at Forest | Cost 2 + penalty 1300000 = 1300002
    8 - drop 3 Fish at Forest | Cost 1 + penalty 400000 = 400001

-----------------------------------------------------------------------------------------------
8 - drop 3 Fish at Forest | Cost 1 + penalty 400000 = 400001

    9 - goto Forest -> drop 3 Fish at Forest | Cost 51 + penalty 300000 = 300051
    10 - catch 1 fish -> drop 3 Fish at Forest | Cost 2 + penalty 400000 = 400002

-----------------------------------------------------------------------------------------------
9 - goto Forest -> drop 3 Fish at Forest | Cost 51 + penalty 300000 = 300051
    
    11 - catch 1 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 266666 = 266718

-----------------------------------------------------------------------------------------------
11 - catch 1 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 266666 = 266718

    12 - goto Lake -> catch 1 fish -> goto Forest -> drop 3 Fish at Forest | Cost 152 + penalty 250000 = 250152
    13 - craft 1 FishingRod -> catch 1 fish -> goto Forest -> drop 3 Fish at Forest | Cost 53 + penalty 275000 = 275053
    14 - catch 2 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 166666 = 166718

-----------------------------------------------------------------------------------------------
14 - catch 2 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 166666 = 166718

    15 - goto Lake -> catch 2 fish -> goto Forest -> drop 3 Fish at Forest | Cost 152 + penalty 150000 = 150152
    16 - craft 1 FishingRod -> catch 2 fish -> goto Forest -> drop 3 Fish at Forest | Cost 53 + penalty 175000 = 175053
    17 - catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 66666 = 66718

-----------------------------------------------------------------------------------------------
17 - catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 52 + penalty 66666 = 66718

    18 - goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 152 + penalty 50000 = 50152
    19 - craft 1 FishingRod -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 53 + penalty 75000 = 75053

-----------------------------------------------------------------------------------------------
18 - goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 152 + penalty 50000 = 50152

    20 - craft 1 FishingRod -> goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 153 + penalty 20000 = 20153

-----------------------------------------------------------------------------------------------
20 - craft 1 FishingRod -> goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 153 + penalty 20000 = 20153

    21 - take 1 Wood at Forest -> craft 1 FishingRod -> goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 154 + penalty 16666 = 16820

-----------------------------------------------------------------------------------------------
21 - take 1 Wood at Forest -> craft 1 FishingRod -> goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 154 + penalty 16666 = 16820

    22 - goto Forest -> take 1 Wood at Forest -> craft 1 FishingRod -> goto Lake -> catch 3 fish -> goto Forest -> drop 3 Fish at Forest | Cost 154 + penalty 0 = 154



FINAL PLAN:
    1) goto Forest
    2) take 1 Wood at Forest
    3) craft 1 FishingRod
    4) goto Lake
    5) catch 3 fish
    6) goto Forest
    7) drop 3 Fish at Forest
The log shows regressive A* search iterations. The planner takes a plan with the lowest "cost+penalty" value from the sorted list, inserts some actions in the beginning and places it back to the list. The plan's "cost" is a sum of all its action costs, the "penalty" is a sum of penalties for all unsatisfied conditions (conditions that are closer to the end of the plan have higher penalties.)

This topic is closed to new replies.

Advertisement