From d36f6f02fb38978bb59896cdbf26fa60565ec855 Mon Sep 17 00:00:00 2001 From: bdullemond Date: Fri, 8 Oct 2010 15:33:04 -0400 Subject: [PATCH] Changed SelectTarget to actually use suitable target. Saw explosion of population as result. --- Cas/Core/SimulationBase.cs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Cas/Core/SimulationBase.cs b/Cas/Core/SimulationBase.cs index fb7fed9..5382a29 100644 --- a/Cas/Core/SimulationBase.cs +++ b/Cas/Core/SimulationBase.cs @@ -20,10 +20,10 @@ public abstract class SimulationBase : ISimulation #endregion - protected SimulationBase() + protected SimulationBase() : this(1.5, 4, 0.25, 1.75, 0.2, 0.005, 0.02, 0.005, 5) { } - protected SimulationBase(double interactionsPerGenerationFactor, int maximumUpkeepCostPerLocation, double upkeepChance, + protected SimulationBase(double interactionsPerGenerationFactor, int maximumUpkeepCostPerLocation, double upkeepChance, double reproductionThreshold, double reproductionInheritance, double migrationBaseChance, double maxMigrationBonus, double randomDeathChance, int maximumAttemptsToFindSuitableTarget) { @@ -44,7 +44,7 @@ protected SimulationBase() } #region ISimulation Members - + public event EventHandler GenerationStarted; public event EventHandler GenerationFinished; @@ -82,7 +82,7 @@ protected set /// /// A catalog of all unique species in the simulation. /// - public List Species + public List Species { get { @@ -153,7 +153,7 @@ public IIsUnique GetSpeciesOrFossil(long id) var species = this.Species.Where(s => s.Id == id).FirstOrDefault(); return species ?? (IIsUnique)new Fossil(id); } - + public int CurrentGeneration { get; protected set; } /// @@ -290,7 +290,7 @@ private void ProcessLocation(ILocation location, List breeders) { if (location == null) throw new ArgumentNullException("location"); @@ -432,11 +432,11 @@ private void QueueForMigration(ILocation location, List allTargets, IAgent actor) { if (allTargets == null) throw new ArgumentNullException("allTargets"); - if (allTargets == null) throw new ArgumentNullException("actor"); + if (actor == null) throw new ArgumentNullException("actor"); IInteractable target = null; for (int i = 0; i < MaximumAttemptsToFindSuitableTarget; i++) { target = SelectRandomTarget(allTargets, actor); - if (target != null && target is IAgent) (target as IAgent).SetInteractionContactPoint(); - if (target == null || !ShouldExchangeOccur(actor, target)) continue; + if (target == null) continue; + + if (target is IAgent) (target as IAgent).SetInteractionContactPoint(); + + if (ShouldExchangeOccur(actor, target)) break; } - + return target; - } + } /// /// Retrieves a random target from the supplied list, ensuring that it @@ -471,11 +474,11 @@ protected IInteractable SelectTarget(List allTargets, IAgent acto protected static TBase SelectRandomTarget(List allTargets, TSpecific actor) where TBase : class { if (allTargets == null) throw new ArgumentNullException("allTargets"); - if (allTargets == null) throw new ArgumentNullException("actor"); + if (actor == null) throw new ArgumentNullException("actor"); TBase target = null; - if (allTargets.Count == 0 || (allTargets.Count == 1 && Object.ReferenceEquals(allTargets[0],actor))) + if (allTargets.Count == 0 || (allTargets.Count == 1 && Object.ReferenceEquals(allTargets[0], actor))) { return null; } @@ -533,7 +536,7 @@ protected static bool CalculateConditionalExchangeMatch(Tag exchange, Tag offens return true; } - + #endregion #region IDisposable Members