Skip to content

Commit

Permalink
Changed SelectTarget to actually use suitable target. Saw explosion o…
Browse files Browse the repository at this point in the history
…f population as result.
  • Loading branch information
bdullemond committed Oct 8, 2010
1 parent ff9eb19 commit d36f6f0
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions Cas/Core/SimulationBase.cs
Expand Up @@ -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)
{
Expand All @@ -44,7 +44,7 @@ protected SimulationBase()
}

#region ISimulation Members

public event EventHandler GenerationStarted;
public event EventHandler GenerationFinished;

Expand Down Expand Up @@ -82,7 +82,7 @@ protected set
/// <summary>
/// A catalog of all unique species in the simulation.
/// </summary>
public List<ISpecies> Species
public List<ISpecies> Species
{
get
{
Expand Down Expand Up @@ -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; }

/// <summary>
Expand Down Expand Up @@ -290,7 +290,7 @@ private void ProcessLocation(ILocation location, List<KeyValuePair<IAgent, ILoca
if (pendingMigrations == null) throw new ArgumentNullException("pendingMigrations");

// Iterate all agents and have them interact with something (agent/resource node) or migrate (if healthy enough)
int interactionsToPerform = (int)(location.Agents.Count*InteractionsPerGenerationFactor);
int interactionsToPerform = (int)(location.Agents.Count * InteractionsPerGenerationFactor);
DoInteractions(location, interactionsToPerform);

// Prior to upkeep, select agents for migration
Expand Down Expand Up @@ -375,7 +375,7 @@ private void DoInteractions(ILocation location, int interactionsToPerform)
}

protected abstract void InnerDoInteraction(ILocation location, IAgent actor, IInteractable target);

private void DoReproduction(ILocation location, List<IAgent> breeders)
{
if (location == null) throw new ArgumentNullException("location");
Expand Down Expand Up @@ -432,11 +432,11 @@ private void QueueForMigration(ILocation location, List<KeyValuePair<IAgent, ILo
protected virtual double CalculateMigrationChance(IAgent agent)
{
double percentFull = Math.Max(1, agent.CurrentResourceCount / agent.Size);

return MigrationBaseChance + ((1 - percentFull) * MaximumMigrationBonus);
}

public void AddEventToAgent(IAgent agent, IEvent newEvent)
public void AddEventToAgent(IAgent agent, IEvent newEvent)
{
if (agent == null) throw new ArgumentNullException("agent");
if (newEvent == null) throw new ArgumentNullException("newEvent");
Expand All @@ -451,18 +451,21 @@ public void AddEventToAgent(IAgent agent, IEvent newEvent)
protected IInteractable SelectTarget(List<IInteractable> 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;
}
}

/// <summary>
/// Retrieves a random target from the supplied list, ensuring that it
Expand All @@ -471,11 +474,11 @@ protected IInteractable SelectTarget(List<IInteractable> allTargets, IAgent acto
protected static TBase SelectRandomTarget<TBase, TSpecific>(List<TBase> 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;
}
Expand Down Expand Up @@ -533,7 +536,7 @@ protected static bool CalculateConditionalExchangeMatch(Tag exchange, Tag offens

return true;
}

#endregion

#region IDisposable Members
Expand Down

0 comments on commit d36f6f0

Please sign in to comment.