1
0
forked from s425077/PotatoPlan

Target Selection

This commit is contained in:
BOTLester 2020-05-07 22:09:45 +02:00
parent 2e9b78f1a0
commit 7a35bb09fe
7 changed files with 245 additions and 14 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" />
@ -86,6 +104,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" />
@ -118,6 +138,9 @@
<None Include="app.manifest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="C5">
<Version>2.5.3</Version>
</PackageReference>
<PackageReference Include="LightGBM">
<Version>2.3.1</Version>
</PackageReference>
@ -155,6 +178,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

@ -47,6 +47,11 @@ class Crops
soilProperties.setSoilProperties();
}
public SoilProperties getSoilProperties()
{
return soilProperties;
}
// Changes the time required for the crops to be harvestable

View File

@ -10,22 +10,31 @@ 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();
@ -92,7 +101,7 @@ class Astar
}
// Main function of A* algorithm
public Path FindPath()
public Path FindPath(bool flipArray)
{
int g = 0;
int direction = ConvertRotation();
@ -149,6 +158,8 @@ class Astar
path.AddNode(current);
current = current.getParent();
}
if (flipArray)
path = path.FlipArray();
openList.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();
@ -40,8 +38,6 @@ class AI
}
public void update(Farm newFarm, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
{
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
@ -50,6 +46,7 @@ class AI
farm = newFarm;
Size = newSize;
Rotation = rotation;
astar.update(farm.getCrops(), Size, tractorPos, housePos, rotation);
}
@ -61,7 +58,28 @@ 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;
Vector2 newTarget;
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() == 4 && farm.getCrop(x, y).getCropTimer() != 1)
{
//do nothing
}
else
{
score = calculateSoilScore(x, y);
queue.AddToQueue(x, y, score);
}
}
}
newTarget = GetMinFNode(5, queue);
return newTarget;
}
public Farm changeCropStatus()
@ -77,4 +95,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

@ -18,10 +18,12 @@ class SmartTractor
{
ai.update(farm, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
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();
return astar.FindPath(true);
}