public object Clone() { var newJug = new Jug(Capacity); newJug.UpdateContent(Current); return(newJug); }
/// <summary> /// Apply rules /// </summary> /// <returns>A <see cref="bool"/> denoting if any solution was applied in current state</returns> bool ApplyRules() { var hasSuccess = false; foreach (var ruleNumber in _rulesOrder) { var rule = _rules[ruleNumber]; var ruleWasApplied = rule(J1, J2); var alreadyVisited = CheckAlreadyVisited(); hasSuccess = !alreadyVisited && ruleWasApplied; if (hasSuccess) { EnqueuePath(ruleNumber); break; } else { J1 = (Jug)_currentState.j1.Clone(); J2 = (Jug)_currentState.j2.Clone(); } } return(hasSuccess); }
public void PopulateOpenList() { foreach (var rule in Jug.GetTransferRules()) { JugPath jugPath; var jugClone1 = (Jug)_currentPath.J1.Clone(); var jugClone2 = (Jug)_currentPath.J2.Clone(); if (rule.Value(jugClone1, jugClone2)) { jugPath = new JugPath(jugClone1, jugClone2, _open.Count) { Anterior = _currentPath }; if (!jugPath.Paths.Any(item => item.J1.Current == jugClone1.Current && item.J2.Current == jugClone2.Current) ) { _open.Add(jugPath); } } } OrderOpenList(); }
public JugProblemGuloso(int capacity1, int capacity2) { _j1 = new Jug(capacity1); _j2 = new Jug(capacity2); _open = new List <JugPath>(); _close = new List <JugPath>(); CreateNewJugPath(_j1, _j2, 0); }
public JugState(int ruleNumber, Jug jug1, Jug jug2) { AppliedRule = new List <int>() { ruleNumber }; j1 = (Jug)jug1.Clone(); j2 = (Jug)jug2.Clone(); }
public JugProblem(int m, int n) { J1 = new Jug(m); J2 = new Jug(n); SetSort(SortType.Asc); _rules = Jug.GetTransferRules(); _currentState = new JugState(0, J1, J2); }
public void CreateNewJugPath(Jug j1, Jug j2, int count) { _currentPath = new JugPath(j1, j2, count); _close.Add(_currentPath); Console.WriteLine("Fechados: {0}", string.Join(",", _close.Select(item => $"({item.J1.Current}, {item.J2.Current} [{item.Order}])") ) ); }
/// <summary> /// Transfer water from current <see cref="Jug"/> to destination <see cref="Jug"/> /// </summary> /// <param name="current">The current jug</param> /// <param name="destination">The destination jug</param> /// <returns>A <see cref="bool"/> denoting if this rule was applied</returns> bool TransferWater(Jug current, Jug destination) { if (current.IsEmpty() || destination.IsFull()) { return(false); } var amount = Math.Min(current.Current, destination.EmptySpace); current.UpdateContent(-amount); destination.UpdateContent(amount); return(true); }
/// <summary> /// Backtrack for last state to try apply another rule. /// </summary> /// <returns>A <see cref="bool"/> denoting if backtracking was sucedeed</returns> bool BacktrackRule() { if (_path.Count == 0) { return(false); } _visitedStates.Add(_currentState); _currentState = _path.Pop(); J1 = (Jug)_currentState.j1.Clone(); J2 = (Jug)_currentState.j2.Clone(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"({_currentState.j1.Current}, {_currentState.j2.Current}) => BT - Backtracking"); Console.ResetColor(); return(true); }
public JugProblem(int m, int n) { J1 = new Jug(m); J2 = new Jug(n); SetSort(SortType.Asc); _rules = new Dictionary <int, Func <Jug, Jug, bool> > { { 1, (current, destination) => current.FillJug() }, { 2, (current, destination) => destination.FillJug() }, { 3, (current, destination) => current.EmptyJug() }, { 4, (current, destination) => destination.EmptyJug() }, { 5, (current, destination) => TransferWater(current, destination) }, { 6, (current, destination) => TransferWater(destination, current) }, }; _currentState = new JugState(0, J1, J2); }