void Start() { world = new World(mapSize, winDistance, winReward, stepReward, loseReward); nextState = GetRandomState(); actions = new List <Action>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x != 0 || y != 0) { Debug.Log("Action: (" + x + "," + y + ")"); actions.Add(new Action(new Vector2(x, y) / inverseDeltaTime, Vector2.zero)); } } } learner = new QLearner(actions, hiddenSize, numInputs, seed); GameObject goal = Instantiate(goalPrefab); goal.transform.position = nextState.GetGoalPos(); goalPrefab = goal; GameObject player = Instantiate(playerPrefab); player.transform.position = nextState.GetPlayerPos(); playerPrefab = player; background.transform.position = (Vector3)(Vector2.one * mapSize / 2) + Vector3.forward; FindObjectOfType <Camera>().transform.position = (Vector3)(Vector2.one * mapSize / 2) + Vector3.forward * FindObjectOfType <Camera>().transform.position.z; FindObjectOfType <Camera>().orthographicSize = mapSize / 2.0f; }
public AIAgent(int player) : base(player) { playerNum = player; opponentNum = playerNum == 1 ? 2 : 1; // Create a new, default QLearner. Usually want to replace this value for more control QLearner = new QLearner(); IsLearning = false; // Set strategy strategy = Strategy.StrategyWithType(playerNum, StrategyType.RunAway); level1Searcher = new DiscreteAdversarialSearch(playerNum, strategy.Level1Heuristic, getFillerAction, getNewPathIndex, Level1StepSize, 4); decisionTimer = 0; level2Searcher = new AStar(Level2MaxNodesInPrioQueue, Level2MaxExpansions, strategy.Level2CostFunction, strategy.Level2GoalFunction, strategy.Level2HeuristicFunction); fillerAction = WorldAction.NoAction; isFirstTime = true; strategyTimer = MaxStrategyTime; calculatePathNextFrame = false; }
void Start() { world = new World(mapSize, winDistance, winReward, stepReward, loseReward); nextState = GetRandomState(); actions = new List <Action>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x != 0 || y != 0) { Debug.Log("Action: (" + x + "," + y + ")"); actions.Add(new Action(new Vector2(x, y) / inverseDeltaTime, Vector2.zero)); } } } learner = new QLearner(eValue, alpha, gamma, eta, actions, hiddenSizes, numInputs, seed); GameObject goal = Instantiate(goalPrefab); goal.transform.position = nextState.GetGoalPos(); goalPrefab = goal; GameObject player = Instantiate(playerPrefab); player.transform.position = nextState.GetPlayerPos(); playerPrefab = player; }
public void Start() { // Set up the world with the initial state currentWorld = new World(); // Create QLearner object qLearner = new QLearner(alpha: 0.3f, epsilon: 0.15f, discount: 0.66f); qLearner.OpenSavedData(); // Create 2 ai agents agentList = new List <IAgent>(); AIAgent ai1 = new AIAgent(1); ai1.IsLearning = true; ai1.QLearner = qLearner; AIAgent ai2 = new AIAgent(2); ai2.IsLearning = true; ai2.QLearner = qLearner; agentList.Add(ai1); agentList.Add(ai2); // Specify number of games numberOfGames = 10; // Specify which iteration of games we are on gameIteration = 1; gameFrames = 0; Debug.Log("Beginning learning. Simulating " + numberOfGames.ToString() + " games before writing."); }
// First-time setup void Start() { // Set up the world with the initial state currentWorld = new RenderedWorld(this); currentWorld.Display(); Gui.SetMode(0); restartTimer = -1; winner = 0; agentList = new List <IAgent>(); #if AIEnabled // Create q-learning object for the AI, and pick up learning where we left off qLearner = new QLearner(alpha: 0.3f, epsilon: 0.15f, discount: 0.66f); qLearner.OpenSavedData(); #endif #if AIP1 // Create AI for player 1 AIAgent player1Ai = new AIAgent(1); player1Ai.ResourceScript = this; // For debug rendering player1Ai.QLearner = qLearner; player1Ai.IsLearning = true; agentList.Add(player1Ai); #else // Create the human agent agentList.Add(new WASDFAgent(1)); #endif #if AIP2 // Create AI for player 1 AIAgent player2Ai = new AIAgent(2); player2Ai.ResourceScript = this; // For debug rendering player2Ai.QLearner = qLearner; player2Ai.IsLearning = true; agentList.Add(player2Ai); #else // Create the human agent agentList.Add(new ArrowsAgent(2)); #endif }
void Start() { world = new World(mapSize); List <Action> actions = new List <Action>(); actions.Add(new Action(Vector2.up)); actions.Add(new Action(Vector2.down)); actions.Add(new Action(Vector2.left)); actions.Add(new Action(Vector2.right)); List <int> tableDimensions = new List <int>(); tableDimensions.Add(mapSize); tableDimensions.Add(mapSize); tableDimensions.Add(mapSize); //tableDimensions.Add(mapSize); tableDimensions.Add(actions.Count); learner = new QLearner(actions, tableDimensions, startingQ); nextState = GetRandomState(); playerPrefab = Instantiate(playerPrefab); counter = Time.time; background.transform.position = Vector2.one * mapSize / 2; FindObjectOfType <Camera>().transform.position = (Vector3)(Vector2.one * mapSize / 2) + Vector3.forward * FindObjectOfType <Camera>().transform.position.z; FindObjectOfType <Camera>().orthographicSize = mapSize / 2.0f; }
/// <summary> /// Basic initialisation. /// </summary> void Start() { QLearner.SetParamReceiver(this); label = this.GetComponent <Text>(); }
private void Start() { _qLearner = GetComponent <QLearner>(); _qLearner.OnStep += Step; _qLearner.OnEpisodeComplete += EpisodeComplete; }
public override void Initialise(GameState state) { Globals.state = state; Globals.QLearnerFolder = FindQLearnerFolder(); Globals.random = new Random(state.PlayerSeed); Globals.pathFinder = new Pathfinder(state.Width, state.Height); Globals.enemyInfluence = new InfluenceMap(state.map); Globals.friendlyInfluence = new InfluenceMap(state.map); Globals.hillInfluence = new InfluenceMap(state.map); Globals.foodInfluence = new InfluenceMap(state.map); string backupFile = Globals.QLearnerFolder + "\\" + "Qbackup.Q"; string originalFile = Globals.QLearnerFolder + "\\" + "QData.Q"; if(File.Exists(originalFile))//previously run, there is learning data { BinaryFormatter formatter = new BinaryFormatter(); Stream learnerStream = new FileStream(originalFile, FileMode.Open); Learner = (QLearner)formatter.Deserialize(learnerStream); learnerStream.Close(); if (File.Exists(backupFile)) { FileInfo originalInfo = new FileInfo(originalFile); FileInfo backupInfo = new FileInfo(backupFile); if (originalInfo.Length > backupInfo.Length)//the more the bots learn the bigger this should get, if it is suddenly smaller there has been a serialisation error, do not backup System.IO.File.Copy(originalFile, backupFile, true);//in case of deserialisation error, your progress is not lost } else { System.IO.File.Copy(originalFile, backupFile, true);//in case of deserialisation error, your progress is not lost } Learner.GamesPlayed++; } else { Learner = new QLearner(0.9f, 0.8f, 0.9f, state.PlayerSeed);//very first game, no learning done previously } }