diff --git a/InverseOfLife.csproj b/InverseOfLife.csproj index b1b52e4..9f19393 100644 --- a/InverseOfLife.csproj +++ b/InverseOfLife.csproj @@ -10,10 +10,4 @@ true - - - - - - diff --git a/src/NeuralSolver/NeuralSolver.cs b/src/NeuralSolver/NeuralSolver.cs index cb51999..229f4f7 100644 --- a/src/NeuralSolver/NeuralSolver.cs +++ b/src/NeuralSolver/NeuralSolver.cs @@ -8,19 +8,12 @@ namespace InverseOfLife.NeuralSolver; public class NeuralSolver : IDisposable { - - - - - - - - private int Width { get; set; } - private int Height { get; set; } - private bool QuotientX { get; set; } - private bool QuotientY { get; set; } - private string NeuralBackend { get; set; } - private HttpClient HttpClient { get; set; } + private int Width { get; } + private int Height { get; } + private bool QuotientX { get; } + private bool QuotientY { get; } + private string NeuralBackend { get; } + private HttpClient HttpClient { get; } public NeuralSolver(int width, int height, bool quotientX = false, bool quotientY = false, string neuralBackend = "http://localhost:8000") @@ -50,6 +43,20 @@ public class NeuralSolver : IDisposable response.EnsureSuccessStatusCode(); } + public void TryLoad(TrainRequest request) + { + string jsonContent = JsonSerializer.Serialize(request); + StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/try_load", content).Result; + response.EnsureSuccessStatusCode(); + while (true) + { + string status = BackendStatus(); + if (status == "ready" || status == "error") + return; + } + } + public void Train(TrainRequest request) { string jsonContent = JsonSerializer.Serialize(request); @@ -69,7 +76,6 @@ public class NeuralSolver : IDisposable public Board Predict(PredictRequest request) { string jsonContent = JsonSerializer.Serialize(request); - Console.WriteLine(jsonContent); StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/predict", content).Result; response.EnsureSuccessStatusCode(); @@ -152,37 +158,58 @@ public class NeuralSolver : IDisposable yield return point; } } - + public void Dispose() + { + HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/finish", null).Result; + } + public static void Test() { using (NeuralSolver s = new NeuralSolver(20, 20, false, false)) { - //s.Initialize(); - s.Train(new TrainRequest + s.TryLoad(new TrainRequest { ForwardDatasetSize = 2000, ForwardBatchSize = 16, ForwardEpochs = 15, - BackwardDatasetSize = 4000, + BackwardDatasetSize = 16000, BackwardBatchSize = 16, - BackwardEpochs = 20, + BackwardEpochs = 40, }); + //s.SaveModel(); Board b = new Board(20, 20); foreach ((int, int) p in Circle()) b.Lives.Add(p); b.Evaluate(); + Console.WriteLine(b.ToString()); + Console.WriteLine("-------------"); Board h = s.Predict(new PredictRequest { Lives = b.Lives.Select(((int x, int y) cell) => new List{cell.x, cell.y}).ToList(), Direction = "backward" }); - Console.WriteLine(h.ToString()); + + Board h2 = s.Predict(new PredictRequest + { + Lives = h.Lives.Select(((int x, int y) cell) => new List{cell.x, cell.y}).ToList(), + Direction = "backward", + }); + Board h3 = s.Predict(new PredictRequest + { + Lives = h2.Lives.Select(((int x, int y) cell) => new List{cell.x, cell.y}).ToList(), + Direction = "backward", + }); + Console.WriteLine(h3.ToString()); + Console.WriteLine("-------------"); + h3.Evaluate(); + Console.WriteLine(h3.ToString()); + Console.WriteLine("-------------"); + h3.Evaluate(); + Console.WriteLine(h3.ToString()); + } } - public void Dispose() - { - HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/finish", null).Result; - } + }