/// <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); } }
public Task(IAnt ant, int tid, int type, JToken msg) { _ant = ant; _tId = tid; _type = type; _message = msg; }
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; }
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()); }
public void MoveToNextCity(IAnt ant) { ICity city = Roulette(ant); ant.Visited.Add(city); ant.Current = city; }
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())); }
/// <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(); }
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)); }
protected IRoulette <ICity> GetCityRouletteForAnt(IAnt ant) { ICity currentCity = ant.Current; IRoulette <ICity> cityRoulette; if (cityRoulettes.TryGetValue(currentCity, out cityRoulette)) { return(cityRoulette); } return(CreateCityRouletteForCity(currentCity)); }
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}"); }
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)); }
public void AntDoneWalking(IAnt ant) { double antPerformance = AntPerformanceCalculator.CalculateAntPerformance(ant); if (antPerformance <= BestAntPerformance) { UnchangedBestCycleCounter = 0; BestAnt = ant; return; } UnchangedBestCycleCounter += 1; DetermineTraverseLevel(); }
/// <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; } }
/// <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); }
public Game(IBoard board, IAnt ant) { _board = board; _ant = ant; }
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; }
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]; }
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]); }