Git Product home page Git Product logo

goap's Introduction

GOAP

Goal Oriented Action Planning

License

The MIT License (MIT)

Copyright (c) 2014 Nikolay Kush

goap's People

Contributors

foc- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

goap's Issues

Depth first search doesn't work; workaround/fix for it

If we follow the example from here;- https://github.com/stolk/GPGOAP

There are 4 steps which should be generated, but plugging in the initial state and goal state from that example page doesn't work in this GOAP project.

I found an alternative;- simply run a check on all the possible combinations/permutations of actions.

using System;
using System.Linq;
using System.IO;
 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using System.Collections.Generic;
using GOAP;
using GOAP.Planning;
 
namespace game1
{
	public partial class Game1
	{
	GOAP_1 test = new GOAP_1();
	
	public class GOAP_1
	{
		public GOAP_1()
		{
			
			var planningActions = new List<PlanningAction<State>>
                {
                    new PlanningAction<State>(
                        name:"load",
                        validator: x => x["armedWithGun"] == 1,
                        executor: x => 
                            {
                                x["weaponLoaded"] = 1;
                            }),
                    new PlanningAction<State>(
                        name:"detonateBomb",
                        validator: x => x["armedWithBomb"] == 1 && x["nearEnemy"] == 1,
                        executor: x => 
                            {
                                x["alive"] = 0;
                                x["enemyAlive"] = 0;
                            }),
                    new PlanningAction<State>(
                        name:"shoot",
                        validator: x => x["enemyLinedUp"] == 1,
                        executor: x => 
                            {
                                x["enemyAlive"] = 0;
                            }),
                    new PlanningAction<State>(
                        name:"aim",
                        validator: x => x["enemyVisible"] == 1 && x["weaponLoaded"] == 1,
                        executor: x => 
                            {
                                x["enemyLinedUp"] = 1;
                            }),
                    new PlanningAction<State>(
                        name:"approach",
                        validator: x => x["enemyVisible"] == 1,
                        executor: x => 
                            {
                                x["nearEnemy"] = 1;
                            }),
                    new PlanningAction<State>(
                        name: "scout",
                        validator: x => x["armedWithGun"] == 1,
                        executor: x =>
                            {
                                x["enemyVisible"] = 1;
                            }),
                    new PlanningAction<State>(
                        name:"flee",
                        validator: x => x["enemyVisible"] == 1,
                        executor: x => 
                            {
                                x["nearEnemy"] = 0;
                            }),
                };
			

            initialState = new State
            {
                {"armedWithGun" , 1},
                {"enemyVisible" , 0},
                {"nearEnemy" , 0},
                {"weaponLoaded" , 0},
                {"enemyLinedUp" , 0},
                {"enemyAlive" , 1},
                {"armedWithBomb" , 1},
                {"alive" , 1},
            };
            goalState = new State
            {
                {"enemyAlive" , 0},
                {"alive" , 1},
            };
            
	        
	        var steps = makePlan(initialState, goalState, planningActions);
	        foreach(var step in steps) //plan
	        {
	        	//..
	        }
	        
		}
		
		
		static List<PlanningAction<State>> makePlan(State initialState, State goalState, List<PlanningAction<State>> actions)
		{
			
			
			
			foreach(var action in actions)
			{
				var qualified = action.validator((State)initialState.Clone());
				if(!qualified) continue;
				var state1 = action.Execute((State)initialState.Clone());
				foreach(var action2 in actions)
				{
					if(action2 != action)
					{
						var qualified2 = action2.validator(state1);
						if(!qualified2) continue;
						var state2 = action2.Execute(state1);
						foreach(var action3 in actions)
						{
							if(action3 != action && action3 != action2)
							{
								var qualified3 = action3.validator(state2);
								if(!qualified3) continue;
								var state3 = action3.Execute(state2);
								foreach(var action4 in actions)
								{
									if(action4 != action && action4 != action2 && action4 != action3)
									{
										var qualified4 = action4.validator(state3);
										if(!qualified4) continue;
										var state4 = action4.Execute(state3);
										foreach(var action5 in actions)
										{
											if(action5 != action && action5 != action2 && action5 != action3 && action5 != action4)
											{
												var qualified5 = action5.validator(state4);
												if(!qualified5) continue;
												var state5 = action5.Execute(state4);
											}
										}
										if(goalStateSatisfied(state4, goalState)) return makeSteps(action, action2, action3, action4);
									}
								}
								if(goalStateSatisfied(state3, goalState)) return makeSteps(action, action2, action3);
							}
						}
						if(goalStateSatisfied(state2, goalState)) return makeSteps(action, action2);
					}
				}
				if(goalStateSatisfied(state1, goalState)) return makeSteps(action);
			}
		
			return new List<PlanningAction<State>>();
			
			
		}
		
		static bool goalStateSatisfied(State currState, State goalState)
		{
			foreach(var kvp in goalState)
			{
				if(kvp.Value == currState[kvp.Key])
					continue;
				return false;
			}
			return true;
		}
		
		static List<PlanningAction<State>> makeSteps(params PlanningAction<State>[] actions)
		{
			List<PlanningAction<State>> steps = new List<PlanningAction<State>>();
			foreach(var action in actions)
				steps.Add(action);
			return steps;
		}
		
		
		
        static State initialState;
        static State goalState;
		
		
		
		
		
		
	}





}

}

License?

Can you attach a license file for this repository? Or can we reference the code freely?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.