Esempio n. 1
0
 public NeuralRobot(BasicNetwork network, bool track, Position source, Position destination)
 {
     _hStats = new NormalizedField(NormalizationAction.Normalize, "Heading", 359, 0, .9, -.9);
     _CanGoStats = new NormalizedField(NormalizationAction.Normalize, "CanGo", 1, 0, 0.9, -0.9);
     _track = track;
     _network = network;
     sim = new RobotSimulator(source, destination);
 }
Esempio n. 2
0
 public RobotSimulator(Position source, Position destination)
 {
     Success = -10000;
     Fuel = 9000;
     Seconds = 0;
     Altitude = 100000;
     Rests = 0;
     Turns = 0;
     Position = new double[2] {source.X, source.Y};
     Destination = new double[2] {destination.X, destination.Y};
     StartPosition = new double[2] {source.X, source.Y};
     CurrentDirection = CommandDirection.MoveForward;
     DistanceToDestination = CalculateDistance();
     LastDistance = DistanceToDestination;
     PreviousDistance = LastDistance;
     StartDistance = DistanceToDestination;
     UpdateHeading();
 }
Esempio n. 3
0
 private void Simulation_PositionChanged(object sender, Position position)
 {
     var p = AddELocation(false);
     p.Fill = new SolidColorBrush(Color.FromRgb(200, 200, 200));
     p.Stroke =new SolidColorBrush(Color.FromRgb(200, 200, 200));
     var w = Canvas.ActualWidth / SourceData.Warehouse.GridWidth;
     var h = Canvas.ActualHeight / SourceData.Warehouse.GridLength;
     Canvas.SetLeft(p, position.X * ((int)(Canvas.ActualHeight / SourceData.Warehouse.GridWidth) - 1));
     Canvas.SetTop(p, position.Y * ((int)(Canvas.ActualWidth / SourceData.Warehouse.GridLength) - 1));
     Canvas.Children.Add(p);
     Canvas.UpdateLayout();
     DoEvents();
 }
Esempio n. 4
0
 private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
     var pos = e.GetPosition(Canvas);
     var x = (int)pos.X / ((int)(Canvas.ActualHeight / SourceData.Warehouse.GridWidth) - 1);
     var y = (int)pos.Y / ((int)(Canvas.ActualWidth / SourceData.Warehouse.GridLength) - 1);
     var p = AddLocation(false);
     switch (SourceData.ModeMap)
     {
         case MapMode.ChargeMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(200, 0, 0));
             break;
         case MapMode.ObstructionMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(100, 100, 100));
             break;
         case MapMode.PersonMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(128, 100, 50));
             break;
         case MapMode.PickupMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(138, 43, 226));
             break;
         case MapMode.ShipMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(127, 255, 0));
             break;
         case MapMode.StorageMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(165, 42, 42));
             break;
         case MapMode.PathMode:
             p.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 200));
             break;
     }
     if (SourceData.Warehouse.Positions == null)
         SourceData.Warehouse.Positions = new ObservableCollection<Position>();
     var l =
         (from loc in SourceData.Warehouse.Positions where loc.X == x && loc.Y == y select loc).FirstOrDefault();
     if (l != null)
     {
         l.MapMode = SourceData.ModeMap;
     }
     else
     {
         l = new Position(x, y) { MapMode = SourceData.ModeMap };
         SourceData.Warehouse.Positions.Add(l);
     }
     var col = DataService.DataBase.GetCollection<Rover.Models.Warehouse>("Warehouse");
     SafeModeResult safeModeResult = col.Save(SourceData.Warehouse);
     Canvas.SetLeft(p, x * ((int)(Canvas.ActualHeight / SourceData.Warehouse.GridWidth) - 1));
     Canvas.SetTop(p, y * ((int)(Canvas.ActualWidth / SourceData.Warehouse.GridLength) - 1));
     Canvas.Children.Add(p);
 }
Esempio n. 5
0
        public void RunNetwork(Guid id)
        {
            BasicNetwork network = null;

            var fn = id.ToString();
            var net = DataService.GetCollectionQueryModel<NeuralNetwork>(Query.EQ("_id", id)).FirstOrDefault();
            if (net != null)
            {
                SourceLocation = new Position(net.StartPosition.X, net.StartPosition.Y);
                DestLocation = new Position(net.EndPosition.X, net.EndPosition.Y);

                lock (RobotContol.NetworkLock)
                {
                    var rawbytes = DataService.OpenFile(id);

                    File.WriteAllBytes(fn, rawbytes);
                    network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(fn));
                }
                var pilot = new NeuralRobot(network, true, SourceLocation, DestLocation);
                SourceData.Simulation = pilot.sim;
                DataContext = SourceData;
                DrawMap();
                SourceData.Simulation.PositionChanged += Simulation_PositionChanged;
                pilot.ScorePilot();
                File.Delete(fn);

            }

            DoEvents();
        }
Esempio n. 6
0
        public void BuildNetwork(double slat, double slon, double lat, double lon)
        {
            string hostname = Dns.GetHostName();
            SourceLocation = new Position(slat, slon);
            DestLocation = new Position(lat, lon);
            BasicNetwork network = CreateNetwork();
            string fn = @"Robot_From" + slat + "_" + slon + "_To_" + lat + "_" + lon + ".net";
            int chromosomes = 128;
            IMLTrain train;
            train = new MLMethodGeneticAlgorithm(() =>
                {
                    BasicNetwork result = CreateNetwork();
                    ((IMLResettable) result).Reset();
                    return result;
                }, new RobotScore(), chromosomes);
            int epoch = 1;
            var scoresAverage = new List<double>();
            double netavg = 0;

            while (epoch <= chromosomes || train.Error <= 0)
            {
                GC.Collect();
                Scores = new List<double>();
                train.Iteration();
                double average = GetitterationAverage();
                scoresAverage.Add(average);
                double avg = GetTrainAverage(scoresAverage);
                //_ProgressQueue.Publish(new TaskProgressMessage
                //    {
                //        CurrentTime = DateTime.Now,
                //        PercentComplete = epoch,
                //        StartTime = _starttime,
                //        Status = TaskStatus.InProcess,
                //        TaskId = _taskid,
                //        MessageId = Guid.NewGuid(),
                //        TransmisionDateTime = DateTime.Now,
                //        Details =
                //            hostname + @" Epoch #" + epoch + @" Score:" + train.Error + @" Chromosomes: " + chromosomes
                //    }
                //    );

                lock (NetworkLock)
                {
                    if (train.Error > 0)
                    {
                        NeuralNetwork net =
                            Mds.GetCollectionQueryModel<NeuralNetwork>(Query.And(Query.EQ("StartPosition.X", slat),
                                                                                 Query.EQ("StartPosition.Y", slon),
                                                                                 Query.EQ("EndPosition.X", lat),
                                                                                 Query.EQ("EndPosition.Y", lon)))
                               .FirstOrDefault();
                        if (net == null)
                            net = new NeuralNetwork
                                {
                                    EndPosition = new Position(lat, lon),
                                    Id = Guid.NewGuid(),
                                    StartPosition = new Position(slat, slon)
                                };

                        FileStream fs = File.Create(fn);
                        EncogDirectoryPersistence.SaveObject(fs, train.Method);
                        fs.Close();
                        MongoCollection<NeuralNetwork> col = Mds.DataBase.GetCollection<NeuralNetwork>("NeuralNetwork");
                        col.Save(net);
                        Mds.SaveFile(fn, net.Id);
                        File.Delete(fn);
                    }
                }
                epoch++;

                if (epoch > (chromosomes/2) && train.Error <= 0)
                {
                    chromosomes *= 2;

                    train = new MLMethodGeneticAlgorithm(() =>
                        {
                            BasicNetwork result = CreateNetwork();
                            ((IMLResettable) result).Reset();
                            return result;
                        }, new RobotScore(), chromosomes);

                    epoch = 1;
                }

                netavg = avg;
                if (chromosomes == 4096)
                    break;
            }
            _ProgressQueue.Publish(new TaskProgressMessage
                {
                    CurrentTime = DateTime.Now,
                    PercentComplete = 100,
                    StartTime = _starttime,
                    Status = TaskStatus.Complete,
                    TaskId = _taskid,
                    MessageId = Guid.NewGuid(),
                    TransmisionDateTime = DateTime.Now,
                    Details = hostname
                }
                );
            EncogFramework.Instance.Shutdown();
        }
Esempio n. 7
0
 public void RunNetworks()
 {
     var reclocs = (from p in SourceData.Warehouse.Positions where p.MapMode == MapMode.PickupMode select p).ToArray();
     var shiplocs = (from p in SourceData.Warehouse.Positions where p.MapMode == MapMode.ShipMode select p).ToArray();
     var storelocs = (from p in SourceData.Warehouse.Positions where p.MapMode == MapMode.StorageMode select p).ToArray();
     var chrlocs = (from p in SourceData.Warehouse.Positions where p.MapMode == MapMode.ChargeMode select p).ToArray();
     var total = ((chrlocs.Length*reclocs.Length)*2) + ((chrlocs.Length*shiplocs.Length)*2) +
                 ((chrlocs.Length*storelocs.Length)*2) + (reclocs.Length*storelocs.Length) + (storelocs.Length*shiplocs.Length);
     Progress.Maximum = total;
     Progress.Value = 0;
     foreach (var cl in chrlocs)
     {
         SourceLocation = new Position(cl.X, cl.Y);
         foreach (var l in reclocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
         foreach (var l in shiplocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
         foreach (var l in storelocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
     }
     foreach (var cl in reclocs)
     {
         SourceLocation = new Position(cl.X, cl.Y);
         foreach (var l in chrlocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
         foreach (var l in storelocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
     }
     foreach (var cl in storelocs)
     {
         SourceLocation = new Position(cl.X, cl.Y);
         foreach (var l in chrlocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
         foreach (var l in shiplocs.ToArray())
         {
             DestLocation = new Position(l.X, l.Y);
             BuildNetwork(cl.X, cl.Y, l.X, l.Y);
             Progress.Value += 1;
             txtProgress.Text = Progress.Value.ToString() + "/" + total.ToString();
             DoEvents();
         }
     }
 }
Esempio n. 8
0
 private void UpdateHeading()
 {
     var src = new Position(Position[0], Position[1]);
     var dest = new Position(Destination[0], Destination[1]);
     var calc = new PositionBearingCalculator(new AngleConverter());
     Heading = calc.CalculateBearing(src, dest);
 }
Esempio n. 9
0
 protected virtual void OnPositionChanged1(Position args)
 {
     OnPositionChanged handler = PositionChanged;
     if (handler != null) handler(this, args);
 }