public static void Main() { AgentHost agentHost = new AgentHost(); try { agentHost.parse( new StringVector( Environment.GetCommandLineArgs() ) ); } catch( Exception ex ) { Console.Error.WriteLine("ERROR: {0}", ex.Message); Console.Error.WriteLine(agentHost.getUsage()); Environment.Exit(1); } if( agentHost.receivedArgument("help") ) { Console.Error.WriteLine(agentHost.getUsage()); Environment.Exit(0); } MissionSpec mission = new MissionSpec(); mission.timeLimitInSeconds(10); mission.requestVideo( 320, 240 ); mission.rewardForReachingPosition(19.5f,0.0f,19.5f,100.0f,1.1f); MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz"); missionRecord.recordCommands(); missionRecord.recordMP4(20, 400000); missionRecord.recordRewards(); missionRecord.recordObservations(); try { agentHost.startMission(mission, missionRecord); } catch (Exception ex) { Console.Error.WriteLine("Error starting mission: {0}", ex.Message); Environment.Exit(1); } WorldState worldState; Console.WriteLine("Waiting for the mission to start"); do { Console.Write("."); Thread.Sleep(100); worldState = agentHost.getWorldState(); foreach (TimestampedString error in worldState.errors) Console.Error.WriteLine("Error: {0}", error.text); } while (!worldState.is_mission_running); Console.WriteLine(); Random rand = new Random(); // main loop: do { agentHost.sendCommand("move 1"); agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble())); Thread.Sleep(500); worldState = agentHost.getWorldState(); Console.WriteLine( "video,observations,rewards received: {0}, {1}, {2}", worldState.number_of_video_frames_since_last_state, worldState.number_of_observations_since_last_state, worldState.number_of_rewards_since_last_state); foreach (TimestampedReward reward in worldState.rewards) Console.Error.WriteLine("Summed reward: {0}", reward.getValue()); foreach (TimestampedString error in worldState.errors) Console.Error.WriteLine("Error: {0}", error.text); } while (worldState.is_mission_running); Console.WriteLine("Mission has stopped."); }
private void runMission(MissionSpec mission) { string recordPath = "none"; MissionRecordSpec missionRecord; if (string.IsNullOrEmpty(Settings.Default.OutputDir)) { missionRecord = new MissionRecordSpec(); } else { recordPath = Path.Combine(Settings.Default.OutputDir, Guid.NewGuid() + ".tar.gz"); missionRecord = new MissionRecordSpec(recordPath); if (Settings.Default.RecordObservations) { missionRecord.recordObservations(); } if (Settings.Default.RecordRewards) { missionRecord.recordRewards(); } if (Settings.Default.RecordCommands) { missionRecord.recordCommands(); } if (Settings.Default.RecordMP4) { missionRecord.recordMP4(Settings.Default.MP4FPS, (long)(Settings.Default.BitRate * 1024)); } } using (AgentHost agentHost = new AgentHost()) { ClientPool clientPool = new ClientPool(); clientPool.add(new ClientInfo(Settings.Default.ClientIP, Settings.Default.ClientPort)); try { agentHost.startMission(mission, clientPool, missionRecord, 0, "hac"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Starting Mission", MessageBoxButton.OK, MessageBoxImage.Error); return; } Dispatcher.Invoke(() => { Activate(); VideoImage.Focus(); startB.Content = "Set"; Title = Settings.Default.Title + " : Waiting for mission start"; if (_recordWindow != null && _recordWindow.IsVisible) { _recordWindow.RecordPath = recordPath; } }); _isInSession = true; try { WorldState worldState; // wait for mission to start do { Thread.Sleep(100); worldState = agentHost.getWorldState(); if (worldState.errors.Any()) { StringBuilder errors = new StringBuilder(); foreach (TimestampedString error in worldState.errors) { errors.AppendLine(error.text); } MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error); return; } } while (!worldState.is_mission_running && !_stopMission); if (_stopMission) { return; } DateTime missionStartTime = DateTime.UtcNow; _timeRemaining = _missionDuration; Dispatcher.Invoke(() => { startB.Content = "Go!"; Title = Settings.Default.Title + " : Recording"; }); // run mission TimeSpan loopTime = TimeSpan.FromSeconds(1.0 / 20); _pendingCommandsMutex.Wait(); try { _pendingCommandQueue = new Queue<Tuple<string, float>>(); } finally { _pendingCommandsMutex.Release(); } _pendingMessagesMutex.Wait(); try { _pendingMessages = new Queue<string>(); } finally { _pendingMessagesMutex.Release(); } bool failure = false; Stopwatch loopTimer = new Stopwatch(); do { loopTimer.Reset(); loopTimer.Start(); worldState = agentHost.getWorldState(); TimestampedVideoFrame frame = worldState.video_frames.FirstOrDefault(); if (frame != null) { if (_missionDuration != TimeSpan.Zero) { TimeSpan elapsed = frame.timestamp - missionStartTime; _timeRemaining = _missionDuration - elapsed; } _pixelsMutex.Wait(); try { if (_pixels == null || _pixels.Length != frame.pixels.Count) { _pixels = new byte[frame.pixels.Count]; Dispatcher.Invoke(() => { if (_bitmap.Width != frame.width || _bitmap.Height != frame.height) { _bitmap = new WriteableBitmap(frame.width, frame.height, 72, 72, PixelFormats.Rgb24, null); VideoImage.Source = _bitmap; if (_recordWindow != null && _recordWindow.IsVisible) { _recordWindow.FrameSource = _bitmap; } } }); } frame.pixels.CopyTo(_pixels); } finally { _pixelsMutex.Release(); } } _pendingMessagesMutex.Wait(); try { foreach (var reward in worldState.rewards) { _score += reward.getValue(); if (reward.getValue() < 0) { failure = true; } _pendingMessages.Enqueue(string.Format("{0}> score {1}", reward.timestamp.ToString("hh:mm:ss.fff"), reward.getValue())); } _score = Math.Max(_score, 0); _score = Math.Min(_score, 99999); foreach (var observation in worldState.observations) { int posStart = observation.text.IndexOf("\"XPos\""); if (posStart < 0) { continue; } int posEnd = observation.text.IndexOf("\"ZPos\""); posEnd = observation.text.IndexOf(',', posEnd); string posSegment = observation.text.Substring(posStart, posEnd - posStart); string[] pos = posSegment.Split(','); float x = Convert.ToSingle(pos[0].Split(':')[1]); float y = Convert.ToSingle(pos[1].Split(':')[1]); float z = Convert.ToSingle(pos[2].Split(':')[1]); _pendingMessages.Enqueue(string.Format("{0}> (x={1:0.00}, y={2:0.00}, z={3:0.00})", observation.timestamp.ToString("hh:mm:ss.fff"), x, y, z)); } } finally { _pendingMessagesMutex.Release(); } CheckGamepad(agentHost); _pendingCommandsMutex.Wait(); try { while (_pendingCommandQueue.Any()) { var command = _pendingCommandQueue.Dequeue(); CheckAndSend(agentHost, command.Item1, command.Item2); } } finally { _pendingCommandsMutex.Release(); } loopTimer.Stop(); if (loopTimer.Elapsed < loopTime) { Thread.Sleep(loopTime - loopTimer.Elapsed); } } while (worldState.is_mission_running && !_stopMission); if (_stopMission) { return; } if (!failure) { _score += _timeRemaining.TotalSeconds * 100; } _missionSuccess = !failure; _pendingCommandsMutex.Wait(); try { _pendingCommandQueue = null; } finally { _pendingCommandsMutex.Release(); } Dispatcher.Invoke(() => { Title = Settings.Default.Title + " : Ready"; UpdateDisplayedMessages(); UpdateDisplayedReward(); }); _resetVerb = null; var keys = _continuousCommandState.Keys.ToList(); foreach (var verb in keys) { _continuousCommandState[verb] = 0; } keys = _discreteCommandState.Keys.ToList(); foreach (var verb in keys) { _discreteCommandState[verb] = false; } if (worldState.errors.Any()) { StringBuilder errors = new StringBuilder(); foreach (TimestampedString error in worldState.errors) { errors.AppendLine(error.text); } MessageBox.Show(errors.ToString(), "Error during mission initialization", MessageBoxButton.OK, MessageBoxImage.Error); return; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error during mission", MessageBoxButton.OK, MessageBoxImage.Error); return; } finally { _isInSession = false; } } }
public static void Main() { AgentHost agentHost = new AgentHost(); try { agentHost.parse(new StringVector(Environment.GetCommandLineArgs())); } catch (Exception ex) { Console.Error.WriteLine("ERROR: {0}", ex.Message); Console.Error.WriteLine(agentHost.getUsage()); Environment.Exit(1); } if (agentHost.receivedArgument("help")) { Console.Error.WriteLine(agentHost.getUsage()); Environment.Exit(0); } MissionSpec mission = new MissionSpec(); mission.timeLimitInSeconds(10); mission.requestVideo(320, 240); mission.rewardForReachingPosition(19.5f, 0.0f, 19.5f, 100.0f, 1.1f); MissionRecordSpec missionRecord = new MissionRecordSpec("./saved_data.tgz"); missionRecord.recordCommands(); missionRecord.recordMP4(20, 400000); missionRecord.recordRewards(); missionRecord.recordObservations(); try { agentHost.startMission(mission, missionRecord); } catch (Exception ex) { Console.Error.WriteLine("Error starting mission: {0}", ex.Message); Environment.Exit(1); } WorldState worldState; Console.WriteLine("Waiting for the mission to start"); do { Console.Write("."); Thread.Sleep(100); worldState = agentHost.getWorldState(); foreach (TimestampedString error in worldState.errors) { Console.Error.WriteLine("Error: {0}", error.text); } }while (!worldState.has_mission_begun); Console.WriteLine(); Random rand = new Random(); // main loop: do { agentHost.sendCommand("move 1"); agentHost.sendCommand(string.Format("turn {0}", rand.NextDouble())); Thread.Sleep(500); worldState = agentHost.getWorldState(); Console.WriteLine( "video,observations,rewards received: {0}, {1}, {2}", worldState.number_of_video_frames_since_last_state, worldState.number_of_observations_since_last_state, worldState.number_of_rewards_since_last_state); foreach (TimestampedReward reward in worldState.rewards) { Console.Error.WriteLine("Summed reward: {0}", reward.getValue()); } foreach (TimestampedString error in worldState.errors) { Console.Error.WriteLine("Error: {0}", error.text); } }while (worldState.is_mission_running); Console.WriteLine("Mission has stopped."); }