From 11c1c934fca48280962ea3e7de7e43645e17c060 Mon Sep 17 00:00:00 2001 From: ryuga4 Date: Tue, 14 May 2019 19:55:00 +0200 Subject: [PATCH] best first --- Trunk/MonoGameView/Algorithms/BFS.cs | 15 ++++--- Trunk/MonoGameView/Algorithms/DFS.cs | 2 +- .../Interfaces/TrashCans/ATrashCan.cs | 42 +++++++++++++++---- Trunk/MonoGameView/DataModels/MapLoader.cs | 9 +--- .../DataModels/Models/Steps/CollectStep.cs | 7 +++- .../DataModels/Models/Steps/SpillStep.cs | 3 +- Trunk/MonoGameView/Game1.cs | 6 +-- Trunk/MonoGameView/MonoGameView.csproj | 2 + 8 files changed, 56 insertions(+), 30 deletions(-) diff --git a/Trunk/MonoGameView/Algorithms/BFS.cs b/Trunk/MonoGameView/Algorithms/BFS.cs index 93b3f81..64adc2c 100644 --- a/Trunk/MonoGameView/Algorithms/BFS.cs +++ b/Trunk/MonoGameView/Algorithms/BFS.cs @@ -130,19 +130,19 @@ namespace MonoGameView.Algorithms //Thread.Sleep(1); int count = 0; - var nodes = new List, GarbageCollector, ICloneable[,]>>() { - new Tuple, GarbageCollector, ICloneable[,]>(new List(),collector,grid) - }; + var nodes = new Queue, GarbageCollector, ICloneable[,]>>(); + nodes.Enqueue(new Tuple, GarbageCollector, ICloneable[,]>(new List(), collector, grid)); while (true) { Console.WriteLine(count); Console.WriteLine(nodes.Count); count++; - var nodes2 = new List, GarbageCollector, ICloneable[,]>>(); - foreach (var item in nodes) + //var nodes2 = new List, GarbageCollector, ICloneable[,]>>(); + while (true) { - //Thread.Sleep(1); + var item = nodes.Dequeue(); + Thread.Sleep(10); this.Collector.Coords = item.Item2.Coords; this.Collector.TrashContainers = item.Item2.TrashContainers; for (int x = 0; x < item.Item3.GetLength(0); x++) @@ -174,13 +174,12 @@ namespace MonoGameView.Algorithms var steps = new List(); steps.AddRange(item.Item1); steps.Add(step); - nodes2.Add(new Tuple, GarbageCollector, ICloneable[,]>(steps, collectorClone, gridClone)); + nodes.Enqueue(new Tuple, GarbageCollector, ICloneable[,]>(steps, collectorClone, gridClone)); } } } - nodes = nodes2; } } diff --git a/Trunk/MonoGameView/Algorithms/DFS.cs b/Trunk/MonoGameView/Algorithms/DFS.cs index 7b143e1..b6496bb 100644 --- a/Trunk/MonoGameView/Algorithms/DFS.cs +++ b/Trunk/MonoGameView/Algorithms/DFS.cs @@ -127,7 +127,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms KeyValuePair, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length) { - //Thread.Sleep(10); + Thread.Sleep(10); this.Collector.Coords = collector.Coords; this.Collector.TrashContainers = collector.TrashContainers; diff --git a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs index fc1b86c..e880049 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs @@ -34,28 +34,52 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans { //if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType) // throw new Exception("You cannot add up different type garbage!"); + if (FillPercent == 1) return false; - var newGarbage = this.Garbage + garbage; - if (newGarbage.Volume > this.MaxVolume) - return false; + if (this.Garbage.Volume + garbage.Volume > this.MaxVolume) + { + this.Garbage.Weight = this.MaxVolume; + garbage.Weight = (this.MaxVolume - this.Garbage.Volume); + return true; + } else + { - this.Garbage = newGarbage; - return true; + var newGarbage = this.Garbage + garbage; + if (newGarbage.Volume > this.MaxVolume) + return false; + + this.Garbage = newGarbage; + return true; + } } + + public virtual object Clone() { throw new NotImplementedException(); } - public virtual AGarbage TakeGarbage() + public virtual AGarbage TakeGarbage(double maxVolume) { if (this.Garbage.Weight == 0) return null; - var result = (AGarbage)this.Garbage.Clone(); - this.Garbage.Weight = 0; - return result; + + if (this.Garbage.Volume > maxVolume) + { + var result = (AGarbage)this.Garbage.Clone(); + result.Weight = maxVolume; + this.Garbage.Weight = this.Garbage.Weight - maxVolume; + return result; + } else + { + + var result = (AGarbage)this.Garbage.Clone(); + this.Garbage.Weight = 0; + return result; + } + } } } diff --git a/Trunk/MonoGameView/DataModels/MapLoader.cs b/Trunk/MonoGameView/DataModels/MapLoader.cs index 50050b9..7e12961 100644 --- a/Trunk/MonoGameView/DataModels/MapLoader.cs +++ b/Trunk/MonoGameView/DataModels/MapLoader.cs @@ -68,10 +68,8 @@ namespace MonoGameView.DataModels type = GarbageType.PlasticMetal; break; } - int canDensity = Convert.ToInt32(trashCanNode.SelectSingleNode("Density").InnerText); - int canProcessingTimePerUnit = Convert.ToInt32(trashCanNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); int volume = Convert.ToInt32(trashCanNode.SelectSingleNode("Volume").InnerText); - TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, canDensity,canProcessingTimePerUnit); + TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, 1,0); TrashCan trashCan = new TrashCan(canTypeOfGarbage, volume, new BasicGarbage(canTypeOfGarbage, volume)); trashCans.Add(trashCan); } @@ -101,10 +99,7 @@ namespace MonoGameView.DataModels break; } - int density = Convert.ToInt32(garbageNode.SelectSingleNode("Density").InnerText); - int processingTimePerUnit = Convert.ToInt32(garbageNode.SelectSingleNode("ProcessingTimePerUnit").InnerText); - int maxVolume = Convert.ToInt32(objectNode.SelectSingleNode("Volume").InnerText); - typeOfGarbage = new TypeOfGarbage(garbageType, density, processingTimePerUnit); + typeOfGarbage = new TypeOfGarbage(garbageType, 1,0); Dump dump = new Dump(typeOfGarbage, int.MaxValue, new Coords(x, y)); grid[x, y] = dump; break; diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs index 73fd31a..8660859 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs @@ -29,7 +29,12 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage); if (!trashCans.Any()) return false; - var preGarbage = trashCans.Select(t => t.TakeGarbage()).ToList(); + var mainCan = _garbageCollector + .TrashContainers + .Where(x => x.TypeOfGarbage.GarbageType == _typeOfGarbage) + .First(); + var maxVolume = mainCan.MaxVolume - mainCan.Garbage.Weight; + var preGarbage = trashCans.Select(t => t.TakeGarbage(maxVolume)).ToList(); if (preGarbage.Any(x => x == null)) return false; var garbage = preGarbage.Aggregate((a,b)=>a+b); diff --git a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs index f67d1c4..baef596 100644 --- a/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs +++ b/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs @@ -32,7 +32,8 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps var containers = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage && t.FillPercent > 0); if (containers.Any()) { - var preGarbage = containers.Select(t => t.TakeGarbage()).ToList(); + + var preGarbage = containers.Select(t => t.TakeGarbage(double.MaxValue)).ToList(); if (preGarbage.Any(x => x == null)) return false; var garbage = preGarbage.Aggregate((a, b) => a + b); if (!_dump.AddGarbage(garbage)) return false; diff --git a/Trunk/MonoGameView/Game1.cs b/Trunk/MonoGameView/Game1.cs index 62d46c5..93446c4 100644 --- a/Trunk/MonoGameView/Game1.cs +++ b/Trunk/MonoGameView/Game1.cs @@ -64,12 +64,12 @@ namespace CzokoŚmieciarka.MonoGameView // TODO: Add your initialization logic here timer = 0f; - mapLoader.Load(out size,out grid,"map3.xml"); + mapLoader.Load(out size,out grid,"map1.xml"); var containers = new List() { new GarbageCollectorContainer( new TypeOfGarbage(GarbageType.Glass,1), - 10000, + 50, new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,1),0) ), new GarbageCollectorContainer( @@ -93,7 +93,7 @@ namespace CzokoŚmieciarka.MonoGameView stepN = 0; - var dfs = new DFS(collector,grid); + var dfs = new BestFirstSearch(collector,grid); //steps = dfs.BestPath(Content, collector, grid); new Thread(delegate() { var x = dfs.BestPath(Content, collector, grid); diff --git a/Trunk/MonoGameView/MonoGameView.csproj b/Trunk/MonoGameView/MonoGameView.csproj index 7deca05..b08a46d 100644 --- a/Trunk/MonoGameView/MonoGameView.csproj +++ b/Trunk/MonoGameView/MonoGameView.csproj @@ -45,8 +45,10 @@ + +