Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
soulblaze1 opened this issue Jul 5, 2019 · 0 comments
Open

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

soulblaze1 opened this issue Jul 5, 2019 · 0 comments

Comments

@soulblaze1
Copy link

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;
		
		
		
		
		
		
	}





}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant