best first

This commit is contained in:
ryuga4 2019-05-14 19:55:00 +02:00
parent ef2ef838a2
commit 11c1c934fc
8 changed files with 56 additions and 30 deletions

View File

@ -130,19 +130,19 @@ namespace MonoGameView.Algorithms
//Thread.Sleep(1);
int count = 0;
var nodes = new List<Tuple<List<IStep>, GarbageCollector, ICloneable[,]>>() {
new Tuple<List<IStep>, GarbageCollector, ICloneable[,]>(new List<IStep>(),collector,grid)
};
var nodes = new Queue<Tuple<List<IStep>, GarbageCollector, ICloneable[,]>>();
nodes.Enqueue(new Tuple<List<IStep>, GarbageCollector, ICloneable[,]>(new List<IStep>(), collector, grid));
while (true)
{
Console.WriteLine(count);
Console.WriteLine(nodes.Count);
count++;
var nodes2 = new List<Tuple<List<IStep>, GarbageCollector, ICloneable[,]>>();
foreach (var item in nodes)
//var nodes2 = new List<Tuple<List<IStep>, 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<IStep>();
steps.AddRange(item.Item1);
steps.Add(step);
nodes2.Add(new Tuple<List<IStep>, GarbageCollector, ICloneable[,]>(steps, collectorClone, gridClone));
nodes.Enqueue(new Tuple<List<IStep>, GarbageCollector, ICloneable[,]>(steps, collectorClone, gridClone));
}
}
}
nodes = nodes2;
}
}

View File

@ -127,7 +127,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
KeyValuePair<List<IStep>, 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;

View File

@ -34,6 +34,15 @@ 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;
if (this.Garbage.Volume + garbage.Volume > this.MaxVolume)
{
this.Garbage.Weight = this.MaxVolume;
garbage.Weight = (this.MaxVolume - this.Garbage.Volume);
return true;
} else
{
var newGarbage = this.Garbage + garbage;
if (newGarbage.Volume > this.MaxVolume)
@ -42,20 +51,35 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
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;
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;
}
}
}
}

View File

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

View File

@ -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);

View File

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

View File

@ -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<GarbageCollectorContainer>()
{
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);

View File

@ -45,8 +45,10 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Compile Include="Algorithms\BestFirstSearch.cs" />
<Compile Include="Algorithms\BFS.cs" />
<Compile Include="Algorithms\DFS.cs" />
<Compile Include="Algorithms\PriorityQueue.cs" />
<Compile Include="DataModels\Displayer.cs" />
<Compile Include="DataModels\Enums\Directions.cs" />
<Compile Include="DataModels\Enums\GarbageTypes.cs" />