private void panelCycle(object state) { // Update internal state of Torus Torus.Update(); // On two threads: Invalidate display (paint event will be run on seperate thread) and calculate new internal state (seperate thread too). Parallel.Invoke(() => panelDisplay.Invalidate(), () => Torus.DoCycle()); }
public Skater Move() { double reward; // Get an action following some policy/strategy int action = DetermineAction(Parameters.CurrentPolicy); Torus.actionHistogram[action]++; // Calculate possible new position double direction = Parameters.DirectionList[action]; double xCoordNewPosition = Shared.Mod((xCoord + Parameters.StepSize * Math.Cos(direction)), 1); double yCoordNewPosition = Shared.Mod((yCoord + Parameters.StepSize * Math.Sin(direction)), 1); // Check Collision if (Torus.CollisionCheck(this, xCoordNewPosition, yCoordNewPosition)) { RecentCollision = Parameters.CollisionFadeSpeed; // Due to collision risk, movement is cancelled! // The newly gennerated coordinates will not be applied. Torus.collisionCount++; reward = CalculateReward(action, false); } else { // No collision, so we update the coordinates of this skater! xCoord = xCoordNewPosition; yCoord = yCoordNewPosition; if (RecentCollision > 0) { RecentCollision--; } reward = CalculateReward(action, true); } // update RL tables UpdateQValue(action, reward); // And log reward to histogram. Torus.rewardHistogram[action] += reward; // No Collision -> Update position return(new Skater(xCoord, yCoord, RecentCollision, qValues, InitialColor)); }
private void buttonStartStop_Click(object sender, EventArgs e) { if (!sim_running) { // Change button text. buttonStartStop.Text = "Reset"; // Flip bool. sim_running = true; // Lock parameter settings. layoutPanelForParameterControls.Enabled = false; // Update internal Parameters Parameters.InitParameters( numericUpDownNumSkaters.Value, comboBoxNumAngles.Text, numericUpDownCollisionRadius.Value, numericUpDownSkaterSpeed.Value, numericUpDownSuccessfullMove.Value, numericUpDownFailedMoveReward.Value, checkBoxModifyRewards.Checked, checkBoxLearningRateDecreasing.Checked, numericUpDownLearningRate.Value, checkBoxEpsilonDecreasing.Checked, numericUpDownEpsilon.Value, numericUpDownSkaterDotSize.Value, comboBoxPalette.Text, numericUpDownFadeSpeed.Value); // Prep torus, pass parameter: Number of skaters Torus.InitializeTorus(); //Clear Chart if (Parameters.AverageOnly) { chartCollisions.Series[0].Points.Clear(); if (chartCollisions.Series.Count > 1) { chartCollisions.Series.RemoveAt(1); } chartRecentCollisions.Series[0].Points.Clear(); if (chartRecentCollisions.Series.Count > 1) { chartRecentCollisions.Series.RemoveAt(1); } } else { chartCollisions.Series[0].Points.Clear(); chartCollisions.Series[1].Points.Clear(); chartRecentCollisions.Series[0].Points.Clear(); chartRecentCollisions.Series[1].Points.Clear(); } // New log file. Logging.NewLogFile(); // Start timer. Refreshes the simulation. refreshTimer = new System.Threading.Timer(panelCycle, null, 1000, (int)(1000 / Parameters.MovementSpeed)); } else { // Reset initial state; re-enable controls. buttonStartStop.Text = "Start"; sim_running = false; layoutPanelForParameterControls.Enabled = true; // Kill timer thread. refreshTimer.Dispose(); // Stop logging Logging.EndLogging(); // Clear display area. panelDisplay.Invalidate(); Torus.Clear(); } }