Merge branch 'dev' into Joel-Repo

This commit is contained in:
Joel 2020-05-09 14:27:48 +02:00
commit efd90e9b32
6 changed files with 270 additions and 24 deletions

View File

@ -17,6 +17,21 @@
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x64</PlatformTarget>
@ -63,6 +78,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<Compile Include="Game1.cs" />
<Compile Include="Program.cs" />
@ -96,6 +114,8 @@
<Compile Include="Sources\Pathing\A-Star\PathSaver\Path.cs" />
<Compile Include="Sources\Pathing\A-Star\PathSaver\PriorityQueue.cs" />
<Compile Include="Sources\Controlls\Controller.cs" />
<Compile Include="Sources\Pathing\PQEntry.cs" />
<Compile Include="Sources\Pathing\PriorityQueueC5.cs" />
<Compile Include="Sources\Smart\AI.cs" />
<Compile Include="Sources\Smart\ScoreSystem.cs" />
<Compile Include="Sources\Smart\SmartTractor.cs" />
@ -175,6 +195,9 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="C5">
<Version>2.5.3</Version>
</PackageReference>
<PackageReference Include="LightGBM">
<Version>2.3.1</Version>
</PackageReference>
@ -212,6 +235,18 @@
<Version>4.11.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -11,29 +11,38 @@ class Astar
private Vector2 tractorPos;
private Vector2 housePos;
private Crops[,] crops;
private static Crops[,] crops;
private Vector2 Size;
private PriorityQueue allPaths;
private Vector2 targetPos;
private int Rotation;
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, int rotation)
{
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
housePos = new Vector2((int)newHousePos.X, (int)newHousePos.Y);
targetPos = newtargetPos;
crops = newCrops;
Size = newSize;
Rotation = rotation;
}
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newTargetPos, int rotation)
{
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
housePos = new Vector2((int)newHousePos.X, (int)newHousePos.Y);
crops = newCrops;
Size = newSize;
Rotation = rotation;
targetPos = newTargetPos;
}
public Nodes getOptimalPath()
{
return allPaths.Peek();
}
// Get all adjacent nodes
private List<Nodes> GetAdjacentNodes(Vector2 currentPos)
public List<Nodes> GetAdjacentNodes(Vector2 currentPos)
{
var adjacentNodes = new List<Nodes>()
@ -93,7 +102,7 @@ class Astar
}
// Main function of A* algorithm
public Path FindPath()
public Path FindPath(bool flipArray)
{
int g = 0;
int direction = ConvertRotation();
@ -150,7 +159,9 @@ class Astar
path.AddNode(current);
current = current.getParent();
}
path = path.FlipArray();
if (flipArray)
path = path.FlipArray();
openList.deleteHeap();
closedList.deleteHeap();

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
public class PQEntry : IComparable<PQEntry>
{
public int Key { get; set; }
public Vector2 Coordinates { get; set; }
public int CompareTo(PQEntry other)
{
if (this.Key < other.Key)
return -1;
else if (this.Key > other.Key)
return 1;
else
return 0;
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using C5;
class PriorityQueueC5
{
private readonly Astar astar = new Astar();
private IPriorityQueue<PQEntry> queue = new IntervalHeap<PQEntry>();
public void AddToQueue(List<PQEntry> nodes)
{
foreach (PQEntry item in nodes)
{
PQEntry temp = new PQEntry
{
Key = item.Key,
Coordinates = item.Coordinates
};
queue.Add(temp);
}
}
public void AddToQueue(int x, int y, int score)
{
PQEntry temp = new PQEntry
{
Key = score,
Coordinates = new Vector2(x, y)
};
queue.Add(temp);
}
public PQEntry DeleteMax()
{
return queue.DeleteMax();
}
public void AddAll(List<PQEntry> entryList)
{
queue.AddAll(entryList);
}
}

View File

@ -9,7 +9,6 @@ using Microsoft.Xna.Framework.Graphics;
class AI
{
private Vector2 tractorPos;
private Vector2 housePos;
private Farm farm;
@ -17,9 +16,8 @@ class AI
private Vector2 targetPos;
private Inventory inventory = new Inventory();
private int Rotation;
private PriorityQueueC5 PriorityQueueC5;
private Astar astar = new Astar();
private Random r = new Random();
@ -27,20 +25,9 @@ class AI
public void init()
{
inventory.initInventorySystem();
inventory.addItem(1, 1);
inventory.addItem(1, 1);
inventory.addItem(3, 1);
inventory.addItem(5, 1);
inventory.addItem(7, 1);
inventory.addItem(7, 1);
inventory.addItem(10, 1);
inventory.addItem(10, 1);
inventory.useItem(10, 1);
}
public void update(Farm newFarm, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
{
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
@ -49,6 +36,7 @@ class AI
farm = newFarm;
Size = newSize;
Rotation = rotation;
astar.update(farm.getCrops(), Size, tractorPos, housePos, rotation);
}
@ -60,7 +48,52 @@ class AI
public Vector2 newTarget()
{
return new Vector2(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
PriorityQueueC5 queue = new PriorityQueueC5();
int score = 0;
int count = 0;
int testsize = 2;
Vector2 newTarget;
while (true)
{
for (int x = 0; x < Size.X; x++)
for (int y = 0; y < Size.Y; y++)
{
if (farm.getCrop(x, y).getStatus() >= 2 && tractorPos != new Vector2(x, y))
{
if (farm.getCrop(x, y).getStatus() == 3 && farm.getCrop(x, y).getCropTimer() != 1)
{
if (housePos == tractorPos)
{
score = calculateSoilScore(x, y);
queue.AddToQueue(x, y, score);
count++;
}
//do nothing
}
else
{
score = calculateSoilScore(x, y);
queue.AddToQueue(x, y, score);
count++;
}
}
}
if (count > 0)
break;
else if (tractorPos != housePos)
return housePos;
/*
else if (tractorPos == housePos)
{
List<Nodes> temp = astar.GetAdjacentNodes(tractorPos);
return temp[0].getCords();
}
*/
}
newTarget = GetMinFNode(Math.Min(testsize, count), queue);
return newTarget;
}
public Farm changeCropStatus()
@ -68,6 +101,11 @@ class AI
if (farm.getCrop((int)tractorPos.X, (int)tractorPos.Y).getCropTimer() == 1)
{
farm.setCropStatus(tractorPos.X, tractorPos.Y);
if (farm.getCrop((int)tractorPos.X, (int)tractorPos.Y).getStatus() == 2)
{
inventory.addItem(farm.getCrop((int)tractorPos.X, (int)tractorPos.Y).getCropType() - 1, 1);
}
}
return farm;
}
@ -76,4 +114,89 @@ class AI
{
return inventory;
}
private int calculateSoilScore(int x, int y)
{
int score = 0;
int statusScore = 0;
int aproxDistance = (int)(Math.Abs(x - tractorPos.X) + Math.Abs(y - tractorPos.Y));
CropTypesHolder holder = new CropTypesHolder();
holder.init();
Crops crop = farm.getCrop(x, y);
SoilProperties soilP = crop.getSoilProperties();
int cropType = crop.getCropType();
CropTypes avgHold = holder.getPresetCropTypes(cropType);
if (crop.getStatus() == 2)
statusScore = 25;
else if (crop.getStatus() == 3)
statusScore = 5;
else if (crop.getStatus() == 4)
statusScore = -10;
else
statusScore = 1;
float[] currentValue = { soilP.Temperature, soilP.Humidity, soilP.Moisture, soilP.Nitrogen, soilP.Potassium, soilP.Phosphorous };
float[] targetAvg = { avgHold.Temparature, avgHold.Humidity, avgHold.Moisture, avgHold.Nitrogen, avgHold.Potassium, avgHold.Phosphorous };
float[,] minMax = { {22,30}, {22*1.9f, 30*2.1f}, {20, 70}, {4, 55}, {0, 28}, {0, 60} }; //probably should make it dynamic
float[] normVal = normArray(minMax, currentValue);
float[] normAvg = normArray(minMax, targetAvg);
for (int i = 0; i < normVal.Count(); i++)
{
score = score + (int)Math.Round(System.Convert.ToDouble(10*(1 - Math.Abs(normAvg[i] - normVal[i]))));
}
return score + (-aproxDistance * 3) + statusScore;
}
private float[] normArray(float[,] minMax, float[] values)
{
float[] temp = new float[values.Count()];
if (minMax.GetLength(0) != values.Count())
throw new InvalidOperationException("Values and their MinMax arrays are not of the same length!");
for (int i = 0; i < values.Count(); i++)
temp[i] = norm(minMax[i, 0], minMax[i, 1], values[i]);
return temp;
}
private float norm(float min, float max, float val)
{
return ((val - min) / (max - min));
}
public Vector2 GetMinFNode(int testSize, PriorityQueueC5 queue)
{
int index = 0;
int min = 999;
Path path = new Path();
List<PQEntry> entryList = new List<PQEntry>();
for (int i = 0; i < testSize; i++)
{
entryList.Add(queue.DeleteMax());
}
for (int i = 0; i < testSize; i++)
{
Nodes temp = new Nodes(entryList[i].Coordinates);
path = astar.FindPath(false);
Nodes tempF = new Nodes(path.getByIndex(0));
if (min > tempF.getF())
{
min = tempF.getF();
index = i;
}
}
PQEntry minEntry = entryList[index];
entryList.RemoveAt(index);
//add the non-minimum entries back to the queue.
queue.AddAll(entryList);
return minEntry.Coordinates;
}
}

View File

@ -19,9 +19,11 @@ class SmartTractor
ai.update(farm, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
farm.UpdatePreferedCrops(Size);
farm = ai.changeCropStatus();
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Rotation);
getTargetPosition(ai.newTarget());
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation);
return astar.FindPath();
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
return astar.FindPath(true);
}