Some cleaning, error corrections

This commit is contained in:
BOTLester 2020-05-09 15:54:53 +02:00
parent e1b4dec764
commit 3566a7af2e
8 changed files with 127 additions and 187 deletions

View File

@ -102,7 +102,6 @@
<Compile Include="Sources\Pathing\A-Star\PathSaver\MinHeap.cs" />
<Compile Include="Sources\Pathing\A-Star\PathSaver\Nodes.cs" />
<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" />

View File

@ -31,9 +31,9 @@ class CropTypesHolder
cropTypes[1].Temparature = 30.1f;
cropTypes[1].Humidity = 59.0f;
cropTypes[1].Moisture = 41.7f;
cropTypes[1].Nitrogen = 12.6f;
cropTypes[1].Potassium = 5.3f;
cropTypes[1].Phosphorous = 26.0f;
cropTypes[1].Nitrogen = 25 + 12.6f;
cropTypes[1].Potassium = 14 + 5.3f;
cropTypes[1].Phosphorous = 30 + 26.0f;
// Cotton
@ -47,9 +47,9 @@ class CropTypesHolder
cropTypes[2].Temparature = 30.4f;
cropTypes[2].Humidity = 59.6f;
cropTypes[2].Moisture = 63.2f;
cropTypes[2].Nitrogen = 16.4f;
cropTypes[2].Potassium = 3.3f;
cropTypes[2].Phosphorous = 23.8f;
cropTypes[2].Nitrogen = 25 + 16.4f;
cropTypes[2].Potassium = 14 + 3.3f;
cropTypes[2].Phosphorous = 30 + 23.8f;
// Ground Nuts
cropTypes[3] = new CropTypes();
@ -59,9 +59,9 @@ class CropTypesHolder
cropTypes[3].Temparature = 30.1f;
cropTypes[3].Humidity = 59.1f;
cropTypes[3].Moisture = 33.1f;
cropTypes[3].Nitrogen = 23.3f;
cropTypes[3].Potassium = 2.0f;
cropTypes[3].Phosphorous = 21.6f;
cropTypes[3].Nitrogen = 25 + 23.3f;
cropTypes[3].Potassium = 14 + 2.0f;
cropTypes[3].Phosphorous = 30 + 21.6f;
// Maize
@ -72,9 +72,9 @@ class CropTypesHolder
cropTypes[4].Temparature = 29.0f;
cropTypes[4].Humidity = 57.3f;
cropTypes[4].Moisture = 42.2f;
cropTypes[4].Nitrogen = 18.3f;
cropTypes[4].Potassium = 5.7f;
cropTypes[4].Phosphorous = 18.7f;
cropTypes[4].Nitrogen = 25 + 18.3f;
cropTypes[4].Potassium = 14 + 5.7f;
cropTypes[4].Phosphorous = 30 + 18.7f;
// Millets
cropTypes[5] = new CropTypes();
@ -86,9 +86,9 @@ class CropTypesHolder
cropTypes[5].Temparature = 29.5f;
cropTypes[5].Humidity = 57.3f;
cropTypes[5].Moisture = 38.5f;
cropTypes[5].Nitrogen = 23.2f;
cropTypes[5].Potassium = 0.1f;
cropTypes[5].Phosphorous = 14.4f;
cropTypes[5].Nitrogen = 25 + 23.2f;
cropTypes[5].Potassium = 14 + 0.1f;
cropTypes[5].Phosphorous = 30 + 14.4f;
//Oil Seeds
cropTypes[6] = new CropTypes();
@ -98,9 +98,9 @@ class CropTypesHolder
cropTypes[6].Temparature = 30.3f;
cropTypes[6].Humidity = 59.1f;
cropTypes[6].Moisture = 32.1f;
cropTypes[6].Nitrogen = 19.0f;
cropTypes[6].Potassium = 2.3f;
cropTypes[6].Phosphorous = 17.3f;
cropTypes[6].Nitrogen = 25 + 19.0f;
cropTypes[6].Potassium = 14 + 2.3f;
cropTypes[6].Phosphorous = 30 + 17.3f;
//Paddys
cropTypes[7] = new CropTypes();
@ -110,9 +110,9 @@ class CropTypesHolder
cropTypes[7].Temparature = 31.2f;
cropTypes[7].Humidity = 60.4f;
cropTypes[7].Moisture = 41.5f;
cropTypes[7].Nitrogen = 20.8f;
cropTypes[7].Potassium = 3.7f;
cropTypes[7].Phosphorous = 16.3f;
cropTypes[7].Nitrogen = 25 + 20.8f;
cropTypes[7].Potassium = 14 + 3.7f;
cropTypes[7].Phosphorous = 30 + 16.3f;
//Pulses
cropTypes[8] = new CropTypes();
@ -122,9 +122,9 @@ class CropTypesHolder
cropTypes[8].Temparature = 27.8f;
cropTypes[8].Humidity = 54.9f;
cropTypes[8].Moisture = 34.1f;
cropTypes[8].Nitrogen = 18.4f;
cropTypes[8].Potassium = 4.2f;
cropTypes[8].Phosphorous = 17.5f;
cropTypes[8].Nitrogen = 25 + 18.4f;
cropTypes[8].Potassium = 14 + 4.2f;
cropTypes[8].Phosphorous = 30 + 17.5f;
//Sugarcane
cropTypes[9] = new CropTypes();
@ -136,9 +136,9 @@ class CropTypesHolder
cropTypes[9].Temparature = 30.0f;
cropTypes[9].Humidity = 58.6f;
cropTypes[9].Moisture = 51.2f;
cropTypes[9].Nitrogen = 14.6f;
cropTypes[9].Potassium = 4.2f;
cropTypes[9].Phosphorous = 17.6f;
cropTypes[9].Nitrogen = 25 + 14.6f;
cropTypes[9].Potassium = 14 + 4.2f;
cropTypes[9].Phosphorous = 30 + 17.6f;
//Tobacco
@ -149,9 +149,9 @@ class CropTypesHolder
cropTypes[10].Temparature = 31.9f;
cropTypes[10].Humidity = 62.4f;
cropTypes[10].Moisture = 31.6f;
cropTypes[10].Nitrogen = 19.1f;
cropTypes[10].Potassium = 4.9f;
cropTypes[10].Phosphorous = 19.3f;
cropTypes[10].Nitrogen = 25 + 19.1f;
cropTypes[10].Potassium = 14 + 4.9f;
cropTypes[10].Phosphorous = 30 + 19.3f;
//Wheat
@ -162,9 +162,9 @@ class CropTypesHolder
cropTypes[11].Temparature = 33.1f;
cropTypes[11].Humidity = 63.8f;
cropTypes[11].Moisture = 50.0f;
cropTypes[11].Nitrogen = 23.3f;
cropTypes[11].Potassium = 2.9f;
cropTypes[11].Phosphorous = 14.4f;
cropTypes[11].Nitrogen = 25 + 23.3f;
cropTypes[11].Potassium = 14 + 2.9f;
cropTypes[11].Phosphorous = 30 + 14.4f;
}

View File

@ -197,19 +197,22 @@ class Crops
public float getProductionRate(CropTypes Sample)
{
ProductionRate = 1;
float min = 1.0f;
if (DataSet != null)
{
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Temperature, Sample.Temparature));
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Moisture, Sample.Moisture));
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Humidity, Sample.Humidity));
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Phosphorous, Sample.Phosphorous));
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Potassium, Sample.Potassium));
ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Nitrogen, Sample.Nitrogen));
min = Math.Min(compareToDatset(soilProperties.Phosphorous, Sample.Phosphorous), Math.Min(compareToDatset(soilProperties.Potassium, Sample.Potassium), compareToDatset(soilProperties.Nitrogen, Sample.Nitrogen)));
ProductionRate = ProductionRate + (ProductionRate * min);
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Phosphorous, Sample.Phosphorous));
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Potassium, Sample.Potassium));
//ProductionRate = ProductionRate + (ProductionRate * compareToDatset(soilProperties.Nitrogen, Sample.Nitrogen));
if (ProductionRate < 0)
{
ProductionRate = 0;
}
ProductionRate = ProductionRate / 20;
ProductionRate = ProductionRate / 10;
}
return ProductionRate;
}

View File

@ -17,9 +17,9 @@ class SoilProperties
public float Nitrogen;
public float Potassium;
public float Phosphorous;
public float NitrogenDegradeRate = 0.01f;
public float PotassiumDegradeRate = 0.01f;
public float PhosphorousDegradeRate = 0.01f;
public float NitrogenDegradeRate = 1.0f - (1.0f/55 * 40);
public float PotassiumDegradeRate = 1.0f - (1.0f/28 * 23);
public float PhosphorousDegradeRate = 1.0f - (1.0f/60 * 37);
public void setSoilProperties()
{

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using C5;
using System.Diagnostics;
class Astar
{
@ -12,7 +14,6 @@ class Astar
private Vector2 housePos;
private static Crops[,] crops;
private Vector2 Size;
private PriorityQueue allPaths;
private Vector2 targetPos;
private int Rotation;
@ -35,11 +36,6 @@ class Astar
targetPos = newTargetPos;
}
public Nodes getOptimalPath()
{
return allPaths.Peek();
}
// Get all adjacent nodes
public List<Nodes> GetAdjacentNodes(Vector2 currentPos)
{
@ -79,9 +75,9 @@ class Astar
if (currDir == newDir)
return 0;
else if (Math.Abs(currDir - newDir) == 1 || Math.Abs(currDir - newDir) == 3)
return 2;
return 20;
else if (Math.Abs(currDir - newDir) == 0 || Math.Abs(currDir - newDir) == 2)
return 9;
return 900;
return 0;
}
@ -89,13 +85,13 @@ class Astar
public int ConvertRotation()
{
int rotation = 0;
if (Rotation == 180)
if (Rotation > 135 && Rotation < 225)
rotation = 0;
else if (Rotation == 270)
else if (Rotation > 225 && Rotation < 315)
rotation = 1;
else if (Rotation == 0)
else if (Rotation > 315 && Rotation < 45)
rotation = 2;
else if (Rotation == 90)
else if (Rotation > 45 && Rotation < 135)
rotation = -1;
return rotation;
}
@ -132,7 +128,6 @@ class Astar
g = current.getG() + crops[(int)adjacentNode.getCords().X, (int)adjacentNode.getCords().Y].getCostOnMovement() + CalculateRotationCost(direction, adjacentNode.getDirection()); // calculate g - cost from start point
if (!(openList.Exists(adjacentNode.getCords()))) // if adjacent node is not on open list, add it
{
adjacentNode.setG(g);
adjacentNode.setH(ComputeHScore(adjacentNode.getCords(), target.getCords()));
adjacentNode.calculateF();
@ -164,6 +159,7 @@ class Astar
openList.deleteHeap();
closedList.deleteHeap();
return path;
}

View File

@ -6,8 +6,9 @@ using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using C5;
class Nodes
class Nodes : IComparable<Nodes>, IEqualityComparer<Nodes>, IPriorityQueueHandle<Nodes>
{
private int F = 0;
private int G = 0;
@ -27,6 +28,12 @@ class Nodes
Direction = direction;
}
public Nodes(int f, Vector2 coordinates)
{
Coordinates = coordinates;
F = f;
}
public Nodes(Nodes node)
{
F = node.F;
@ -94,4 +101,24 @@ class Nodes
{
return Direction;
}
public int CompareTo(Nodes other)
{
if (this.F < other.F)
return -1;
else if (this.F > other.F)
return 1;
else
return 0;
}
public bool Equals(Nodes source, Nodes other)
{
return (source.Coordinates.X == other.Coordinates.X && source.Coordinates.Y == other.Coordinates.Y);
}
public int GetHashCode(Nodes nodes)
{
return (nodes.Coordinates.GetHashCode());
}
}

View File

@ -1,88 +0,0 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
class PriorityQueue
{
public List<Nodes> list;
public int Count { get { return list.Count; } }
public PriorityQueue()
{
list = new List<Nodes>();
}
public PriorityQueue(int count)
{
list = new List<Nodes>(count);
}
public void Enqueue(Nodes x)
{
list.Add(x);
int i = Count - 1;
while (i > 0)
{
int p = (i - 1) / 2;
if (list[p].getF() <= x.getF()) break;
list[i] = list[p];
i = p;
}
if (Count > 0) list[i] = x;
}
public void Dequeue()
{
Nodes min = Peek();
Nodes root = list[Count - 1];
list.RemoveAt(Count - 1);
int i = 0;
while (i * 2 + 1 < Count)
{
int a = i * 2 + 1;
int b = i * 2 + 2;
int c = b < Count && list[b].getF() < list[a].getF() ? b : a;
if (list[c].getF() >= root.getF()) break;
list[i] = list[c];
i = c;
}
if (Count > 0) list[i] = root;
}
public Nodes Peek()
{
if (Count == 0) throw new InvalidOperationException("Queue is empty.");
return list[0];
}
public Boolean Exists(Vector2 coordinates)
{
if (Count == 0)
return false;
foreach(Nodes node in list)
{
if (node.getCords() == coordinates)
return true;
}
return false;
}
public void Clear()
{
list.Clear();
}
}

View File

@ -16,7 +16,6 @@ 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();
@ -69,28 +68,16 @@ class AI
{
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);
score = calculateSoilScore(x, y);
queue.AddToQueue(x, y, score);
if (farm.getCrop(x, y).getCropTimer() == 1 || farm.getCrop(x, y).getStatus() == 2)
count++;
}
}
}
if (count > 0)
break;
else if (tractorPos != housePos)
return housePos;
return newTarget = GetMaxFNode(testsize, queue);
/*
else if (tractorPos == housePos)
@ -100,8 +87,8 @@ class AI
}
*/
}
newTarget = GetMinFNode(Math.Min(testsize, count), queue);
return newTarget;
}
@ -123,6 +110,7 @@ class AI
{
int score = 0;
int statusScore = 0;
int timerScore = 0;
int aproxDistance = (int)(Math.Abs(x - tractorPos.X) + Math.Abs(y - tractorPos.Y));
CropTypesHolder holder = new CropTypesHolder();
holder.init();
@ -135,36 +123,18 @@ class AI
if (crop.getStatus() == 2)
statusScore = 25;
else if (crop.getStatus() == 3)
statusScore = 5;
else if (crop.getStatus() == 4)
statusScore = -10;
statusScore = -100;
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);
if (crop.getCropTimer() == 1)
timerScore = 999;
else
statusScore = 0;
for (int i = 0; i < normVal.Count(); i++)
{
score = score + (int)Math.Round(System.Convert.ToDouble(10*(1 - Math.Abs(normAvg[i] - normVal[i]))));
}
score = (int)(crop.getProductionRate(avgHold) * 10);
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;
return score + (-aproxDistance * 5) + statusScore + timerScore;
}
private float norm(float min, float max, float val)
@ -175,7 +145,7 @@ class AI
public Vector2 GetMinFNode(int testSize, PriorityQueueC5 queue)
{
int index = 0;
int min = 999;
int min = 9999;
Path path = new Path();
List<PQEntry> entryList = new List<PQEntry>();
for (int i = 0; i < testSize; i++)
@ -203,4 +173,37 @@ class AI
return minEntry.Coordinates;
}
public Vector2 GetMaxFNode(int testSize, PriorityQueueC5 queue)
{
int index = 0;
int max = -9999;
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 (max < tempF.getF())
{
max = 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;
}
}