예제 #1
0
        /// <summary>
        /// Executes the antWorld test/benchmark
        /// </summary>
        /// <param name="n">The number of doAntWorld rounds to execute</param>
        static void AlternativeOne(int n)
        {
            int startTime = Environment.TickCount;

            AntWorld_NoGammel graph = new AntWorld_NoGammel();
            AntWorld_ExtendAtEndOfRound_NoGammelActions actions = new AntWorld_ExtendAtEndOfRound_NoGammelActions(graph);

            Action_InitWorld initWorld = Action_InitWorld.Instance;
            IAnt             firstAnt  = null;

            initWorld.Apply(graph, ref firstAnt);

            int endTime = Environment.TickCount;

            PrintResults(endTime - startTime, graph);

            for (int i = 0; i < n; ++i)
            {
                Action_doAntWorld doAntWorld = Action_doAntWorld.Instance;
                doAntWorld.Apply(graph, firstAnt);

                endTime = Environment.TickCount;
                PrintResults(endTime - startTime, graph);
            }
        }
예제 #2
0
파일: Task.cs 프로젝트: shlee322/Netronics
 public Task(IAnt ant, int tid, int type, JToken msg)
 {
     _ant = ant;
     _tId = tid;
     _type = type;
     _message = msg;
 }
예제 #3
0
파일: Ants.cs 프로젝트: shlee322/Netronics
 public void JoinAnt(IAnt ant)
 {
     _ant[ant.GetId()] = ant;
     _connectAnt.Enqueue(ant.GetId());
     if (_maxAntId < ant.GetId())
         _maxAntId = ant.GetId();
 }
        /// <summary>
        /// Selects the index of the next node based on the "roulette wheel selection" principle.
        /// </summary>
        /// <param name="ant"></param>
        /// <returns>The index of the next node to visit.</returns>
        public int SelectNextNode(IAnt ant)
        {
            var choice = _problemData.ChoiceInfo(ant);
              var sumProbabilities = 0.0;

              // LINQ is not viable in this case due to the closure.  Performance
              // is increased significantly by using a basic for loop here instead.
              // ReSharper disable once LoopCanBeConvertedToQuery
              for (var i = 0; i < _problemData.NodeCount; i++)
              {
            var probability = ant.Visited[i] ? 0.0 : choice[ant.CurrentNode][i];
            _probabilities[i] = probability;
            sumProbabilities += probability;
              }

              var selectedProbability = _random.NextDouble() * sumProbabilities;
              var j = 0;
              var probabilitySum = _probabilities[j];

              // NextDouble returns a value in [0.0, 1.0), deal with the edge
              // case where it is zero or all probabilities of selection are zero;
              if (selectedProbability.Equals(0.0))
              {
            return Enumerable.Range(0, _probabilities.Length).First(i => !ant.Visited[i]);
              }

              while (probabilitySum < selectedProbability &&
             j < _probabilities.Length - 1)
              {
            j++;
            probabilitySum += _probabilities[j];
              }

              return j;
        }
예제 #5
0
        protected ICity Roulette(IAnt ant)
        {
            if (ant.Visited.Count - 1 == ant.CitiesToVisit)
            {
                return(ant.Colony.Destination);
            }

            if (TraverseLevel == 3)
            {
                ICountry country = GetCountryRouletteForAnt(ant).GetNext();

                IRegion region = country.Regions.FirstOrDefault().Value;

                ICity city = region.Cities.FirstOrDefault().Value;

                return(city);
            }
            if (TraverseLevel == 2)
            {
                IRegion region = GetRegionRouletteForAnt(ant).GetNext();

                ICity city = region.Cities.FirstOrDefault().Value;

                return(city);
            }

            return(GetCityRouletteForAnt(ant).GetNext());
        }
예제 #6
0
        public void MoveToNextCity(IAnt ant)
        {
            ICity city = Roulette(ant);

            ant.Visited.Add(city);
            ant.Current = city;
        }
예제 #7
0
파일: Test1.cs 프로젝트: shlee322/Netronics
 private IEnumerator<IYield> TestMethod1(IAnt ant = null)
 {
     yield return Microthread.Sleep(1);
     if(ant == null)
         ant = Ants.GetAnts("Test1").GetAnt();
     yield return ant.SendTask(0, new JValue(123));
     //System.Console.WriteLine("Result : " + Microthread.CurrentMicrothread.Result);
     Netronics.Scheduling.Scheduler.Default.RunMicrothread(0, new Microthread(() => TestMethod1()));
 }
예제 #8
0
        /// <summary>
        /// Initializes a new <see cref="AntDirectory"/>.
        /// </summary>
        /// <param name="ant">Ant pattern used for directory-searching.</param>
        /// <param name="fileSystem">File system to be used</param>
        /// <exception cref="ArgumentNullException">Throw when <paramref name="ant"/> is null.</exception>
        public AntDirectory(IAnt ant, IFileSystem fileSystem = null)
        {
            if (ant == null)
            {
                throw new ArgumentNullException(nameof(ant));
            }

            this.ant        = ant;
            this.fileSystem = fileSystem ?? new FileSystem();
        }
예제 #9
0
        protected IRoulette <IRegion> GetRegionRouletteForAnt(IAnt ant)
        {
            IRegion currentRegion = ant.Current.Region;

            IRoulette <IRegion> regionRoulette;

            if (regionRoulettes.TryGetValue(currentRegion, out regionRoulette))
            {
                return(regionRoulette);
            }

            return(CreateRegionRouletteForRegion(currentRegion));
        }
예제 #10
0
        protected IRoulette <ICity> GetCityRouletteForAnt(IAnt ant)
        {
            ICity currentCity = ant.Current;

            IRoulette <ICity> cityRoulette;

            if (cityRoulettes.TryGetValue(currentCity, out cityRoulette))
            {
                return(cityRoulette);
            }

            return(CreateCityRouletteForCity(currentCity));
        }
예제 #11
0
        static void Main(string[] args)
        {
            WorldCitiesParser parser = new WorldCitiesParser();

            Console.WriteLine("MultiThreaded Parser - Starting");
            IDatabase database = parser.ParseWorldCitiesPopDb("./Data/worldcitiespop.txt");

            Console.WriteLine("MultiThreaded Parser - Completed");

            Console.WriteLine($"----------------------------------------");
            Console.WriteLine($"Stats: ");
            Console.WriteLine($"Countries: {database.Countries.Count}");
            Console.WriteLine($"Regions: {database.Regions.Count}");
            Console.WriteLine($"Cities: {database.Cities.Count}");

            Console.WriteLine($"----------------------------------------");
            Console.WriteLine($"Trip info: ");
            ICity startCity = database.Cities.First(city => city.Name == "oslo");

            Console.WriteLine($"Start City: {startCity.Name}");

            ICity endCity = database.Cities.First(city => city.Name == "london");

            Console.WriteLine($"Destination City: {endCity.Name}");
            Console.WriteLine($"Cities to visit in total: {CitiesToVisit}");

            Console.WriteLine($"----------------------------------------");
            Console.WriteLine($"Creating colony with {Ants} ants");
            IColony colony = new Colony.Colony();

            MMAS algorithm = new MMAS();

            algorithm.Database = database;
            colony.Algorithm   = algorithm;

            colony.Start         = startCity;
            colony.Destination   = endCity;
            colony.CitiesToVisit = CitiesToVisit;
            colony.CreateAnts(Ants);

            Console.WriteLine($"Colony created!");

            Console.WriteLine($"----------------------------------------");
            Console.WriteLine($"Starting the walk");

            IAnt bestAnt = colony.TraverseUntilConvergence();

            Console.WriteLine($"Walk done");
            Console.WriteLine($"----------------------------------------");
            Console.WriteLine($"Best ant {bestAnt.Name}");
        }
예제 #12
0
        protected IRoulette <ICountry> GetCountryRouletteForAnt(IAnt ant)
        {
            ICity currentCity = ant.Current;

            ICountry currentCountry = currentCity.Country;

            IRoulette <ICountry> countryRoulette;

            if (countryRoulettes.TryGetValue(currentCountry, out countryRoulette))
            {
                return(countryRoulette);
            }

            return(CreateCountryRouletteForCountry(currentCountry));
        }
예제 #13
0
        public void AntDoneWalking(IAnt ant)
        {
            double antPerformance = AntPerformanceCalculator.CalculateAntPerformance(ant);

            if (antPerformance <= BestAntPerformance)
            {
                UnchangedBestCycleCounter = 0;
                BestAnt = ant;
                return;
            }

            UnchangedBestCycleCounter += 1;

            DetermineTraverseLevel();
        }
예제 #14
0
        /// <summary>
        /// Executes the antWorld test/benchmark
        /// </summary>
        /// <param name="n">The number of doAntWorld rounds to execute</param>
        static void AlternativeTwo(int n)
        {
            int startTime = Environment.TickCount;

            AntWorld_NoGammel graph = new AntWorld_NoGammel();
            AntWorld_ExtendAtEndOfRound_NoGammelActions actions = new AntWorld_ExtendAtEndOfRound_NoGammelActions(graph);

            Action_InitWorld initWorld = Action_InitWorld.Instance;
            IAnt             firstAnt  = null;

            initWorld.Apply(graph, ref firstAnt);

            // TODO

            int endTime = Environment.TickCount;

            PrintResults(endTime - startTime, graph);
        }
        public virtual void Fire()
        {
            if (thresholdReached)
            {
                for (int i = 0; i < numberOfAntsToFire; i++)
                {
                    IAnt ant = ants[0];
                    ants.RemoveAt(0);

                    IConnection connection = ChooseConnection();
                    connection.Neuron.AddAnt(ant);
                    ant.AddConnectionToHistory(connection);
                    ant.Sign *= Sign.GetSign(connection.Weight);
                }

                thresholdReached   = false;
                numberOfAntsToFire = 0;
            }
        }
예제 #16
0
        /// <summary>
        /// Initializes a new <see cref="FusionBuilder"/> with all it's dependencies.
        /// </summary>
        /// <param name="logService">Service used for logging.</param>
        /// <param name="antFactory">Factory used to create <see cref="IAnt"/> instances.</param>
        /// <param name="configStore">Store that resolved app-level configuration.</param>
        /// <param name="packService">Service that is used to get the packages from.</param>
        /// <param name="nuGetPackageResolver">Resolver that handles required NuGet packages.</param>
        /// <param name="frameworkPackageEntryFactory">Factory used for creating framework instances of <see cref="IPackageEntry"/>.</param>
        /// <param name="fusionFilters">A collection of filters used to filter instances of <see cref="IPackageEntry"/>.</param>
        /// <param name="fusionInterceptors">A collection of interceptors that intercept the creation of <see cref="IPackageEntry"/>.</param>
        public FusionBuilder(
            ILog logService,
            IAntFactory antFactory,
            IConfigStore configStore,
            IPackService packService,
            INuGetPackageResolver nuGetPackageResolver,
            IFrameworkPackageEntryFactory frameworkPackageEntryFactory,
            IEnumerable <IFusionFilter> fusionFilters,
            IEnumerable <IFusionInterceptor> fusionInterceptors)
        {
            this.logService                   = logService;
            this.configStore                  = configStore;
            this.packService                  = packService;
            this.nuGetPackageResolver         = nuGetPackageResolver;
            this.frameworkPackageEntryFactory = frameworkPackageEntryFactory;

            this.fusionFilters      = fusionFilters.StaleReadOnly();
            this.fusionInterceptors = fusionInterceptors.StaleReadOnly();

            entryFilter = antFactory
                          .CreateNew(configStore?.Value?.Fuse?.EntryPattern);

            dependencyFiles = GetDependencyFiles().StaleReadOnly();
        }
 public void AddAnt(IAnt ant)
 {
     ants.Add(ant);
 }
예제 #18
0
파일: Game.cs 프로젝트: kolman/LangtonsAnt
 public Game(IBoard board, IAnt ant)
 {
     _board = board;
     _ant = ant;
 }
예제 #19
0
 protected void Travel(IAnt ant)
 {
     while (true)
     {
         var allPossibleVertecies = _graph.GetSibilings(ant.VisitedVetecies.Last());
         foreach (var currentNode in ant.VisitedVetecies)
         {
             if (allPossibleVertecies.ContainsKey(currentNode))
             {
                 allPossibleVertecies.Remove(currentNode);
             }
         }
         var probes = allPossibleVertecies.Keys.ToDictionary(vertex => vertex,
             vertex => _prober.GetProb(allPossibleVertecies[vertex]));
         if (probes.Count == 0)
         {
             ant.VisitedVetecies.Add(ant.VisitedVetecies.First());
             return;
         }
         ant.VisitedVetecies.Add(_selectRule.Proccess(probes, _greedyRate));
     }
 }
 public override IReadOnlyList<IReadOnlyList<double>> ChoiceInfo(IAnt ant = null)
 {
     return _choiceInfo;
 }
예제 #21
0
 public void AntTravel(IAnt ant)
 {
     while (ant.VisitedNodes.Last() != ant.VisitedNodes.First() || ant.VisitedNodes.Count == 1)
     {
         ant.VisitedNodes.Add(GetNextNode(ant));
     }
 }
 /// <summary>
 /// Randomly selects the index of the next unvisited node to visit.
 /// </summary>
 /// <param name="ant"></param>
 public int SelectNextNode(IAnt ant)
 {
     var notVisited = Enumerable.Range(0, ant.Visited.Count).Where(n => !ant.Visited[n]).ToArray();
       var index = _random.Next(0, notVisited.Length);
       return notVisited[index];
 }
예제 #23
0
 protected void Travel(IAnt ant, IGraph input)
 {
     while (true)
     {
         var allPossibleVertecies = input.GetSibilings(ant.VisitedVetecies.Last());
         foreach (var currentNode in ant.VisitedVetecies)
         {
             if (allPossibleVertecies.ContainsKey(currentNode))
             {
                 allPossibleVertecies.Remove(currentNode);
             }
         }
         var probes = allPossibleVertecies.Keys.ToDictionary(vertex => vertex,
             vertex => Prober.GetProb(allPossibleVertecies[vertex]));
         if (probes.Count == 0)
         {
             ant.VisitedVetecies.Add(ant.VisitedVetecies.First());
             return;
         }
         ant.VisitedVetecies.Add(Rules[ant.GetType()].Proccess(probes));
     }
 }
 public static double CalculateAntPerformance(IAnt ant)
 {
     return(0.0);
 }
 public abstract IReadOnlyList<IReadOnlyList<double>> ChoiceInfo(IAnt ant = null);
 /// <summary>
 /// Selects the index of the nearest next node to visit.
 /// </summary>
 /// <param name="ant"></param>
 public int SelectNextNode(IAnt ant)
 {
     var nearestNeighbours = _problemData.NearestNeighbours(ant.CurrentNode);
       return nearestNeighbours.First(neighbour => !ant.Visited[neighbour]);
 }