From 72a7a8aab36d5ebffae8157646e3d6f08e9d6a24 Mon Sep 17 00:00:00 2001 From: BOTLester <58360400+BOTLester@users.noreply.github.com> Date: Mon, 15 Jun 2020 01:45:16 +0200 Subject: [PATCH] Proper files paths for debug only --- Game1/Sources/Crops/Farm.cs | 19 +++++++++++++-- Game1/Sources/ML/MLModel.cs | 39 +++++++++++++++++++------------ Game1/Sources/ML_Joel/Model.cs | 42 +++++++++++++++++++--------------- 3 files changed, 65 insertions(+), 35 deletions(-) diff --git a/Game1/Sources/Crops/Farm.cs b/Game1/Sources/Crops/Farm.cs index f971b99..4e351f3 100644 --- a/Game1/Sources/Crops/Farm.cs +++ b/Game1/Sources/Crops/Farm.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Reflection; using System.Collections.Generic; using System.Linq; using System.Text; @@ -24,6 +26,7 @@ class Farm private float updateProgress = 0; private float nextUpdate = 0; private int productionUpdate = 0; + private string path_base = FindPath(); //initializes the crops @@ -84,8 +87,7 @@ class Farm coef = 1.0f; WindSpeed.X = GetRandomNumber(0.9f, 1f) / 50 * coef; WindSpeed.Y = GetRandomNumber(0.9f, 1f) / 50 * coef; - //RainfallMap = PerlinNoise.LoadImage("C:\\Users\\Joel\\source\\repos\\Oskars Repo\\Game1\\Content\\Rainfall.png"); - RainfallMap = PerlinNoise.LoadImage("C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/Rainfall.png"); + RainfallMap = PerlinNoise.LoadImage(System.IO.Path.Combine(path_base, "Content/Rainfall.png")); } public Rectangle getRainPosition(int TileSize, int x, int y, Vector2 Size) @@ -331,4 +333,17 @@ class Farm { return (float)(Math.Round(r.NextDouble() * (maximum - minimum) + minimum, 2)); } + + private static string FindPath() + { + string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).ToString(); + while (true) + { + path = Directory.GetParent(path).ToString(); + if (path.EndsWith("\\Game1")) + { + return path; + } + } + } } diff --git a/Game1/Sources/ML/MLModel.cs b/Game1/Sources/ML/MLModel.cs index 4dd8cd5..294bddd 100644 --- a/Game1/Sources/ML/MLModel.cs +++ b/Game1/Sources/ML/MLModel.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.ML; @@ -11,23 +12,17 @@ using Microsoft.ML.Trainers.LightGbm; class MLModel { private static MLContext mlContext = new MLContext(seed: 1); + + private static string path_base = FindPath(); - 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 = System.IO.Path.Combine(path_base, "Content/ML/Fertilizer_Prediction.csv"); + private static string modelpath = System.IO.Path.Combine(path_base, "Content/ML/MLmodel"); + private static string report = System.IO.Path.Combine(path_base, "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 pathBig = System.IO.Path.Combine(path_base, "Content/ML/BigFertPredict.csv"); + private static string modelpathBig = System.IO.Path.Combine(path_base, "Content/ML/MLmodelBig"); + private static string reportBig = System.IO.Path.Combine(path_base, "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() { @@ -167,11 +162,25 @@ class MLModel return mlContext.Model.Load(modelpath, out DataViewSchema inputSchema); } - public static Microsoft.ML.PredictionEngine CreateEngine() + public static PredictionEngine CreateEngine() { ITransformer mlModel = LoadModel(false); return mlContext.Model.CreatePredictionEngine(mlModel); } + private static string FindPath() + { + string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).ToString(); + while (true) + { + path = Directory.GetParent(path).ToString(); + if (path.EndsWith("\\Game1")) + { + return path; + } + } + } + + } diff --git a/Game1/Sources/ML_Joel/Model.cs b/Game1/Sources/ML_Joel/Model.cs index 1724956..6acef1a 100644 --- a/Game1/Sources/ML_Joel/Model.cs +++ b/Game1/Sources/ML_Joel/Model.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,24 +14,17 @@ namespace Game1.Sources.ML_Joel class Model { private static MLContext mlContext = new MLContext(seed: 1); - - private static string path = "C:/Users/Oskar/source/repos/PotatoPlanFinal/Game1/Content/ML/Rainfall.csv"; - 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"; - - /* - private static string path = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/Rainfall.csv"; - private static string modelpath = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodel_Joel"; - private static string report = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report_Joel"; + private static string path_base = FindPath(); + + private static string path = System.IO.Path.Combine(path_base, "Content/ML/Rainfall.csv"); + private static string modelpath = System.IO.Path.Combine(path_base, "Content/ML/MLmodel_Joel"); + private static string report = System.IO.Path.Combine(path_base, "Content/ML/report_Joel"); + + private static string path_area = System.IO.Path.Combine(path_base, "Content/ML/Rainfall_area.csv"); + private static string modelpath_area = System.IO.Path.Combine(path_base, "Content/ML/MLmodel_Joel_area"); + private static string report_area = System.IO.Path.Combine(path_base, "Content/ML/report_Joel_area"); - private static string path_area = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/Rainfall_area.csv"; - private static string modelpath_area = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/MLmodel_Joel_area"; - private static string report_area = "C:/Users/Joel/source/repos/Oskars Repo/Game1/Content/ML/report_Joel_area"; - */ // Loading data, creatin and saving ML model for smaller dataset (100) public static void CreateModel() { @@ -163,7 +157,7 @@ namespace Game1.Sources.ML_Joel return mlContext.Model.Load(modelpath, out DataViewSchema inputSchema); } - public static Microsoft.ML.PredictionEngine CreateEngine() + public static PredictionEngine CreateEngine() { ITransformer mlModel = LoadModel(); return mlContext.Model.CreatePredictionEngine(mlModel); @@ -174,11 +168,23 @@ namespace Game1.Sources.ML_Joel return mlContext.Model.Load(modelpath_area, out DataViewSchema inputSchema); } - public static Microsoft.ML.PredictionEngine CreateEngineArea() + public static PredictionEngine CreateEngineArea() { ITransformer mlModel = LoadModelArea(); return mlContext.Model.CreatePredictionEngine(mlModel); } + private static string FindPath() + { + string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).ToString(); + while(true) + { + path = Directory.GetParent(path).ToString(); + if (path.EndsWith("\\Game1")) + { + return path; + } + } + } } }