This commit is contained in:
ryuga4 2019-06-11 23:17:44 +02:00
parent f6e30ed848
commit 281f31aec9
34 changed files with 13065 additions and 60 deletions

View File

@ -13,6 +13,7 @@ using Microsoft.Xna.Framework.Content;
using System.Threading; using System.Threading;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models; using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using MonoGameView.DataModels.Models; using MonoGameView.DataModels.Models;
using System.IO;
namespace MonoGameView.Algorithms namespace MonoGameView.Algorithms
{ {
@ -53,7 +54,7 @@ namespace MonoGameView.Algorithms
var r = SearchBestFirst(content, collector, grid, 0); var r = SearchBestFirst(content, collector, grid, 0);
var dataToLog = Jazda(r.Item1, CopyGrid(grid), (GarbageCollector) collector.Clone()).ToList(); Jazda(r.Item1, CopyGrid(grid), (GarbageCollector) collector.Clone());
@ -64,40 +65,68 @@ namespace MonoGameView.Algorithms
} }
public IEnumerable<KeyValuePair<IStep, ICloneable[,]>> Jazda(List<IStep> steps, ICloneable[,] grid, GarbageCollector collector) public void Jazda(List<IStep> steps, ICloneable[,] grid, GarbageCollector collector)
{ {
for (int i =0;i<steps.Count();i++) for (int i =0;i<steps.Count();i++)
{ {
var minGrid = new ICloneable[5, 5]; var s = "";
foreach (var can in collector.TrashContainers)
{
s += " " + can.TypeOfGarbage.GarbageType.ToString() + ":." + (int)can.FillPercent * 100;
}
for (int x = collector.Coords.X - 2; x <= collector.Coords.X + 2; x++) for (int x = collector.Coords.X - 2; x <= collector.Coords.X + 2; x++)
{ {
for (int y = collector.Coords.Y - 2; y <= collector.Coords.Y + 2; y++) for (int y = collector.Coords.Y - 2; y <= collector.Coords.Y + 2; y++)
{ {
var xoffset = x - (collector.Coords.X - 2); var xoffset = x - (collector.Coords.X - 2);
var yoffset = y - (collector.Coords.Y - 2); var yoffset = y - (collector.Coords.Y - 2);
if (x >= 0 && y >= 0 && x < grid.GetLength(0) && y < grid.GetLength(1)) if (x >= 0 && y >= 0 && x < grid.GetLength(0) && y < grid.GetLength(1))
{ {
var cell = grid[x, y]; var cell = grid[x, y];
if (cell is Grass) minGrid[xoffset, yoffset] = "grass"; if (cell is Grass) s += " " + xoffset.ToString() + yoffset.ToString() + "grass";
if (cell is Road1) minGrid[xoffset, yoffset] = "road1"; if (cell is Road1) s += " " + xoffset.ToString() + yoffset.ToString() + "road1";
if (cell is Road2) minGrid[xoffset, yoffset] = "road2"; if (cell is Road2) s += " " + xoffset.ToString() + yoffset.ToString() + "road2";
if (cell is Dump) minGrid[xoffset, yoffset] = (cell as Dump).TypeOfGarbage.GarbageType.ToString(); if (cell is Dump) s += " " + xoffset.ToString() + yoffset.ToString() + (cell as Dump).TypeOfGarbage.GarbageType.ToString();
if (cell is House) minGrid[xoffset, yoffset] = "house"; if (cell is House)
if (cell is EmptyHouse) minGrid[xoffset, yoffset] = "emptyHouse";
} else
{ {
minGrid[xoffset, yoffset] = "grass";
var h = (cell as House);
foreach (var can in h.TrashCans)
{
s += " " + xoffset.ToString() + yoffset.ToString() + can.TypeOfGarbage.GarbageType.ToString() + ":." + (int)can.FillPercent * 100;
} }
} }
if (cell is EmptyHouse) s += " " + xoffset.ToString() + yoffset.ToString() + "emptyHouse";
} }
yield return new KeyValuePair<IStep, ICloneable[,]>(steps[i], minGrid); else
{
s += " " + xoffset.ToString() + yoffset.ToString() + "grass";
}
}
}
for (int l = 0; l < 12; l++)
{
var minGrid = new ICloneable[5, 5];
var s0 = ((steps[i].ID == l) ? 1 : 0) + " |";
using (StreamWriter w = File.AppendText("garbage_dataset" + l + ".txt"))
{
w.WriteLine(s0 + s);
}
}
@ -265,18 +294,18 @@ namespace MonoGameView.Algorithms
var item = p.Key; var item = p.Key;
var priority = p.Value; var priority = p.Value;
/*Thread.Sleep(10); //Thread.Sleep(10);
this.Collector.Coords = item.Item2.Coords; //this.Collector.Coords = item.Item2.Coords;
this.Collector.TrashContainers = item.Item2.TrashContainers; //this.Collector.TrashContainers = item.Item2.TrashContainers;
for (int x = 0; x < item.Item3.GetLength(0); x++) //for (int x = 0; x < item.Item3.GetLength(0); x++)
{ //{
for (int y = 0; y < item.Item3.GetLength(1); y++) // for (int y = 0; y < item.Item3.GetLength(1); y++)
{ // {
this.Grid[x, y] = item.Item3[x, y]; // this.Grid[x, y] = item.Item3[x, y];
} // }
}*/ //}
if (Houses.All(c => (item.Item3[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0)) if (Houses.All(c => (item.Item3[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0 || j.FillPercent == double.NaN))
&& &&
item.Item2.TrashContainers.All(i => i.FillPercent == 0.0) item.Item2.TrashContainers.All(i => i.FillPercent == 0.0)
) )

View File

@ -0,0 +1,316 @@
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Content;
using System.Threading;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.Algorithms
{
public class Vowpal
{
public GarbageCollector Collector { get; set; }
public ICloneable[,] Grid { get; set; }
public Vowpal(GarbageCollector collector, ICloneable[,] grid)
{
this.Collector = collector;
this.Grid = grid;
}
int count = 0;
public List<Coords> Houses { get; set; }
public KeyValuePair<List<IStep>, int> BestPath(ContentManager content, GarbageCollector collector, ICloneable[,] grid)
{
Houses = new List<Coords>();
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is House)
{
Houses.Add(new Coords(x, y));
}
}
}
var r = SearchBfs(content, collector, grid, 0);
Console.WriteLine($"Counts : {count}");
if (r.Key == null) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), 0);
return r;
}
List<IStep> PossibleSteps(AGarbageCollector collector, ICloneable[,] grid)
{
var result = new List<IStep>();
var itemdupa = grid[collector.Coords.X, collector.Coords.Y];
if (grid[collector.Coords.X, collector.Coords.Y] is House)
{
var collectSteps = new List<IStep>()
{
new CollectStep(GarbageType.Glass),
new CollectStep(GarbageType.Organic),
new CollectStep(GarbageType.Paper),
new CollectStep(GarbageType.PlasticMetal)
};
foreach (var item in collectSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid);
if (item.Invoke(copyCollector, copyGrid))
result.Add(item);
}
}
if (grid[collector.Coords.X, collector.Coords.Y] is ADump)
{
var collectSteps = new List<IStep>()
{
new SpillStep(GarbageType.Glass),
new SpillStep(GarbageType.Organic),
new SpillStep(GarbageType.Paper),
new SpillStep(GarbageType.PlasticMetal)
};
foreach (var item in collectSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid);
if (item.Invoke(copyCollector, copyGrid))
result.Add(item);
}
}
if (!(grid[collector.Coords.X, collector.Coords.Y] is Dump && collector.TrashContainers.Any(x => x.FillPercent > 0 && (grid[collector.Coords.X, collector.Coords.Y] as Dump).TypeOfGarbage.GarbageType == x.TypeOfGarbage.GarbageType))
&& !(grid[collector.Coords.X, collector.Coords.Y] is House
&& (grid[collector.Coords.X, collector.Coords.Y] as House).TrashCans.Any(j => j.FillPercent > 0 && collector.TrashContainers.Any(i => i.FillPercent < 1 && i.Garbage.TypeOfGarbage.GarbageType == j.TypeOfGarbage.GarbageType))))
{
var moveSteps = new List<IStep>()
{
new MoveStep(Direction.Up),
new MoveStep(Direction.Down),
new MoveStep(Direction.Left),
new MoveStep(Direction.Right)
};
var filteredMoveSteps = new List<IStep>();
foreach (var item in moveSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
if (item.Invoke(copyCollector, grid))
{
var gcx = copyCollector.Coords.X;
var gcy = copyCollector.Coords.Y;
if (grid[gcx, gcy] is Road1 || grid[gcx, gcy] is House || (grid[gcx, gcy] is ADump && copyCollector.TrashContainers.Any(x => x.FillPercent > 0)))
{
result.Add(item);
}
}
}
}
return result;
}
string Line(GarbageCollector collector, ICloneable [,] grid)
{
var s = "";
foreach (var can in collector.TrashContainers)
{
s += " " + can.TypeOfGarbage.GarbageType.ToString() + ":." + (int)can.FillPercent * 100;
}
for (int x = collector.Coords.X - 2; x <= collector.Coords.X + 2; x++)
{
for (int y = collector.Coords.Y - 2; y <= collector.Coords.Y + 2; y++)
{
var xoffset = x - (collector.Coords.X - 2);
var yoffset = y - (collector.Coords.Y - 2);
if (x >= 0 && y >= 0 && x < grid.GetLength(0) && y < grid.GetLength(1))
{
var cell = grid[x, y];
if (cell is Grass) s += " " + xoffset.ToString() + yoffset.ToString() + "grass";
if (cell is Road1) s += " " + xoffset.ToString() + yoffset.ToString() + "road1";
if (cell is Road2) s += " " + xoffset.ToString() + yoffset.ToString() + "road2";
if (cell is Dump) s += " " + xoffset.ToString() + yoffset.ToString() + (cell as Dump).TypeOfGarbage.GarbageType.ToString();
if (cell is House)
{
var h = (cell as House);
foreach (var can in h.TrashCans)
{
s += " " + xoffset.ToString() + yoffset.ToString() + can.TypeOfGarbage.GarbageType.ToString() + ":." + (int)can.FillPercent * 100;
}
}
if (cell is EmptyHouse) s += " " + xoffset.ToString() + yoffset.ToString() + "emptyHouse";
}
else
{
s += " " + xoffset.ToString() + yoffset.ToString() + "grass";
}
}
}
return "| " + s;
}
KeyValuePair<List<IStep>, int> SearchBfs(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length)
{
//Thread.Sleep(1);
var steps = new List<IStep>();
int count = 0;
while (true)
{
count++;
Thread.Sleep(5);
this.Collector.Coords = collector.Coords;
this.Collector.TrashContainers = collector.TrashContainers;
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
this.Grid[x, y] = grid[x, y];
}
}
if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0))
&&
collector.TrashContainers.All(i => i.FillPercent == 0.0)
)
{
Console.WriteLine(count);
return new KeyValuePair<List<IStep>, int>(steps, count);
}
var possible = PossibleSteps(collector, grid);
if (possible.Count == 0)
{
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y));
}
}
}
else
{
var mapped = possible.Select(x =>
{
using (var vw = new VW.VowpalWabbit($" -i garbage{x.ID}.model"))
{
var line = Line(collector, grid);
double result = vw.Predict(Line(collector, grid), VW.VowpalWabbitPredictionType.Scalar);
return new KeyValuePair<IStep, double>(x, result);
}
}).ToArray();
var sum = mapped.Aggregate(0.0, (a, b) => a + b.Value);
Random random = new Random();
var rnd = random.NextDouble() * sum;
double cumulative = 0.0;
for (int i = 0; i < mapped.Count(); i++)
{
cumulative += mapped[i].Value;
if (rnd <= cumulative)
{
var best = mapped[i].Key;
steps.Add(best);
best.Invoke(collector, grid);
break;
}
}
}
}
}
private ICloneable[,] CopyGrid(ICloneable[,] grid)
{
ICloneable[,] result = new ICloneable[grid.GetLength(0), grid.GetLength(1)];
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
result[x, y] = (ICloneable)grid[x, y].Clone();
}
}
return result;
}
}
}

View File

@ -5,6 +5,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces
{ {
public interface IStep public interface IStep
{ {
int ID { get; }
bool Invoke(IGarbageCollector collector, ICloneable [,] grid); bool Invoke(IGarbageCollector collector, ICloneable [,] grid);
} }
} }

View File

@ -19,7 +19,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{ {
this._typeOfGarbage = typeOfGarbage; this._typeOfGarbage = typeOfGarbage;
} }
public int ID { get
{
return (int) _typeOfGarbage;
} }
private GarbageType _typeOfGarbage; private GarbageType _typeOfGarbage;
public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid) public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid)
@ -49,7 +52,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x,y)); if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x,y));
} }
} }
if (_garbageLocalization.TrashCans.All(x => x.FillPercent == 0.0)) if (_garbageLocalization.TrashCans.All(x => x.FillPercent == 0.0 || x.FillPercent == double.NaN))
{ {
grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y)); grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));

View File

@ -16,7 +16,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{ {
this._direction = direction; this._direction = direction;
} }
public int ID { get
{
return (int)_direction + 4;
} }
private Direction _direction; private Direction _direction;
private IGarbageCollector _garbageCollector; private IGarbageCollector _garbageCollector;

View File

@ -17,7 +17,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{ {
this._typeOfGarbage = typeOfGarbage; this._typeOfGarbage = typeOfGarbage;
} }
public int ID { get
{
return (int) _typeOfGarbage + 8;
} }
private GarbageType _typeOfGarbage; private GarbageType _typeOfGarbage;
public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid) public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid)

View File

@ -64,7 +64,7 @@ namespace CzokoŚmieciarka.MonoGameView
// TODO: Add your initialization logic here // TODO: Add your initialization logic here
timer = 1f; timer = 1f;
mapLoader.Load(out size,out grid,"mapa4.xml"); mapLoader.Load(out size,out grid,"mapa5.xml");
var containers = new List<GarbageCollectorContainer>() var containers = new List<GarbageCollectorContainer>()
{ {
new GarbageCollectorContainer( new GarbageCollectorContainer(
@ -93,15 +93,15 @@ namespace CzokoŚmieciarka.MonoGameView
stepN = 0; stepN = 0;
var dfs = new BestFirstSearch(collector,grid); var dfs = new Vowpal(collector,grid);
var r = dfs.BestPath(Content, collector, grid); //var r = dfs.BestPath(Content, collector, grid);
steps = r.Item1; //steps = r.Key;
Console.WriteLine(r);
//displayer.Add("Count", r.Value.ToString());
//new Thread(delegate() {
// var x = dfs.BestPath(Content, collector, grid);
//}).Start(); //displayer.Add("Count", r.Value.ToString());
new Thread(delegate() {
var x = dfs.BestPath(Content, collector, grid);
}).Start();
base.Initialize(); base.Initialize();
} }
@ -139,17 +139,7 @@ namespace CzokoŚmieciarka.MonoGameView
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit(); Exit();
var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
timer -= elapsed;
if (timer<0)
{
timer = TIMER;
if (steps.Any())
{
steps.First().Invoke(collector, grid);
steps.RemoveAt(0);
}
}
// TODO: Add your update logic here // TODO: Add your update logic here
var barsDictionary = new Dictionary<string, int>(); var barsDictionary = new Dictionary<string, int>();

View File

@ -17,7 +17,7 @@
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
@ -49,6 +49,7 @@
<Compile Include="Algorithms\BFS.cs" /> <Compile Include="Algorithms\BFS.cs" />
<Compile Include="Algorithms\DFS.cs" /> <Compile Include="Algorithms\DFS.cs" />
<Compile Include="Algorithms\PriorityQueue.cs" /> <Compile Include="Algorithms\PriorityQueue.cs" />
<Compile Include="Algorithms\Vowpal.cs" />
<Compile Include="DataModels\Displayer.cs" /> <Compile Include="DataModels\Displayer.cs" />
<Compile Include="DataModels\Enums\Directions.cs" /> <Compile Include="DataModels\Enums\Directions.cs" />
<Compile Include="DataModels\Enums\GarbageTypes.cs" /> <Compile Include="DataModels\Enums\GarbageTypes.cs" />
@ -102,6 +103,15 @@
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="VowpalWabbit, Version=8.4.0.3, Culture=neutral, PublicKeyToken=a76afd1645210483, processorArchitecture=AMD64">
<HintPath>..\packages\VowpalWabbit.8.4.0.3\lib\net45\VowpalWabbit.dll</HintPath>
</Reference>
<Reference Include="VowpalWabbit.Common, Version=8.4.0.3, Culture=neutral, PublicKeyToken=a76afd1645210483, processorArchitecture=AMD64">
<HintPath>..\packages\VowpalWabbit.8.4.0.3\lib\net45\VowpalWabbit.Common.dll</HintPath>
</Reference>
<Reference Include="VowpalWabbit.Core, Version=8.4.0.3, Culture=neutral, PublicKeyToken=a76afd1645210483, processorArchitecture=AMD64">
<HintPath>..\packages\VowpalWabbit.8.4.0.3\lib\net45\VowpalWabbit.Core.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Icon.ico" /> <Content Include="Icon.ico" />

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="C5" version="2.5.3" targetFramework="net461" /> <package id="C5" version="2.5.3" targetFramework="net461" />
<package id="VowpalWabbit" version="8.4.0.3" targetFramework="net461" />
</packages> </packages>

1
Trunk/mapa5.xml Normal file

File diff suppressed because one or more lines are too long

BIN
Trunk/vp/garbage0.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage1.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage10.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage11.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage2.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage3.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage4.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage5.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage6.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage7.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage8.model Normal file

Binary file not shown.

BIN
Trunk/vp/garbage9.model Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff