Created
July 8, 2017 08:39
-
-
Save 0111narwhalz/1c91f62899f6a5275ee00cae7c89fff1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Interfaces the ANN with KSP | |
*/ | |
using System; | |
using Saiak; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using KSP; | |
namespace Saiak | |
{ | |
[KSPAddon(KSPAddon.Startup.Flight,false)] | |
class Environment : PartModule | |
{ | |
static bool firstRun = true; | |
bool vesselFound = false; | |
public static Vessel ship; | |
/* | |
public static void Main() | |
{ | |
Console.Write("Initializing"); | |
Start(); | |
Console.WriteLine("Complete"); | |
for(;;) | |
{ | |
if(Loop()) | |
{ | |
break; | |
} | |
} | |
Console.WriteLine("Terminated"); | |
Console.Read(); | |
} | |
*/ | |
public new void Start() | |
{ | |
vesselFound = true; | |
try | |
{ | |
ship = part.vessel; | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("vessel err"); | |
vesselFound = false; | |
} | |
ExhaustManifold.Start(); | |
if(HighLogic.LoadedScene != GameScenes.FLIGHT || !firstRun) | |
{ | |
return; | |
} | |
NeuralNetwork.Start(); | |
//Console.Write('.'); | |
GeneticAlgorithm.Start(); | |
//Console.Write('.'); | |
IntakeManifold.Start(); | |
//Console.Write('.'); | |
NeuralNetwork.Zero(); | |
firstRun = false; | |
} | |
public void FixedUpdate() | |
{ | |
if(HighLogic.LoadedScene != GameScenes.FLIGHT) | |
{ | |
return; | |
} | |
if(FlightGlobals.ready && FlightGlobals.fetch != null) | |
{ | |
Debug.Log("FlightGlobals fault"); | |
return; | |
} | |
if(!vesselFound) | |
{ | |
Debug.Log("Acquiring vessel"); | |
try | |
{ | |
ship = FlightGlobals.fetch.ActiveVessel; | |
vesselFound = true; | |
} | |
catch(NullReferenceException nre) | |
{ | |
vesselFound = false; | |
Debug.Log("Not found"); | |
} | |
} | |
if(ship.missionTime <= 10) | |
{ | |
return; | |
} | |
try | |
{ | |
if(Terminate()) | |
{ | |
Debug.Log(string.Format("Individual {0} is dead.", GeneticAlgorithm.id)); | |
GeneticAlgorithm.fitness[GeneticAlgorithm.id] = ExhaustManifold.Evaluate(); | |
Debug.Log(string.Format("Fitness {0}", GeneticAlgorithm.fitness[GeneticAlgorithm.id])); | |
GeneticAlgorithm.id++; | |
NeuralNetwork.Zero(); | |
GamePersistence.LoadGame("SaiakStartPoint", HighLogic.fetch.GameSaveFolder, false, false).Start(); | |
} | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("Problems with termination!"); | |
} | |
try | |
{ | |
IntakeManifold.Update(); | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("IntakeManifold"); | |
} | |
try | |
{ | |
NeuralNetwork.Update(); | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("NeuralNetwork"); | |
} | |
try | |
{ | |
ExhaustManifold.Update(); | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("ExhaustManifold"); | |
} | |
try | |
{ | |
GeneticAlgorithm.Update(); | |
} | |
catch(NullReferenceException nre) | |
{ | |
Debug.Log("GA"); | |
} | |
} | |
bool Terminate() | |
{ | |
if(ship.state == Vessel.State.DEAD) | |
{ | |
return true; | |
} | |
if(ship.state == Vessel.State.INACTIVE) | |
{ | |
return false; | |
} | |
if(ship.CurrentControlLevel == Vessel.ControlLevel.NONE) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment