Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/auto-evo/AutoEvoExploringTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ private void PatchListMenuIndexChanged(int index)
var selectedPatch = world.GameProperties.GameWorld.Map.Patches.Values
.First(p => p.Name.ToString() == patchName);

// Get current snapshot
// Get the current snapshot
var patch = new Patch(selectedPatch.Name, 0, selectedPatch.BiomeTemplate, selectedPatch.BiomeType,
world.PatchHistoryList[generationDisplayed][selectedPatch.ID], selectedPatch.DynamicDataSeed)
{
Expand All @@ -788,10 +788,10 @@ private void PatchListMenuUpdate(Patch patch)
var cache = new SimulationCache(world.WorldSettings);
var globalCache = new AutoEvoGlobalCache(world.WorldSettings);

var generateMiche = new GenerateMiche(patch, cache, globalCache);
var generateMiche = new GenerateMiche(patch, globalCache);
var newMiche = generateMiche.GenerateMicheTree(globalCache);

generateMiche.PopulateMiche(newMiche);
generateMiche.PopulateMiche(newMiche, cache);

micheTree.SetMiche(newMiche);
}
Expand Down
48 changes: 35 additions & 13 deletions src/auto-evo/AutoEvoRun.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -29,6 +30,8 @@ public class AutoEvoRun

private readonly List<Task> concurrentStepTasks = new();

private readonly ConcurrentStack<SimulationCache> simulationCaches = new();

private volatile RunStage state = RunStage.GatheringInfo;

private bool started;
Expand Down Expand Up @@ -398,11 +401,9 @@ protected virtual void GatherInfo(Queue<IRunStep> steps)

var allSpecies = new HashSet<Species>();

var generateMicheCache = new SimulationCache(worldSettings);

foreach (var entry in map.Patches)
{
steps.Enqueue(new GenerateMiche(entry.Value, generateMicheCache, globalCache));
steps.Enqueue(new GenerateMiche(entry.Value, globalCache));

foreach (var species in entry.Value.SpeciesInPatch)
{
Expand All @@ -412,13 +413,12 @@ protected virtual void GatherInfo(Queue<IRunStep> steps)

foreach (var entry in map.Patches)
{
steps.Enqueue(new ModifyExistingSpecies(entry.Value, new SimulationCache(worldSettings), worldSettings,
random));
steps.Enqueue(new ModifyExistingSpecies(entry.Value, worldSettings, random));
}

foreach (var species in allSpecies)
{
steps.Enqueue(new MigrateSpecies(species, map, worldSettings, new SimulationCache(worldSettings), random));
steps.Enqueue(new MigrateSpecies(species, map, worldSettings, random));
}

// The new populations don't depend on the mutations but will take into account changes in the miche tree.
Expand All @@ -436,7 +436,7 @@ protected virtual void GatherInfo(Queue<IRunStep> steps)
}

/// <summary>
/// Adds a step that adjusts the player species population results
/// Adds a step that adjusts the player species' population results
/// </summary>
/// <param name="steps">The list of steps to add the adjustment step to</param>
/// <param name="map">Used to get a list of patches to act on</param>
Expand Down Expand Up @@ -486,17 +486,17 @@ protected void AddPlayerSpeciesPopulationChangeClampStep(Queue<IRunStep> steps,
/// <summary>
/// Single step run wrapper that handles checking timing if needed
/// </summary>
/// <returns>Returns true when step is complete and can be discarded</returns>
/// <returns>Returns true when the step is complete and can be discarded</returns>
[SuppressMessage("ReSharper", "HeuristicUnreachableCode", Justification = "False positive due to Constant bool")]
private static bool RunSingleStep(IRunStep step, RunResults results)
private static bool RunSingleStep(IRunStep step, RunResults results, SimulationCache cache)
{
DateTime startTime;

#pragma warning disable CS0162 // Unreachable code detected
if (Constants.AUTO_EVO_TRACK_STEP_TIME)
startTime = DateTime.Now;

var result = step.RunStep(results);
var result = step.RunStep(results, cache);

if (Constants.AUTO_EVO_TRACK_STEP_TIME)
{
Expand Down Expand Up @@ -595,7 +595,7 @@ private bool Step()

if (concurrentStepTasks.Count < 1)
{
// No steps that can run concurrently, need to run just a normal run
// No steps that can run concurrently need to run just a normal run
NormalRunPartOfNextStep();
}
else
Expand Down Expand Up @@ -627,27 +627,49 @@ private bool Step()

private void NormalRunPartOfNextStep()
{
if (RunSingleStep(runSteps.Peek(), results))
var cache = GetCache();
if (RunSingleStep(runSteps.Peek(), results, cache))
runSteps.Dequeue();

Interlocked.Increment(ref completeSteps);

ReturnCache(cache);
}

private void RunSingleStepToCompletion(IRunStep step)
{
int steps = 0;

var cache = GetCache();

// This condition is here to allow abandoning auto-evo runs quickly
while (!Aborted)
{
++steps;

if (RunSingleStep(step, results))
if (RunSingleStep(step, results, cache))
break;
}

// Doing the steps counting this way is slightly faster than an increment after each step
Interlocked.Add(ref completeSteps, steps);

ReturnCache(cache);
}

private SimulationCache GetCache()
{
if (simulationCaches.TryPop(out var cache))
return cache;

return new SimulationCache(Parameters.World.WorldSettings);
}

private void ReturnCache(SimulationCache cache)
{
cache.OnAfterUse();

simulationCaches.Push(cache);
}

private void UpdateMap(bool playerCantGoExtinct)
Expand Down
4 changes: 1 addition & 3 deletions src/auto-evo/EditorAutoEvoRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ protected override void GatherInfo(Queue<IRunStep> steps)
var map = Parameters.World.Map;
var worldSettings = Parameters.World.WorldSettings;

var generateMicheCache = new SimulationCache(worldSettings);

foreach (var entry in map.Patches)
{
steps.Enqueue(new GenerateMiche(entry.Value, generateMicheCache, globalCache));
steps.Enqueue(new GenerateMiche(entry.Value, globalCache));
}

var populationCalculation = new CalculatePopulation(configuration, worldSettings, map,
Expand Down
13 changes: 8 additions & 5 deletions src/auto-evo/IRunStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
public interface IRunStep
{
/// <summary>
/// Total number of steps. As step is called this is allowed to return lower values
/// Total number of steps. As <see cref="RunStep"/> is called, this is allowed to return lower values
/// than initially
/// </summary>
/// <value>The total steps.</value>
public int TotalSteps { get; }

/// <summary>
/// If true this step can be ran concurrently with other steps. If false all previous steps need to finish
/// before this can be ran.
/// If true, this step can be run concurrently with other steps. If false, all previous steps need to finish
/// before this can be run.
/// </summary>
public bool CanRunConcurrently { get; }

/// <summary>
/// Performs a single step. This needs to be called TotalSteps times
/// </summary>
/// <returns>True once final step is complete</returns>
/// <returns>True once the final step is complete</returns>
/// <param name="results">Results are stored here</param>
public bool RunStep(RunResults results);
/// <param name="cache">
/// Access to a cache where various auto-evo data can be got from efficiently.
/// </param>
public bool RunStep(RunResults results, SimulationCache cache);
}
13 changes: 8 additions & 5 deletions src/auto-evo/simulation/MichePopulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
/// </summary>
public static class MichePopulation
{
public static void Simulate(SimulationConfiguration parameters, SimulationCache? existingCache,
public static void Simulate(SimulationConfiguration parameters, ref SimulationCache? existingCache,
Random randomSource)
{
if (existingCache?.MatchesSettings(parameters.WorldSettings) == false)
throw new ArgumentException("Given cache doesn't match world settings");

// This only seems to help a bit, so caching entirely in an auto-evo task by adding the cache parameter
// to IRunStep.RunStep might not be worth the effort at all
var cache = existingCache ?? new SimulationCache(parameters.WorldSettings);
existingCache ??= new SimulationCache(parameters.WorldSettings);
Simulate(parameters, existingCache, randomSource);
}

public static void Simulate(SimulationConfiguration parameters, SimulationCache cache,
Random randomSource)
{
var insertWorkMemory = new Miche.InsertWorkingMemory();

var random = new XoShiRo256starstar(randomSource.NextInt64());
Expand All @@ -30,7 +33,7 @@ public static void Simulate(SimulationConfiguration parameters, SimulationCache?

IEnumerable<KeyValuePair<int, Patch>> patchesToSimulate = parameters.OriginalMap.Patches;

// Skip patches not configured to be simulated in order to run faster
// Skip patches that aren't configured to be simulated in order to run faster
if (parameters.PatchesToRun.Count > 0)
{
patchesToSimulate = patchesToSimulate.Where(p => parameters.PatchesToRun.Contains(p.Value));
Expand Down
14 changes: 8 additions & 6 deletions src/auto-evo/simulation/SimulationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ namespace AutoEvo;
/// caching is moved to a higher level in the auto-evo, that needs to be considered.
/// </para>
/// </remarks>
/// <remarks>
/// <para>
/// TODO: would be better to reuse instances of this class after clearing them for next use (there's now a Clear
/// method for this future usecase)
/// </para>
/// </remarks>
public class SimulationCache
{
private readonly CompoundDefinition oxytoxy = SimulationParameters.GetCompound(Compound.Oxytoxy);
Expand Down Expand Up @@ -1233,6 +1227,14 @@ public ResolvedMicrobeTolerances GetEnvironmentalTolerances(MicrobeSpecies speci
return result;
}

/// <summary>
/// Called after being used for an auto-evo step. This performs cleanup and readies this for the next use
/// </summary>
public void OnAfterUse()
{
Clear();
}

/// <summary>
/// Calculates cos of the angle between the organelle and vertical axis
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/auto-evo/steps/CalculatePopulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Xoshiro.PRNG64;

/// <summary>
/// Step that calculate the populations for all species
/// Step that calculates the populations for all species
/// </summary>
public class CalculatePopulation : IRunStep
{
Expand Down Expand Up @@ -34,7 +34,7 @@ public CalculatePopulation(IAutoEvoConfiguration configuration, WorldGenerationS
/// </summary>
public Dictionary<Patch, Species>? EnsurePatchesHaveSpecies { get; set; }

public bool RunStep(RunResults results)
public bool RunStep(RunResults results, SimulationCache cache)
{
// ReSharper disable RedundantArgumentDefaultValue
var config = new SimulationConfiguration(configuration, map, worldSettings)
Expand All @@ -58,7 +58,7 @@ public bool RunStep(RunResults results)

// TODO: allow passing in a random seed

MichePopulation.Simulate(config, null, new XoShiRo256starstar());
MichePopulation.Simulate(config, cache, new XoShiRo256starstar());

return true;
}
Expand Down
10 changes: 4 additions & 6 deletions src/auto-evo/steps/GenerateMiche.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@
public class GenerateMiche : IRunStep
{
private readonly Patch patch;
private readonly SimulationCache cache;
private readonly AutoEvoGlobalCache globalCache;

public GenerateMiche(Patch patch, SimulationCache cache, AutoEvoGlobalCache globalCache)
public GenerateMiche(Patch patch, AutoEvoGlobalCache globalCache)
{
this.patch = patch;
this.cache = cache;
this.globalCache = globalCache;
}

public int TotalSteps => 1;

public bool CanRunConcurrently => false;

public bool RunStep(RunResults results)
public bool RunStep(RunResults results, SimulationCache cache)
{
var generatedMiche = GenerateMicheTree(globalCache);
results.AddNewMicheForPatch(patch, PopulateMiche(generatedMiche));
results.AddNewMicheForPatch(patch, PopulateMiche(generatedMiche, cache));

return true;
}
Expand Down Expand Up @@ -193,7 +191,7 @@ public Miche GenerateMicheTree(AutoEvoGlobalCache globalCache)
return rootMiche;
}

public Miche PopulateMiche(Miche miche)
public Miche PopulateMiche(Miche miche, SimulationCache cache)
{
// If no species, don't need to do anything
if (patch.SpeciesInPatch.Count < 1)
Expand Down
2 changes: 1 addition & 1 deletion src/auto-evo/steps/LambdaStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LambdaStep(Action<RunResults> operation)
/// </summary>
public bool CanRunConcurrently { get; set; }

public bool RunStep(RunResults results)
public bool RunStep(RunResults results, SimulationCache cache)
{
operation(results);
return true;
Expand Down
8 changes: 3 additions & 5 deletions src/auto-evo/steps/MigrateSpecies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public class MigrateSpecies : IRunStep
private readonly Species species;
private readonly PatchMap map;
private readonly WorldGenerationSettings worldSettings;
private readonly SimulationCache cache;

private readonly Random random;
private readonly Miche.InsertWorkingMemory insertWorkingMemory = new();

public MigrateSpecies(Species species, PatchMap map, WorldGenerationSettings worldSettings, SimulationCache cache,
Random randomSource)
public MigrateSpecies(Species species, PatchMap map, WorldGenerationSettings worldSettings, Random randomSource)
{
this.species = species;
this.cache = cache;
this.map = map;
this.worldSettings = worldSettings;

Expand All @@ -31,7 +29,7 @@ public MigrateSpecies(Species species, PatchMap map, WorldGenerationSettings wor

public bool CanRunConcurrently => true;

public bool RunStep(RunResults results)
public bool RunStep(RunResults results, SimulationCache cache)
{
// Player has a separate GUI to control their migrations purposefully so auto-evo doesn't do it automatically
if (species.PlayerSpecies)
Expand Down
Loading