forked from s425077/PotatoPlan
Merge branch 'Joel-ML' of https://git.wmi.amu.edu.pl/s425077/PotatoPlan into Joel-ML
This commit is contained in:
commit
10e58f984f
BIN
Game1/Content/ML/MLmodel_Joel_area
Normal file
BIN
Game1/Content/ML/MLmodel_Joel_area
Normal file
Binary file not shown.
1048576
Game1/Content/ML/Rainfall_area.csv
Normal file
1048576
Game1/Content/ML/Rainfall_area.csv
Normal file
File diff suppressed because it is too large
Load Diff
2
Game1/Content/ML/report_Joel_area
Normal file
2
Game1/Content/ML/report_Joel_area
Normal file
@ -0,0 +1,2 @@
|
||||
Mean Absolute Error: 187.060835104336
|
||||
R Squared: 0.913526230109177
|
@ -75,7 +75,9 @@ namespace Game1
|
||||
cropTypesNames[11] = "Wheat";
|
||||
|
||||
Engine.init();
|
||||
Sources.ML_Joel.Engine.initArea();
|
||||
//Sources.ML_Joel.Engine.CreateModel();
|
||||
//Sources.ML_Joel.Engine.CreateModelArea();
|
||||
|
||||
|
||||
|
||||
|
@ -90,7 +90,9 @@
|
||||
<Compile Include="Sources\Crops\PerlinNoise.cs" />
|
||||
<Compile Include="Sources\Crops\SoilProperties.cs" />
|
||||
<Compile Include="Sources\ML\Engine.cs" />
|
||||
<Compile Include="Sources\ML_Joel\DataModel\InputArea.cs" />
|
||||
<Compile Include="Sources\ML_Joel\DataModel\Input.cs" />
|
||||
<Compile Include="Sources\ML_Joel\DataModel\OutputArea.cs" />
|
||||
<Compile Include="Sources\ML_Joel\DataModel\Output.cs" />
|
||||
<Compile Include="Sources\ML_Joel\Engine.cs" />
|
||||
<Compile Include="Sources\ML_Joel\Model.cs" />
|
||||
|
@ -11,18 +11,24 @@ using Microsoft.ML.Trainers.LightGbm;
|
||||
class MLModel
|
||||
{
|
||||
private static MLContext mlContext = new MLContext(seed: 1);
|
||||
/*
|
||||
private static string path = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/Fertilizer_Prediction.csv";
|
||||
private static string modelpath = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodel";
|
||||
private static string report = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report";
|
||||
*/
|
||||
private static string pathBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/BigFertPredict.csv";
|
||||
private static string modelpathBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodelBig";
|
||||
private static string reportBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report_BigModel";
|
||||
|
||||
private static string path = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/Fertilizer_Prediction.csv";
|
||||
private static string modelpath = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/MLmodel";
|
||||
private static string report = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/report";
|
||||
|
||||
private static string path = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/Fertilizer_Prediction.csv";
|
||||
private static string modelpath = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodel";
|
||||
private static string report = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report";
|
||||
private static string pathBig = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/BigFertPredict.csv";
|
||||
private static string modelpathBig = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/MLmodelBig";
|
||||
private static string reportBig = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/report_BigModel";
|
||||
|
||||
/*
|
||||
private static string pathBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/BigFertPredict.csv";
|
||||
private static string modelpathBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodelBig";
|
||||
private static string reportBig = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report_BigModel";
|
||||
|
||||
private static string path = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/Fertilizer_Prediction.csv";
|
||||
private static string modelpath = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodel";
|
||||
private static string report = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report";
|
||||
*/
|
||||
|
||||
// Loading data, creatin and saving ML model for smaller dataset (100)
|
||||
public static void CreateModel()
|
||||
|
27
Game1/Sources/ML_Joel/DataModel/InputArea.cs
Normal file
27
Game1/Sources/ML_Joel/DataModel/InputArea.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.ML.Data;
|
||||
|
||||
namespace Game1.Sources.ML_Joel
|
||||
{
|
||||
class InputArea
|
||||
{
|
||||
[ColumnName("Season"), LoadColumn(0)]
|
||||
public String Season { get; set; }
|
||||
|
||||
[ColumnName("Crop"), LoadColumn(1)]
|
||||
public String Crop { get; set; }
|
||||
|
||||
[ColumnName("Area"), LoadColumn(2)]
|
||||
public float Area { get; set; }
|
||||
|
||||
[ColumnName("Rainfall"), LoadColumn(3)]
|
||||
public float Rainfall { get; set; }
|
||||
|
||||
[ColumnName("Production"), LoadColumn(4)]
|
||||
public float Production { get; set; }
|
||||
}
|
||||
}
|
20
Game1/Sources/ML_Joel/DataModel/OutputArea.cs
Normal file
20
Game1/Sources/ML_Joel/DataModel/OutputArea.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.ML.Data;
|
||||
|
||||
namespace Game1.Sources.ML_Joel
|
||||
{
|
||||
class OutputArea
|
||||
{
|
||||
//[ColumnName("PredictedLabel")]
|
||||
public float Prediction { get; set; }
|
||||
public float Score { get; set; }
|
||||
|
||||
//[ColumnName("Score")]
|
||||
// public float[] Score { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -11,33 +11,40 @@ namespace Game1.Sources.ML_Joel
|
||||
static class Engine
|
||||
{
|
||||
private static MLContext mlContext = new MLContext(seed: 1);
|
||||
private static PredictionEngine<ModelInput, ModelOutput> PredictionEngine;
|
||||
private static PredictionEngine<Input, Output> PredictionEngine;
|
||||
private static PredictionEngine<InputArea, OutputArea> PredictionEngineArea;
|
||||
|
||||
|
||||
public static void CreateModel()
|
||||
{
|
||||
Model.CreateModel();
|
||||
}
|
||||
|
||||
public static void init()
|
||||
public static void CreateModelArea()
|
||||
{
|
||||
PredictionEngine = MLModel.CreateEngine();
|
||||
Model.CreateModelArea();
|
||||
}
|
||||
|
||||
public static string PredictFertilizer(Crops crop, CropTypes cropTypes)
|
||||
public static void init()
|
||||
{
|
||||
ModelInput modelInput = new ModelInput
|
||||
PredictionEngine = Model.CreateEngine();
|
||||
}
|
||||
|
||||
public static void initArea()
|
||||
{
|
||||
PredictionEngineArea = Model.CreateEngineArea();
|
||||
}
|
||||
|
||||
public static float PredictProductionwithRainfall(Crops crop, CropTypes cropTypes)
|
||||
{
|
||||
InputArea modelInput = new InputArea
|
||||
{
|
||||
Temperature = crop.getSoilProperties().Temperature,
|
||||
Humidity = crop.getSoilProperties().Humidity,
|
||||
Moisture = crop.getSoilProperties().Moisture,
|
||||
Soil_Type = crop.getSoilProperties().soilType,
|
||||
Crop_Type = cropTypes.CropName,
|
||||
Nitrogen = crop.getSoilProperties().Nitrogen,
|
||||
Potassium = crop.getSoilProperties().Potassium,
|
||||
Phosporous = crop.getSoilProperties().Phosphorous
|
||||
//Season = ,
|
||||
//Area = crop.getSoilProperties().Area,
|
||||
Rainfall = crop.getSoilProperties().Humidity,
|
||||
};
|
||||
|
||||
return PredictionEngine.Predict(modelInput).Prediction;
|
||||
return PredictionEngineArea.Predict(modelInput).Score;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,10 @@ namespace Game1.Sources.ML_Joel
|
||||
private static string modelpath = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/MLmodel_Joel";
|
||||
private static string report = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/report_Joel";
|
||||
|
||||
private static string path_area = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/Rainfall_area.csv";
|
||||
private static string modelpath_area = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/MLmodel_Joel_area";
|
||||
private static string report_area = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/report_Joel_area";
|
||||
|
||||
// Loading data, creatin and saving ML model for smaller dataset (100)
|
||||
public static void CreateModel()
|
||||
{
|
||||
@ -75,6 +79,57 @@ namespace Game1.Sources.ML_Joel
|
||||
return MLModel;
|
||||
}
|
||||
|
||||
public static void CreateModelArea()
|
||||
{
|
||||
|
||||
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<InputArea>(
|
||||
path: path_area,
|
||||
hasHeader: true,
|
||||
separatorChar: ',',
|
||||
allowQuoting: true,
|
||||
allowSparse: false);
|
||||
|
||||
var splitData = mlContext.Data.TrainTestSplit(trainingDataView, testFraction: 0.2);
|
||||
trainingDataView = splitData.TrainSet;
|
||||
IDataView testDataView = splitData.TestSet;
|
||||
ITransformer MLModel = BuildAndTrainArea(mlContext, trainingDataView, testDataView, report_area);
|
||||
SaveModel(mlContext, MLModel, modelpath_area, trainingDataView.Schema);
|
||||
}
|
||||
|
||||
// Building and training ML model
|
||||
public static ITransformer BuildAndTrainArea(MLContext mLContext, IDataView trainingDataView, IDataView testDataView, string reportPath)
|
||||
{
|
||||
|
||||
var options = new LightGbmRegressionTrainer.Options
|
||||
{
|
||||
MaximumBinCountPerFeature = 40,
|
||||
LearningRate = 0.00020,
|
||||
NumberOfIterations = 20000,
|
||||
NumberOfLeaves = 55,
|
||||
LabelColumnName = "Production",
|
||||
FeatureColumnName = "Features",
|
||||
|
||||
Booster = new DartBooster.Options()
|
||||
{
|
||||
MaximumTreeDepth = 10
|
||||
}
|
||||
};
|
||||
|
||||
var pipeline = mlContext.Transforms
|
||||
.Text.FeaturizeText("SeasonF", "Season")
|
||||
.Append(mlContext.Transforms.Text.FeaturizeText("CropF", "Crop"))
|
||||
.Append(mlContext.Transforms.Concatenate("Features", "SeasonF", "CropF", "Area", "Rainfall"))
|
||||
.AppendCacheCheckpoint(mLContext)
|
||||
.Append(mLContext.Regression.Trainers.LightGbm(options));
|
||||
|
||||
ITransformer MLModel = pipeline.Fit(trainingDataView);
|
||||
var testEval = MLModel.Transform(testDataView);
|
||||
|
||||
Evaluate(mlContext, testEval, pipeline, 10, reportPath, "Production");
|
||||
|
||||
return MLModel;
|
||||
}
|
||||
|
||||
public static ITransformer TrainModel(MLContext mlContext, IDataView trainingDataView, IEstimator<ITransformer> trainingPipeline)
|
||||
{
|
||||
ITransformer model = trainingPipeline.Fit(trainingDataView);
|
||||
@ -98,16 +153,27 @@ namespace Game1.Sources.ML_Joel
|
||||
mlContext.Model.Save(Model, modelInputSchema, modelPath);
|
||||
}
|
||||
|
||||
public static ITransformer LoadModel(bool isBig)
|
||||
public static ITransformer LoadModel()
|
||||
{
|
||||
return mlContext.Model.Load(modelpath, out DataViewSchema inputSchema);
|
||||
}
|
||||
|
||||
public static Microsoft.ML.PredictionEngine<Input, Output> CreateEngine()
|
||||
{
|
||||
ITransformer mlModel = LoadModel(false);
|
||||
ITransformer mlModel = LoadModel();
|
||||
return mlContext.Model.CreatePredictionEngine<Input, Output>(mlModel);
|
||||
}
|
||||
|
||||
public static ITransformer LoadModelArea()
|
||||
{
|
||||
return mlContext.Model.Load(modelpath_area, out DataViewSchema inputSchema);
|
||||
}
|
||||
|
||||
public static Microsoft.ML.PredictionEngine<InputArea, OutputArea> CreateEngineArea()
|
||||
{
|
||||
ITransformer mlModel = LoadModelArea();
|
||||
return mlContext.Model.CreatePredictionEngine<InputArea, OutputArea>(mlModel);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user