Projekt_SI_automatyczny_kelner/Assets/Logic/SceneContext.cs
2021-05-19 10:35:58 +02:00

89 lines
3.0 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Logic.Data;
using Logic.Graph;
using Newtonsoft.Json;
using UnityEngine;
namespace Logic
{
public class SceneContext : MonoBehaviour
{
public List<Node> map;
public ObstacleData[] obstacles;
public float nodeBaseCost;
public float rotationCost;
public bool useAStar = true;
public static SceneContext Instance;
public List<Recipe> Recipes = new List<Recipe>();
public string TimeOfDay = "morning";
public Tree DecisionTree;
void Awake()
{
Instance = this;
Application.targetFrameRate = 144;
map = GetComponentsInChildren<Node>().ToList();
DecisionTree = LoadOrLearnDecisionTree();
}
private Tree LoadOrLearnDecisionTree()
{
if (File.Exists("DecisionTree.json"))
{
var tree = JsonConvert.DeserializeObject<Tree>(File.ReadAllText("DecisionTree.json"));
return tree;
}
else
{
var examples = Examples.SelectionExamples();
var knownValues = Tree.GetKnownValuesOfAttribute(examples, examples.Columns.Count - 1);
var valuesAmount = Tree.GetValuesAmount(examples, examples.Columns.Count - 1);
var max = double.MinValue;
var maxIndex = -1;
for (int i = 0; i < valuesAmount.Count; i++)
{
if (valuesAmount[i] > max)
{
max = valuesAmount[i];
maxIndex = i;
}
}
var tree = new Tree {Root = Tree.Learn(examples, null, knownValues[maxIndex])};
var treeJson = JsonConvert.SerializeObject(tree);
File.WriteAllText("DecisionTree.json", treeJson);
return tree;
}
}
private void DrawGizmosFrom(Node from, List<Node> without)
{
foreach (var x in from.neighbors.Where(x=>!without.Contains(x)))
{
Gizmos.color = Color.blue;
var position = @from.transform.position;
Gizmos.DrawLine(position, x.transform.position);
Gizmos.DrawSphere(position,0.1f);
if(!(x is Table)) without.Add(x);
DrawGizmosFrom(x, without);
}
foreach (var x in from.neighbors.Where(x => without.Contains(x)))
{
Gizmos.color = Color.blue;
var position = @from.transform.position;
Gizmos.DrawLine(position, x.transform.position);
Gizmos.DrawSphere(position,0.1f);
}
}
private void OnDrawGizmos()
{
if(map.Count>0)
DrawGizmosFrom(map.First(), new List<Node>());
}
}
}