From 3647b10dc546ea19d26788c48afafe8e09abb29b Mon Sep 17 00:00:00 2001 From: BOTLester <58360400+BOTLester@users.noreply.github.com> Date: Sun, 10 May 2020 12:55:13 +0200 Subject: [PATCH] ML fixes --- Game1/Game1.cs | 4 +- Game1/Sources/Crops/CropTypesHolder.cs | 66 +++++++++++------------ Game1/Sources/Crops/Crops.cs | 21 +++++++- Game1/Sources/Crops/SoilProperties.cs | 8 +-- Game1/Sources/ML/Engine.cs | 7 ++- Game1/Sources/Objects/FertilizerHolder.cs | 42 +++++++-------- Game1/Sources/Smart/AI.cs | 27 +++++----- 7 files changed, 98 insertions(+), 77 deletions(-) diff --git a/Game1/Game1.cs b/Game1/Game1.cs index b6ccf57..cbbff67 100644 --- a/Game1/Game1.cs +++ b/Game1/Game1.cs @@ -63,9 +63,7 @@ namespace Game1 cropTypesNames[10] = "Tobacco"; cropTypesNames[11] = "Wheat"; - - - + Engine.init(); diff --git a/Game1/Sources/Crops/CropTypesHolder.cs b/Game1/Sources/Crops/CropTypesHolder.cs index 3a11f83..4312ab6 100644 --- a/Game1/Sources/Crops/CropTypesHolder.cs +++ b/Game1/Sources/Crops/CropTypesHolder.cs @@ -33,9 +33,9 @@ class CropTypesHolder cropTypes[1].Temparature = 30.1f; cropTypes[1].Humidity = 59.0f; cropTypes[1].Moisture = 41.7f; - cropTypes[1].Nitrogen = 35 + 12.6f; - cropTypes[1].Potassium = 20 + 5.3f; - cropTypes[1].Phosphorous = 32 + 26.0f; + cropTypes[1].Nitrogen = 21 + 12.6f; + cropTypes[1].Potassium = 10 + 5.3f; + cropTypes[1].Phosphorous = 20 + 26.0f; // Cotton @@ -50,9 +50,9 @@ class CropTypesHolder cropTypes[2].Temparature = 30.4f; cropTypes[2].Humidity = 59.6f; cropTypes[2].Moisture = 63.2f; - cropTypes[2].Nitrogen = 35 + 16.4f; - cropTypes[2].Potassium = 20 + 3.3f; - cropTypes[2].Phosphorous = 32 + 23.8f; + cropTypes[2].Nitrogen = 21 + 16.4f; + cropTypes[2].Potassium = 10 + 3.3f; + cropTypes[2].Phosphorous = 20 + 23.8f; // Ground Nuts cropTypes[3] = new CropTypes(); @@ -63,9 +63,9 @@ class CropTypesHolder cropTypes[3].Temparature = 30.1f; cropTypes[3].Humidity = 59.1f; cropTypes[3].Moisture = 33.1f; - cropTypes[3].Nitrogen = 35 + 23.3f; - cropTypes[3].Potassium = 20 + 2.0f; - cropTypes[3].Phosphorous = 32 + 21.6f; + cropTypes[3].Nitrogen = 21 + 23.3f; + cropTypes[3].Potassium = 10 + 2.0f; + cropTypes[3].Phosphorous = 20 + 21.6f; // Maize @@ -77,9 +77,9 @@ class CropTypesHolder cropTypes[4].Temparature = 29.0f; cropTypes[4].Humidity = 57.3f; cropTypes[4].Moisture = 42.2f; - cropTypes[4].Nitrogen = 35 + 18.3f; - cropTypes[4].Potassium = 20 + 5.7f; - cropTypes[4].Phosphorous = 32 + 18.7f; + cropTypes[4].Nitrogen = 21 + 18.3f; + cropTypes[4].Potassium = 10 + 5.7f; + cropTypes[4].Phosphorous = 20 + 18.7f; // Millets cropTypes[5] = new CropTypes(); @@ -92,9 +92,9 @@ class CropTypesHolder cropTypes[5].Temparature = 29.5f; cropTypes[5].Humidity = 57.3f; cropTypes[5].Moisture = 38.5f; - cropTypes[5].Nitrogen = 35 + 23.2f; - cropTypes[5].Potassium = 20 + 0.1f; - cropTypes[5].Phosphorous = 32 + 14.4f; + cropTypes[5].Nitrogen = 21 + 23.2f; + cropTypes[5].Potassium = 10 + 0.1f; + cropTypes[5].Phosphorous = 20 + 14.4f; //Oil Seeds cropTypes[6] = new CropTypes(); @@ -105,9 +105,9 @@ class CropTypesHolder cropTypes[6].Temparature = 30.3f; cropTypes[6].Humidity = 59.1f; cropTypes[6].Moisture = 32.1f; - cropTypes[6].Nitrogen = 35 + 19.0f; - cropTypes[6].Potassium = 20 + 2.3f; - cropTypes[6].Phosphorous = 32 + 17.3f; + cropTypes[6].Nitrogen = 21 + 19.0f; + cropTypes[6].Potassium = 10 + 2.3f; + cropTypes[6].Phosphorous = 20 + 17.3f; //Paddys cropTypes[7] = new CropTypes(); @@ -118,9 +118,9 @@ class CropTypesHolder cropTypes[7].Temparature = 31.2f; cropTypes[7].Humidity = 60.4f; cropTypes[7].Moisture = 41.5f; - cropTypes[7].Nitrogen = 35 + 20.8f; - cropTypes[7].Potassium = 20 + 3.7f; - cropTypes[7].Phosphorous = 32 + 16.3f; + cropTypes[7].Nitrogen = 21 + 20.8f; + cropTypes[7].Potassium = 10 + 3.7f; + cropTypes[7].Phosphorous = 20 + 16.3f; //Pulses cropTypes[8] = new CropTypes(); @@ -131,9 +131,9 @@ class CropTypesHolder cropTypes[8].Temparature = 27.8f; cropTypes[8].Humidity = 54.9f; cropTypes[8].Moisture = 34.1f; - cropTypes[8].Nitrogen = 35 + 18.4f; - cropTypes[8].Potassium = 20 + 4.2f; - cropTypes[8].Phosphorous = 32 + 17.5f; + cropTypes[8].Nitrogen = 21 + 18.4f; + cropTypes[8].Potassium = 10 + 4.2f; + cropTypes[8].Phosphorous = 20 + 17.5f; //Sugarcane cropTypes[9] = new CropTypes(); @@ -146,9 +146,9 @@ class CropTypesHolder cropTypes[9].Temparature = 30.0f; cropTypes[9].Humidity = 58.6f; cropTypes[9].Moisture = 51.2f; - cropTypes[9].Nitrogen = 35 + 14.6f; - cropTypes[9].Potassium = 20 + 4.2f; - cropTypes[9].Phosphorous = 32 + 17.6f; + cropTypes[9].Nitrogen = 21 + 14.6f; + cropTypes[9].Potassium = 10 + 4.2f; + cropTypes[9].Phosphorous = 20 + 17.6f; //Tobacco @@ -160,9 +160,9 @@ class CropTypesHolder cropTypes[10].Temparature = 31.9f; cropTypes[10].Humidity = 62.4f; cropTypes[10].Moisture = 31.6f; - cropTypes[10].Nitrogen = 35 + 19.1f; - cropTypes[10].Potassium = 20 + 4.9f; - cropTypes[10].Phosphorous = 32 + 19.3f; + cropTypes[10].Nitrogen = 21 + 19.1f; + cropTypes[10].Potassium = 10 + 4.9f; + cropTypes[10].Phosphorous = 20 + 19.3f; //Wheat @@ -174,9 +174,9 @@ class CropTypesHolder cropTypes[11].Temparature = 33.1f; cropTypes[11].Humidity = 63.8f; cropTypes[11].Moisture = 50.0f; - cropTypes[11].Nitrogen = 35 + 23.3f; - cropTypes[11].Potassium = 20 + 2.9f; - cropTypes[11].Phosphorous = 32 + 14.4f; + cropTypes[11].Nitrogen = 21 + 23.3f; + cropTypes[11].Potassium = 10 + 2.9f; + cropTypes[11].Phosphorous = 20 + 14.4f; } diff --git a/Game1/Sources/Crops/Crops.cs b/Game1/Sources/Crops/Crops.cs index b959550..4e0f21c 100644 --- a/Game1/Sources/Crops/Crops.cs +++ b/Game1/Sources/Crops/Crops.cs @@ -55,13 +55,30 @@ class Crops soilProperties.Nitrogen += fertilizer.Nitrogen; soilProperties.Phosphorous += fertilizer.Phosphorus; soilProperties.Potassium += fertilizer.Potassium; + + if (soilProperties.Nitrogen > 42) + soilProperties.Nitrogen = 42; + if (soilProperties.Phosphorous > 42) + soilProperties.Phosphorous = 42; + if (soilProperties.Potassium > 19) + soilProperties.Potassium = 19; + } public bool isSaturated() { - if (soilProperties.Nitrogen > 52 && (soilProperties.Phosphorous > 57 || soilProperties.Potassium > 25)) + if (soilProperties.Nitrogen > 40 && (soilProperties.Phosphorous > 40 || soilProperties.Potassium > 17)) return true; - if (soilProperties.Phosphorous > 57 && soilProperties.Potassium > 25) + if (soilProperties.Phosphorous > 40 && soilProperties.Potassium > 17) + return true; + return false; + } + + public bool isSaturated(int threshold) + { + if (soilProperties.Nitrogen > 40 - threshold && (soilProperties.Phosphorous > 40 - threshold || soilProperties.Potassium > 17 - (threshold * 17/40))) + return true; + if (soilProperties.Phosphorous > 40 - threshold && soilProperties.Potassium > 17 - (threshold *17/40)) return true; return false; } diff --git a/Game1/Sources/Crops/SoilProperties.cs b/Game1/Sources/Crops/SoilProperties.cs index f79219b..c1b7558 100644 --- a/Game1/Sources/Crops/SoilProperties.cs +++ b/Game1/Sources/Crops/SoilProperties.cs @@ -20,7 +20,7 @@ class SoilProperties 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 int Capacity = 150; + public int Capacity = 80; public void setSoilProperties() { @@ -48,9 +48,9 @@ class SoilProperties Temperature = GetRandomNumber(22, 30); Humidity = Temperature * GetRandomNumber(1.9, 2.1); Moisture = GetRandomNumber(20, 70); - Nitrogen = GetRandomNumber(4 , 55); - Potassium = GetRandomNumber(0.01f, 28); - Phosphorous = GetRandomNumber(0.01f, 60); + Nitrogen = GetRandomNumber(4 , 42); //was 4, 60 + Potassium = GetRandomNumber(0.01f, 19); // was 0, 28 + Phosphorous = GetRandomNumber(0.01f, 42); // was 0, 60 } public float GetRandomNumber(double minimum, double maximum) diff --git a/Game1/Sources/ML/Engine.cs b/Game1/Sources/ML/Engine.cs index 9f123f1..cb3c6ff 100644 --- a/Game1/Sources/ML/Engine.cs +++ b/Game1/Sources/ML/Engine.cs @@ -9,7 +9,12 @@ using Microsoft.ML; static class Engine { private static MLContext mlContext = new MLContext(seed: 1); - private static PredictionEngine PredictionEngine = MLModel.CreateEngine(); + private static PredictionEngine PredictionEngine; + + public static void init() + { + PredictionEngine = MLModel.CreateEngine(); + } public static string PredictFertilizer(Crops crop, CropTypes cropTypes) { diff --git a/Game1/Sources/Objects/FertilizerHolder.cs b/Game1/Sources/Objects/FertilizerHolder.cs index 828e13b..696fffd 100644 --- a/Game1/Sources/Objects/FertilizerHolder.cs +++ b/Game1/Sources/Objects/FertilizerHolder.cs @@ -98,63 +98,63 @@ class FertilizerHolder { ID = 0, Name = "10-26-26", - Nitrogen = 26.0f / 5, - Phosphorus = 30 * 0.436f / 5, - Potassium = 6 * 0.83f / 5 + Nitrogen = 17.21f / 5, + Phosphorus = 12.14f / 5, + Potassium = 0.64f / 5 }; FertilizerType[2] = new Fertilizer { ID = 1, Name = "14-35-14", - Nitrogen = 24.0f / 5, - Phosphorus = 17 * 0.436f / 5, - Potassium = 22 * 0.83f / 5 + Nitrogen = 16.89f / 5, + Phosphorus = 6.21f / 5, + Potassium = 5.21f / 5 }; FertilizerType[3] = new Fertilizer { ID = 2, Name = "17-17-17", - Nitrogen = 17.0f / 5, - Phosphorus = 17 * 0.436f / 5, - Potassium = 17 * 0.83f / 5 + Nitrogen = 14.92f / 5, + Phosphorus = 14.42f / 5, + Potassium = 3.0f / 5 }; FertilizerType[4] = new Fertilizer { ID = 3, Name = "20-20", - Nitrogen = 10.0f / 5, - Phosphorus = 10 * 0.436f / 5, - Potassium = 20 * 0.83f / 5 + Nitrogen = 15.39f / 5, + Phosphorus = 15.21f / 5, + Potassium = 9.5f / 5 }; FertilizerType[5] = new Fertilizer { ID = 4, Name = "28-28", - Nitrogen = 14.0f / 5, - Phosphorus = 14 * 0.436f / 5, - Potassium = 28 * 0.83f / 5 + Nitrogen = 9.67f / 5, + Phosphorus = 10.47f / 5, + Potassium = 9.5f / 5 }; FertilizerType[6] = new Fertilizer { ID = 5, Name = "DAP", - Nitrogen = 12.0f / 5, - Phosphorus = 18 * 0.436f / 5, - Potassium = 34 * 0.83f / 5 + Nitrogen = 14.52f / 5, + Phosphorus = 1.77f / 5, + Potassium = 9.5f / 5 }; FertilizerType[7] = new Fertilizer { ID = 6, Name = "Urea", - Nitrogen = 0.0f / 5, - Phosphorus = 23 * 0.436f / 5, - Potassium = 23 * 0.83f / 5 + Nitrogen = 1.81f / 5, + Phosphorus = 21.0f / 5, + Potassium = 9.5f / 5 }; } diff --git a/Game1/Sources/Smart/AI.cs b/Game1/Sources/Smart/AI.cs index b727887..db58e07 100644 --- a/Game1/Sources/Smart/AI.cs +++ b/Game1/Sources/Smart/AI.cs @@ -63,7 +63,7 @@ class AI { if (farm.getCrop(x, y).getStatus() >= 2 && tractorPos != new Vector2(x, y)) { - if (farm.getCrop(x, y).isSaturated() || !farm.getCrop(x, y).belowCapacity()) + if (farm.getCrop(x, y).isSaturated(5) || !farm.getCrop(x, y).belowCapacity()) { //do nothing } @@ -93,17 +93,18 @@ class AI int y = (int)tractorPos.Y; Fertilizer fertilizer = new Fertilizer(); fertilizerHolder.init(); + if (farm.getCrop(x, y).getStatus() >= 2) + { + fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType()))); + while (!(farm.getCrop(x, y).isSaturated()) && farm.getCrop(x, y).belowCapacity() && inventory.useItem(fertilizerHolder.GetFertilizerID(fertilizer.Name), 0)) + { + farm.getCrop(x, y).Fertilize(fertilizer); + fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType()))); + System.Threading.Thread.Sleep(5); + } + } if (farm.getCrop(x, y).getCropTimer() == 1) { - if (farm.getCrop(x, y).getStatus() == 2) - { - fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType()))); - while (!(farm.getCrop(x, y).isSaturated()) && farm.getCrop(x, y).belowCapacity() && inventory.useItem(fertilizerHolder.GetFertilizerID(fertilizer.Name), 0)) - { - farm.getCrop(x, y).Fertilize(fertilizer); - fertilizer = fertilizerHolder.GetFertilizer(Engine.PredictFertilizer(farm.getCrop(x, y), farm.getPresetCropTypes(farm.getCrop(x, y).getCropType()))); - } - } farm.setCropStatus(tractorPos.X, tractorPos.Y); if (farm.getCrop(x, y).getStatus() == 2) { @@ -135,20 +136,20 @@ class AI CropTypes avgHold = holder.getPresetCropTypes(cropType); if (crop.getStatus() == 2) - statusScore = 25; + statusScore = 50; else if (crop.getStatus() == 3) statusScore = -100; else statusScore = 1; if (crop.getCropTimer() == 1) - timerScore = 999; + timerScore = 100; else statusScore = 0; score = (int)(crop.getProductionRate(avgHold) * 10); - if (!crop.isSaturated()) + if (!crop.isSaturated(2)) saturationScore = 5; else {