public Problem(int smallBucketCapacity, int bigBucketCapacity, int goalVolume) { _smallBucketCapacity = smallBucketCapacity; _bigBucketCapacity = bigBucketCapacity; _goalVolume = goalVolume; _initialState = new ProblemState(smallBucketCapacity, bigBucketCapacity); }
public string PathFromRoot() { HashSet <ProblemState> states = new HashSet <ProblemState> { State }; Node node = Parent; while (node != null) { states.Add(node.State); node = node.Parent; } states = states.Reverse().ToHashSet(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < states.Count; i++) { ProblemState nextState = states.ElementAt(i); sb.Append(nextState); if (i + 1 != states.Count) { sb.Append("->"); } } return(sb.ToString()); }
private static Node GetExcludedNode(ProblemState state) { ProblemState ps = new ProblemState(state); ps.SmallBucket.FillUp(); ps.BigBucket.FillUp(); return(new Node(ps)); }
public static HashSet <Node> Expand(Node node) { HashSet <Node> successors = new HashSet <Node>(); ProblemState state = node.State; if (state.SmallBucket.IsEmpty()) { ProblemState ps = new ProblemState(node.State); ps.SmallBucket.FillUp(); successors.Add(new Node(ps, node)); } if (state.BigBucket.IsEmpty()) { ProblemState ps = new ProblemState(node.State); ps.BigBucket.FillUp(); successors.Add(new Node(ps, node)); } if (!state.SmallBucket.IsEmpty()) { ProblemState ps = new ProblemState(node.State); ps.SmallBucket.Dump(); successors.Add(new Node(ps, node)); if (!state.BigBucket.IsFull()) { ps = new ProblemState(node.State); ps.SmallBucket.PourWaterInto(ps.BigBucket); successors.Add(new Node(ps, node)); } } if (!state.BigBucket.IsEmpty()) { ProblemState ps = new ProblemState(node.State); ps.BigBucket.Dump(); successors.Add(new Node(ps, node)); if (!state.SmallBucket.IsFull()) { ps = new ProblemState(node.State); ps.BigBucket.PourWaterInto(ps.SmallBucket); successors.Add(new Node(ps, node)); } } HashSet <Node> parentNodes = node.GetParentNodes(); successors.RemoveWhere(n => parentNodes.Contains(n) || Equals(n, GetExcludedNode(state))); return(successors); }
/// <summary> /// Returns a boolean value whether specified state is the goal state /// </summary> public bool IsGoalState(ProblemState state) { if (Equals(state.SmallBucket.GetVolume(), _goalVolume)) { return(true); } if (Equals(state.BigBucket.GetVolume(), _goalVolume)) { return(true); } return(false); }
public Node(ProblemState state) : this(state, null) { }
public Node(ProblemState state, Node parent) { State = state ?? throw new ArgumentNullException(nameof(state)); Parent = parent; }