diff --git a/M4MCode/M4M_MkI/M4M.Old.sln b/M4MCode/M4M_MkI/M4M.Old.sln deleted file mode 100644 index 3c0721bef34b58b3297fdb8f528f25db8d082478..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old.sln +++ /dev/null @@ -1,137 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26430.16 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M4M", "M4M\M4M.csproj", "{B6E4567C-558D-44D1-9CDC-FD4472E785E6}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.Model", "M4M.Model\M4M.Model.csproj", "{6A470149-D199-4B60-9F76-0F01FBD587E0}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M4M.Old", "M4M.Old\M4M.Old.csproj", "{8B27E40D-9DB6-4179-A2BD-E9DE907509B0}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.New", "M4M.New\M4M.New.csproj", "{1E7D2BDD-30E8-48F8-9636-AF93F451EC38}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.CoreRunner", "M4M.CoreRunner\M4M.CoreRunner.csproj", "{9F765166-0377-446C-8A23-3D5E08DC51D5}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - Release2|Any CPU = Release2|Any CPU - Release2|x64 = Release2|x64 - Release3|Any CPU = Release3|Any CPU - Release3|x64 = Release3|x64 - Release4|Any CPU = Release4|Any CPU - Release4|x64 = Release4|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Debug|x64.ActiveCfg = Debug|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Debug|x64.Build.0 = Debug|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release|Any CPU.Build.0 = Release|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release|x64.ActiveCfg = Release|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release|x64.Build.0 = Release|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release2|Any CPU.ActiveCfg = Release2|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release2|Any CPU.Build.0 = Release2|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release2|x64.ActiveCfg = Release2|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release2|x64.Build.0 = Release2|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release3|Any CPU.ActiveCfg = Release3|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release3|Any CPU.Build.0 = Release3|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release3|x64.ActiveCfg = Release3|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release3|x64.Build.0 = Release3|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release4|Any CPU.ActiveCfg = Release4|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release4|Any CPU.Build.0 = Release4|Any CPU - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release4|x64.ActiveCfg = Release4|x64 - {B6E4567C-558D-44D1-9CDC-FD4472E785E6}.Release4|x64.Build.0 = Release4|x64 - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Debug|x64.ActiveCfg = Debug|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Debug|x64.Build.0 = Debug|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release|Any CPU.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release|x64.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release|x64.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release2|Any CPU.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release2|Any CPU.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release2|x64.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release2|x64.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release3|Any CPU.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release3|Any CPU.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release3|x64.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release3|x64.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|Any CPU.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|Any CPU.Build.0 = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|x64.ActiveCfg = Release|Any CPU - {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|x64.ActiveCfg = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|x64.Build.0 = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|x64.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|x64.ActiveCfg = Debug|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|x64.Build.0 = Debug|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release|Any CPU.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release|x64.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release|x64.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release2|Any CPU.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release2|Any CPU.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release2|x64.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release2|x64.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release3|Any CPU.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release3|Any CPU.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release3|x64.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release3|x64.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release4|Any CPU.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release4|Any CPU.Build.0 = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release4|x64.ActiveCfg = Release|Any CPU - {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Release4|x64.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Debug|x64.ActiveCfg = Debug|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Debug|x64.Build.0 = Debug|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release|Any CPU.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release|x64.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release|x64.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release2|Any CPU.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release2|Any CPU.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release2|x64.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release2|x64.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release3|Any CPU.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release3|Any CPU.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release3|x64.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release3|x64.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release4|Any CPU.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release4|Any CPU.Build.0 = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release4|x64.ActiveCfg = Release|Any CPU - {9F765166-0377-446C-8A23-3D5E08DC51D5}.Release4|x64.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {24F33315-4D5B-43D3-93D4-9B4D6A603384} - EndGlobalSection -EndGlobal diff --git a/M4MCode/M4M_MkI/M4M.Old/Analysis.cs b/M4MCode/M4M_MkI/M4M.Old/Analysis.cs deleted file mode 100644 index 7ba23694d596f220ccf49ec076155e8bbdb450d9..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Analysis.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Linear = MathNet.Numerics.LinearAlgebra; // reasonable quality API reference here: https://numerics.mathdotnet.com/api/MathNet.Numerics.LinearAlgebra/ (inline isn't good enogh) -using RandomSource = MathNet.Numerics.Random.RandomSource; -using static M4M.Misc; -using static M4M.Analysis; -using System.Diagnostics; -using MathNet.Numerics.Random; -using OxyPlot; - -namespace M4M -{ - public class PdfPlotExporter : IPlotExporter - { - public static PlotExportReport Translate(Plotting.PdfExportReport report) - { - return new PlotExportReport(report.Filename, report.ExportedWidth, report.ExportedHeight); - } - - public PlotExportReport ExportPlot(string filename, PlotModel model, double size, bool forceAspect, PostProcessPlot postProcessor = null) - { - return Translate(Plotting.OldOxyPlotting.ExportToPdf(model, filename + ".pdf", size, true, forceAspect, postProcessor)); - } - - public PlotExportReport ExportPlot(string filename, PlotModel model, double width, double height, bool forceAspect, PostProcessPlot postProcessor = null) - { - return Translate(Plotting.OldOxyPlotting.ExportToPdf(model, filename + ".pdf", width, height, true, forceAspect, postProcessor)); - } - } - - public class MutantAnalysis - { - public GenomeInfo<DenseGenome> Source { get; } - public IReadOnlyList<GenomeInfo<DenseGenome>> MutantInfos { get; } - - public ReproductionRules ReproductionRules { get; } - public DevelopmentRules DevelopmentRules { get; } - public JudgementRules JudgementRules { get; } - public ITarget Target { get; } - - public double[] FitnessChanges { get; } - - /// <summary> - /// makes a copy of the genome, so you don't have to worry about it - /// </summary> - public MutantAnalysis(DenseGenome genome, int mutantCount, ModelExecutionContext context, ReproductionRules rrules, DevelopmentRules drules, JudgementRules jrules, ITarget target) - { - // defensive clone (insiginificant cost in terms of everything else) - genome = genome.Clone(context); - - ReproductionRules = rrules; - DevelopmentRules = drules; - JudgementRules = jrules; - Target = target; - - Source = GenomeInfo<DenseGenome>.Process(genome, context, rrules, drules, jrules, target); - MutantInfos = Enumerable.Range(0, mutantCount).Select(i => GenomeInfo<DenseGenome>.Process(genome.Mutate(context, rrules), context, rrules, drules, jrules, target)).ToArray(); - - FitnessChanges = MutantInfos.Select(mi => mi.Judgement.CombinedFitness - Source.Judgement.CombinedFitness).ToArray(); - } - - public void ComputeFitnessChangePercentiles(double[] positions, ref double[] reusableBuffer, ref Percentile[] percentiles) - { - Percentile.ComputePercentiles(positions, FitnessChanges, ref reusableBuffer, ref percentiles); - } - - public void Plot(string outDir, string filePrefix, string prefix) - { - EnsureDirectory(outDir); - - string file(string name) => System.IO.Path.Combine(outDir, filePrefix + name); - - { // histogram of mutant fitness - OxyPlot.PlotModel model = Plotting.OldOxyPlotting.LinearAxes(prefix + " Mutation Fitness Variation", "Fitness Delta", "Frequency"); - OxyPlot.Series.HistogramSeries hs = new OxyPlot.Series.HistogramSeries() { Title = "Mutation Benefit Variation" }; - hs.ItemsSource = OxyPlot.HistogramHelpers.Collect(FitnessChanges, HistogramHelpers.CreateUniformBins(FitnessChanges.Min(), FitnessChanges.Max(), 100), new BinningOptions(BinningOutlierMode.RejectOutliers, BinningIntervalType.InclusiveLowerBound, BinningExtremeValueMode.IncludeExtremeValues)); - model.Series.Add(hs); - Plotting.OldOxyPlotting.ExportToPdf(model, file("mfv.pdf"), true, true); - } - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/DefaultDensePopulationFeedback.cs b/M4MCode/M4M_MkI/M4M.Old/DefaultDensePopulationFeedback.cs deleted file mode 100644 index bc9ce2b189d0cf3452418d9393bb9e4993c5e5d7..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/DefaultDensePopulationFeedback.cs +++ /dev/null @@ -1,190 +0,0 @@ -using MathNet.Numerics.Random; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static M4M.MiscPlotting; -using static M4M.Analysis; -using M4M.State; - -namespace M4M -{ - public static class OldPopulationTrace - { - /// <summary> - /// Runs a trace, and returns the terminal population - /// Does not modify the population passed to it - /// </summary> - /// <param name="rand">Random source</param> - /// <param name="stuff">File Stuff</param> - /// <param name="population">A template population (cloned before doing anything)</param> - /// <param name="populationConfig">The population configuration</param> - /// <param name="target">The target to trace to</param> - /// <returns>The cloned population after the trace</returns> - public static Population<DenseIndividual> RunTrace(ModelExecutionContext context, FileStuff stuff, Population<DenseIndividual> population, PopulationExperimentConfig<DenseIndividual> populationConfig, ITarget target, int exposureEpoch) - { - var rand = context.Rand; - - var populationSpinner = populationConfig.CustomPopulationSpinner ?? DefaultPopulationSpinner<DenseIndividual>.Instance; - - // clone population - population = population.Clone(); - - // perform a trace from one target (i.e. whatever was before) to another - ExperimentConfiguration config = populationConfig.ExperimentConfiguration; - int N = config.Size; - - List<double> traceFitness = new List<double>(); - List<IndividualJudgement<DenseIndividual>> traceMedianSamples = new List<IndividualJudgement<DenseIndividual>>(); - - void gjFeedback(int generation, IReadOnlyList<IndividualJudgement<DenseIndividual>> judgements) - { - traceFitness.Add(judgements.Average(ij => ij.Judgement.CombinedFitness)); - traceMedianSamples.Add(judgements.OrderBy(ij => ij.Judgement.CombinedFitness).Skip(judgements.Count / 2).First()); - } - - // spin it - ExposureInformation exposureInformation = new ExposureInformation(config.GenerationsPerTargetPerEpoch); - target.NextExposure(rand, populationConfig.ExperimentConfiguration.JudgementRules, exposureEpoch, ref exposureInformation); - if (exposureInformation.ExposureDuration < 0) - throw new Exception("ExposureDuration was negative"); - - populationSpinner.SpinPopulation(population, context, config.ReproductionRules, config.DevelopmentRules, config.JudgementRules, target, populationConfig.SelectorPreparer, exposureInformation.ExposureDuration, gjFeedback, populationConfig.EliteCount, populationConfig.HillclimberMode); - - PlotLine(stuff.TitlePrefix + "TrMeanFitness", stuff.File("trfitness"), traceFitness.ToArray(), 1, 1, "Generation", "Mean Fitness"); - - double[][] trPhenotypeState = Enumerable.Range(0, N).Select(i => traceMedianSamples.Select(ij => ij.Individual.Phenotype.Vector[i]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "Tr(Median)PhenotypicState", stuff.File("trpstate"), trPhenotypeState, 1, 1, "Generation", "Trait Expression"); - - double[][] trInitialState = Enumerable.Range(0, N).Select(i => traceMedianSamples.Select(ij => ij.Individual.Genome.InitialState[i]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "Tr(Median)InitialState", stuff.File("tristate"), trInitialState, 1, 1, "Generation", "Trait Expression"); - - double[][] trRcs = Enumerable.Range(0, N*N).Select(i => traceMedianSamples.Select(ij => ij.Individual.Genome.TransMat[i / N, i % N]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "Tr(Median)Rcs", stuff.File("trrcs"), trRcs, 1, 1, "Generation", "Regulation Coeficient"); - - return population; - } - } - - /// <summary> - /// Adds a lot of plotting stuff to the CommonDensePopulationExperimentFeedback - /// </summary> - public class DefaultDensePopulationExperimentFeedback : CommonDensePopulationExperimentFeedback - { - public static DensePopulationExperimentFeedbackFactory SimpleDefaultDensePopulationExperimentFeedback(bool disableTransientIO = false, bool disableAllIO = false, int sampleMax = 2000, int savePeriod = 200, int plotPeriod = 1000, int tracePeriod = 500, IPopulationResetOperation<DenseIndividual> preTraceResetOperation = null) - { - return (rand, popConfig, feedback) => new DefaultDensePopulationExperimentFeedback(rand, popConfig, disableTransientIO, disableAllIO, savePeriod, plotPeriod, tracePeriod, feedback, Math.Max(1, popConfig.ExperimentConfiguration.Epochs / sampleMax), preTraceResetOperation ?? popConfig.PopulationResetOperation); - } - - public DefaultDensePopulationExperimentFeedback(RandomSource rand, PopulationExperimentConfig<DenseIndividual> populationConfig, bool disableTransientIO, bool disableAllIO, int genomeSavePeriod, int partialPlotPeriod = 100, int tracePeriod = 100, PopulationExperimentFeedback<DenseIndividual> feedback = null, int samplePeriod = 1, IPopulationResetOperation<DenseIndividual> preTraceResetOperation = null) : base(System.Console.Out, rand, populationConfig, disableTransientIO, disableAllIO, genomeSavePeriod, partialPlotPeriod, feedback, samplePeriod) - { - ModelExecutionContext context = new ModelExecutionContext(rand); - - disableTransientIO = disableTransientIO | disableAllIO; - - ITarget lastTarget = null; - - Feedback.EndTarget.Register((stuff, population, target, epoch, generationCount, oldPopulationJudgements) => - { - // record last Target - lastTarget = target; - }); - - // feedback - Feedback.EndEpoch.Register((stuff, population, epoch, generationCount, opj) => - { - if (epoch % genomeSavePeriod == 0 || epoch == populationConfig.ExperimentConfiguration.Epochs) - { - DenseIndividual di = population.PeekRandom(rand); - DenseGenome g = di.Genome; - Phenotype p = di.Phenotype; - - if (!disableTransientIO) - { - // plot genome - PlotVectorFolded(stuff.TitlePrefix + "Phenotype " + epoch, stuff.File("p" + epoch + ".pdf"), p.Vector); - PlotMatrix(stuff.TitlePrefix + "Development Transformation Matrix " + epoch, stuff.File("dtm" + epoch + ".pdf"), g.TransMat); - PlotVectorFolded(stuff.TitlePrefix + "Intial State " + epoch, stuff.File("is" + epoch + ".pdf"), g.InitialState); - } - } - - if (epoch % partialPlotPeriod == 0) - { - if (!disableTransientIO) - { - PlotLine(stuff.TitlePrefix + "Fitness" + epoch, stuff.File("fitness" + epoch), _fitness.Take(epoch).ToArray(), 1, SamplePeriod, "Epoch", "Mean Fitness"); - - PlotRcs(stuff, epoch + "(best)"); - } - } - - if (epoch % tracePeriod == 0) - { - if (!disableTransientIO) - { - foreach (ITarget other in populationConfig.ExperimentConfiguration.Targets.Where(t => t != lastTarget)) - { - var tpop = population.Clone(); - preTraceResetOperation?.Reset(context, tpop, Config.InitialStateResetRange, Config.DevelopmentRules, Config.ReproductionRules, other); - OldPopulationTrace.RunTrace(context, stuff.CreateSubStuff((string)("tr" + epoch + "\\" + lastTarget.FriendlyName + "_" + other.FriendlyName)), tpop, populationConfig, other, epoch); - } - } - - //Target other = populationConfig.ExperimentConfiguration.Targets.FirstOrDefault(t => t != lastTarget); - //PopulationExperiment.RunTrace(rand, stuff.CreateSubStuff("tr" + epoch), population.Clone(), populationConfig, other); - } - }); - - Feedback.Finished.Register((stuff, population, epoch, generationCount) => - { - if (!disableAllIO) // we usually want the terminals - { - DenseIndividual di = population.PeekRandom(rand); - DenseGenome g = di.Genome; - Phenotype p = di.Phenotype; - - // plot individual - PlotVectorFolded(stuff.Title("Terminal Phenotype"), stuff.File("tp.pdf"), p.Vector); - PlotMatrix(stuff.Title("Terminal Development Transformation Matrix"), stuff.File("tdtm.pdf"), g.TransMat); - PlotVectorFolded(stuff.Title("Terminal Intial State"), stuff.File("tis.pdf"), g.InitialState); - - PlotLine(stuff.TitlePrefix + "Fitness", stuff.File("fitness"), _fitness.ToArray(), 1, SamplePeriod, "Epoch", "Mean Fitness"); - - PlotRcs(stuff, "_t(best)"); - - System.IO.File.WriteAllText(stuff.File("dtmDesc10.txt"), "Thresh10" + Environment.NewLine + Module.Describe(DtmClassification.DiscerneTrueModules(g.TransMat, g.TransMat.Enumerate().Max(e => Math.Abs(e)) / 10.0))); - System.IO.File.WriteAllText(stuff.File("dtmDesc50.txt"), "Thresh50" + Environment.NewLine + Module.Describe(DtmClassification.DiscerneTrueModules(g.TransMat, g.TransMat.Enumerate().Max(e => Math.Abs(e)) / 50.0))); - System.IO.File.WriteAllText(stuff.File("dtmDesc100.txt"), "Thresh100" + Environment.NewLine + Module.Describe(DtmClassification.DiscerneTrueModules(g.TransMat, g.TransMat.Enumerate().Max(e => Math.Abs(e)) / 100.0))); - - // perform trace to every target... to every target - foreach (ITarget other in populationConfig.ExperimentConfiguration.Targets.Where(t => t != lastTarget)) - { - var tpop = population.Clone(); - preTraceResetOperation?.Reset(context, tpop, Config.InitialStateResetRange, Config.DevelopmentRules, Config.ReproductionRules, other); - var traced = OldPopulationTrace.RunTrace(context, stuff.CreateSubStuff((string)("ttr" + lastTarget.FriendlyName + "_" + other.FriendlyName)), tpop, populationConfig, other, epoch); - - foreach (ITarget otherOther in populationConfig.ExperimentConfiguration.Targets) - { - var innterTpop = traced.Clone(); - preTraceResetOperation?.Reset(context, innterTpop, Config.InitialStateResetRange, Config.DevelopmentRules, Config.ReproductionRules, otherOther); - OldPopulationTrace.RunTrace(context, stuff.CreateSubStuff((string)("ttr" + other.FriendlyName + "_" + otherOther.FriendlyName)), innterTpop, populationConfig, otherOther, epoch); - } - } - } - }); - } - - public void PlotRcs(FileStuff stuff, string name) - { - double[][] rcs = GetRcsData(); - //PlotLines(stuff.Title(name), stuff.File("rcs" + name), rcs, 1, SamplePeriod, "Epoch", "Regulation Coeficient"); - PlotRcs(stuff.Title(name), stuff.File("rcs" + name), rcs, SamplePeriod); - } - - public static void PlotRcs(string title, string filename, double[][] rcs, int samplePeriod) - { - PlotLines(title, filename, rcs, 1, samplePeriod, "Epoch", "Regulation Coeficient"); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Epistatics/Epistatics.cs b/M4MCode/M4M_MkI/M4M.Old/Epistatics/Epistatics.cs deleted file mode 100644 index c0d1a25cf15f45d77c20f63ef05483f09e1572c4..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Epistatics/Epistatics.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Linear = MathNet.Numerics.LinearAlgebra; -using RandomSource = MathNet.Numerics.Random.RandomSource; -using static M4M.Misc; -using static M4M.MiscPlotting; -using static M4M.Analysis; -using System.Diagnostics; - -namespace M4M.Epistatics -{ - public class EpistaticStuff - { - public static Population<DenseIndividual> GeneralEpistaticExperiment(string friendlyName, EpistaticTargetPackage targetPackage, string superDir, RandomSource rand, RandomSource ioRand, bool disableTransientIO, double λ, double gNoiseTerm = 0.6, double bNoiseTerm = 0.00001, int K = 1000, int epochs = 100000, IRegularisationFunction<IGenome> regularisationFunction = null, IPopulationResetOperation<DenseIndividual> resetIndividualInitialStateOperation = null, IPopulationResetOperation<DenseIndividual> preTraceResetIndividualInitialStateOperation = null, ISquash squash = null, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null, int popSize = 1, int eliteCount = 1, bool hillclimberMode = true) - { - var p = EpistaticStuff.EpistaticPopulationTest( - friendlyName: friendlyName, - targetPackage: targetPackage, - λ: λ, - epigenetic: false, - epochs: epochs / targetPackage.Targets.Length, - K: K, - T: 10, - decayRate: 0.2, - paretoSelection: false, - g0: null, - gFixed: false, - gClamp: null, - gResetProb: 1.0, - gResetRange: new Misc.Range(-1, 1), - gNoiseTerm: gNoiseTerm, - gNoiseType: NoiseType.Gaussian, - bProb: 1, - b0: null, - bNoiseTerm: bNoiseTerm, - tracePeriod: 500, - squash: squash, - superDir: superDir, - gUpdateCount: 1, - popSize: popSize, - eliteCount: eliteCount, - hillclimberMode: hillclimberMode, - disableTransientIO: disableTransientIO, - rand: rand, - ioRand: ioRand, - regularisationFunction: regularisationFunction, - resetIndividualInitialStateOperation: resetIndividualInitialStateOperation, - preTraceResetIndividualInitialStateOperation: preTraceResetIndividualInitialStateOperation, - gOpenEntries: gOpenEntries, - bOpenEntries: bOpenEntries, - densePopulationExperimentFeedbackConfigurator: densePopulationExperimentFeedbackConfigurator); - return p; - } - - public static Population<DenseIndividual> EpistaticPopulationTest(string friendlyName, EpistaticTargetPackage targetPackage, double λ, bool epigenetic = false, double gNoiseTerm = 0.1, NoiseType gNoiseType = NoiseType.Uniform, int epochs = 5000, int K = 1000, int T = 10, double bProb = 1.0, Linear.Vector<double> g0 = null, bool gFixed = false, bool paretoSelection = false, Linear.Matrix<double> b0 = null, int popSize = 5, Range gClamp = null, int tracePeriod = 500, double decayRate = 0.2, ISquash squash = null, string superDir = "", double bNoiseTerm = 0.001, NoiseType bNoiseType = NoiseType.Uniform, int gUpdateCount = 1, ICycler targetCycler = null, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, double gResetProb = 0, Range gResetRange = null, IRegularisationFunction<IGenome> regularisationFunction = null, string customPrefix = null, int eliteCount = 0, bool disableTransientIO = false, bool disableAllIO = false, RandomSource rand = null, RandomSource ioRand = null, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null, IPopulationResetOperation<DenseIndividual> resetIndividualInitialStateOperation = null, IPopulationResetOperation<DenseIndividual> preTraceResetIndividualInitialStateOperation = null, bool hillclimberMode = false) - { - disableTransientIO = disableTransientIO | disableAllIO; - - ITarget[] targets = targetPackage.Targets; - - // config - int N = targetPackage.TargetSize; - - gClamp = gClamp ?? new Range(-1.0, 1.0); - regularisationFunction = regularisationFunction ?? JudgementRules.L1Equivalent; - resetIndividualInitialStateOperation = resetIndividualInitialStateOperation ?? new RandomIndependantPopulationResetOperation(); - preTraceResetIndividualInitialStateOperation = preTraceResetIndividualInitialStateOperation ?? new RandomIndependantPopulationResetOperation(); - - DevelopmentRules drules = new DevelopmentRules(T, 1.0, decayRate, squash ?? DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(gFixed ? 0.0 : (epigenetic ? 0.0 : gNoiseTerm), bNoiseTerm, bProb, false, gUpdateCount, gClamp, gNoiseType, bNoiseType); - JudgementRules jrules = new JudgementRules(λ, regularisationFunction, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, targetCycler ?? Cyclers.Loop, epochs, K, gResetProb, gResetRange, false, drules, rrules, jrules); - - ITransMatMutator mutator = null; - - // essentials - rand = rand ?? new MathNet.Numerics.Random.MersenneTwister(); - ioRand = ioRand ?? new MathNet.Numerics.Random.MersenneTwister(); - var context = new ModelExecutionContext(rand); - - g0 = g0 ?? Linear.CreateVector.Dense(N, 0.0); - b0 = b0 ?? Linear.CreateMatrix.Dense(N, N, 0.0); - - DenseIndividual CreateDefaultIndividual(ModelExecutionContext _context) - { - var customDevelopmentStep = Development.DefaultDevelopmentStep(N, epigenetic ? Math.Sqrt(gNoiseTerm) : 0.0); - var genome = new DenseGenome(g0, b0, gOpenEntries, bOpenEntries, mutator, null, null, null, customDevelopmentStep, null, false); - var individual = DenseIndividual.Develop(genome, _context, drules, epigenetic); - return individual; - } - - // population stuff - ISelectorPreparer<DenseIndividual> selector = paretoSelection - ? SelectorPreparers<DenseIndividual>.Pareto - : SelectorPreparers<DenseIndividual>.Ranked; - Population<DenseIndividual> population = new Population<DenseIndividual>(context, _context => CreateDefaultIndividual(_context), popSize); - PopulationExperimentConfig<DenseIndividual> popConfig = new PopulationExperimentConfig<DenseIndividual>(config, selector, eliteCount, hillclimberMode, null, resetIndividualInitialStateOperation, DefaultPopulationSpinner<DenseIndividual>.Instance); - DefaultDensePopulationExperimentFeedback denseFeedback = new DefaultDensePopulationExperimentFeedback(ioRand, popConfig, disableTransientIO, disableAllIO, Math.Max(1, epochs / 25), 500, tracePeriod, null, Math.Max(1, epochs / 1000), preTraceResetIndividualInitialStateOperation); - PopulationExperimentFeedback<DenseIndividual> feedback = denseFeedback.Feedback; - densePopulationExperimentFeedbackConfigurator?.Invoke(denseFeedback); - - // go - string expName = $"EpistaticPopTest{friendlyName}{targetPackage.Name}"; - string subName = "λ" + λ + "Epi" + epigenetic + "mG" + gNoiseTerm + "mB" + bNoiseTerm + "E" + epochs + "K" + K + "T" + T + "P" + (paretoSelection ? "T" : "F") + decayRate.ToString(".000") + "G" + gUpdateCount; - PopulationExperimentRunners.RunExperiment<DenseIndividual>(System.Console.Out, "", context, population, popConfig, outDir: System.IO.Path.Combine(superDir, expName + "_" + subName), disableTransientIO: disableTransientIO, disableAllIO: disableAllIO, feedback: feedback, filePrefix: "", titlePrefix: customPrefix ?? (subName + " ")); - return population; - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Evolvability.cs b/M4MCode/M4M_MkI/M4M.Old/Evolvability.cs deleted file mode 100644 index b5c952ee0286f5ac6ffe8a7e33c033698f6e6a49..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Evolvability.cs +++ /dev/null @@ -1,466 +0,0 @@ -using OxyPlot; -using OxyPlot.Axes; -using OxyPlot.Series; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using RandomSource = MathNet.Numerics.Random.RandomSource; - -namespace M4M -{ - public class Evolvability - { - public class EnvironmentTransitionConfiguration - { - public EnvironmentTransitionConfiguration(int generations, int samplePeriod, DevelopmentRules drules, ReproductionRules rrules, JudgementRules jrules) - { - Generations = generations; - SamplePeriod = samplePeriod; - DevelopmentRules = drules; - ReproductionRules = rrules; - JudgementRules = jrules; - } - - public int Generations { get; } - public int SamplePeriod { get; } - public DevelopmentRules DevelopmentRules { get; } - public ReproductionRules ReproductionRules { get; } - public JudgementRules JudgementRules { get; } - - public void WriteOut(System.IO.StreamWriter cw) - { - cw.WriteLine(); - cw.WriteLine($"Generations = {Generations}"); - - cw.WriteLine(); - cw.WriteLine($"DRules.UpdateRate (τ1) = {DevelopmentRules.UpdateRate}"); - cw.WriteLine($"DRules.DecayRate (τ2) = {DevelopmentRules.DecayRate}"); - cw.WriteLine($"DRules.TimeSteps (T) = {DevelopmentRules.TimeSteps}"); - cw.WriteLine($"DRules.SquashFunction (σ) = {DevelopmentRules.Squash.Name}"); - - cw.WriteLine(); - cw.WriteLine($"RRules.{nameof(ReproductionRules.InitialStateMutationSize)} (M_G) = {ReproductionRules.InitialStateMutationSize}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.InitialStateMutationType)} (M_G type) = {ReproductionRules.InitialStateMutationType}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.DevelopmentalTransformationMatrixMutationSize)} (M_B) = {ReproductionRules.DevelopmentalTransformationMatrixMutationSize}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.DevelopmentalTransformationMatrixMutationType)} (M_B type) = {ReproductionRules.DevelopmentalTransformationMatrixMutationType}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.DevelopmentalTransformationMatrixRate)} (R_B) = {ReproductionRules.DevelopmentalTransformationMatrixRate}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.ExclusiveBMutation)} (BEx) = {ReproductionRules.ExclusiveBMutation}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.InitialTraitUpdates)} (C_G) = {ReproductionRules.InitialTraitUpdates}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.InitialStateClamping)} = {ReproductionRules.InitialStateClamping}"); - cw.WriteLine($"RRules.{nameof(ReproductionRules.DevelopmentalTransformationMatrixClamping)} = {ReproductionRules.DevelopmentalTransformationMatrixClamping}"); - - cw.WriteLine(); - cw.WriteLine($"JRules.NoiseFactor (κ) = {JudgementRules.NoiseFactor}"); - cw.WriteLine($"JRules.RegularisationFactor (λ) = {JudgementRules.RegularisationFactor}"); - cw.WriteLine($"JRules.RegularisationFunction (ϕ) = {JudgementRules.RegularisationFunction.Name}"); - } - } - - public class EnvironmentPopulationTransitionConfiguration - { - public EnvironmentTransitionConfiguration EnvironmentTransitionConfiguration { get; } - public ISelectorPreparer<DenseIndividual> SelectorPreparer { get; } - - public EnvironmentPopulationTransitionConfiguration(EnvironmentTransitionConfiguration environmentTransitionConfiguration, ISelectorPreparer<DenseIndividual> selectorPreparer) - { - EnvironmentTransitionConfiguration = environmentTransitionConfiguration; - SelectorPreparer = selectorPreparer; - } - - public void WriteOut(System.IO.StreamWriter cw) - { - EnvironmentTransitionConfiguration.WriteOut(cw); - - cw.WriteLine(); - cw.WriteLine($"PopulationSpecifics.SelectorPreparer = {SelectorPreparer.Name}"); - } - } - - public class EnvironmentTransitionResults - { - public EnvironmentTransitionResults(double[] fitnessSamples, EnvironmentTransitionConfiguration config) - { - FitnessSamples = fitnessSamples; - Configuration = config; - } - - public double[] FitnessSamples { get; } - public EnvironmentTransitionConfiguration Configuration { get; } - private int SamplePeriod => Configuration.SamplePeriod; - - public int IndexToGeneration(int index) - { - return index * SamplePeriod; - } - - public void GenerationToIndex(int generation, out int closestSampledGeneration, out int closestSampleIndex) - { - // floor - closestSampleIndex = generation / SamplePeriod; - closestSampledGeneration = IndexToGeneration(closestSampleIndex); - } - - public int GenerationToIndex(int generation) - { - GenerationToIndex(generation, out int _, out int csi); - return csi; - } - } - - public static void StringOut(EnvironmentTransitionResults[] results, string fname) - { - using (StreamWriter writer = new StreamWriter(fname)) - { - StringOut(results, writer); - } - } - - public static void StringOut(EnvironmentTransitionResults[] results, StreamWriter writer) - { - writer.WriteLine(string.Join("\t", results[0].FitnessSamples.Select((_, i) => results[0].IndexToGeneration(i)))); - - foreach (var r in results) - { - writer.WriteLine(string.Join("\t", results[0].FitnessSamples)); - } - } - - // lazy, very lazy - public static void PlotLines(IEnumerable<Tuple<EnvironmentTransitionResults[], OxyColor, string>> results, string fname, string title) - { - PlotModel model = new PlotModel() { Title = title }; - - foreach (var r in results) - { - EnvironmentTransitionResults[] res = r.Item1; - OxyColor colour = r.Item2; - string name = r.Item3; - - for (int i = 0; i < res.Length; i++) - { - LineSeries ls = new LineSeries(); - - if (i == 0) - ls.Title = name; // only name one of them - - double[] samples = res[i].FitnessSamples; - for (int idx = 0; i < samples.Length; i++) - { - // this definitely can't be more efficient, oh no, certainly not - ls.Points.Add(new DataPoint(res[i].IndexToGeneration(idx), samples[idx])); - } - - model.Series.Add(ls); - } - } - - Plotting.OldOxyPlotting.ExportToPdf(model, fname, true); - } - - /// <summary> - /// Do not store the results, they are liable to be modified - /// </summary> - public static IEnumerable<Tuple<int, Percentile[]>> Percentiles(EnvironmentTransitionResults[] results, double[] positions, int step = 1) - { - for (int i = 0; i < results[0].FitnessSamples.Length; i += step) - { - double[] buffer = null; - Percentile[] percentiles = null; - Percentile.ComputePercentiles(positions, results.Select(r => r.FitnessSamples[i]), ref buffer, ref percentiles); - yield return new Tuple<int, Percentile[]>(results[0].IndexToGeneration(i), percentiles); - } - } - - public static void PlotAreas(IEnumerable<Tuple<EnvironmentTransitionResults[], OxyColor, string>> results, string fname, string title, int step = 1) - { - PlotModel model = new PlotModel() { Title = title }; - - model.Axes.Add(new LinearAxis() { Title = "Generation", Position = AxisPosition.Bottom }); - model.Axes.Add(new LinearAxis() { Title = "Fitness", Position = AxisPosition.Left }); - - foreach (var r in results) - { - EnvironmentTransitionResults[] res = r.Item1; - OxyColor colour = r.Item2; - string name = r.Item3; - - AreaSeries innerArea = new AreaSeries() { Color = colour.ChangeIntensity(0.8), Title = name + " 25-75" }; - AreaSeries outerArea = new AreaSeries() { Color = colour.ChangeIntensity(0.5), Title = name + " 10-90" }; - LineSeries meanLine = new LineSeries() { Color = colour, Title = name + " mean" }; - - var percentiles = Percentiles(res, new double[] { 0.1, 0.25, 0.5, 0.75, 0.9 }, step); - - foreach (var p in percentiles) - { - var generation = p.Item1; - var pcts = p.Item2; - - outerArea.Points.Add(new DataPoint(generation, pcts[0].Value)); - outerArea.Points2.Add(new DataPoint(generation, pcts[4].Value)); - - innerArea.Points.Add(new DataPoint(generation, pcts[1].Value)); - innerArea.Points2.Add(new DataPoint(generation, pcts[3].Value)); - - meanLine.Points.Add(new DataPoint(generation, pcts[2].Value)); - } - - model.Series.Add(outerArea); - model.Series.Add(innerArea); - model.Series.Add(meanLine); - } - - Plotting.OldOxyPlotting.ExportToPdf(model, fname, true, true, false, null); - Plotting.OldOxyPlotting.ExportToPdf(model, fname + "_sm", 0.9, true, false, null); - } - - public interface ISampler - { - void Start(EnvironmentTransitionConfiguration config, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement); - void Stop(int generations, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement); - - /// <summary> - /// The values past to this should NOT be stored or modified, they are liable to change, be returned more than once, or otherwise act strangely - /// </summary> - void Sample(int generation, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement); - } - - public delegate void SubSampler(int generation, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement, string name, string outDir, Func<string, string> file); - - public class SimpleSampler : ISampler - { - public SimpleSampler(string outDir, string name, SubSampler subSampler) - { - if (!System.IO.Directory.Exists(outDir)) - { - System.IO.Directory.CreateDirectory(outDir); - } - - OutDir = outDir; - _name = name; - _generations = new List<int>(); - SubSampler = subSampler; - } - - public string OutDir { get; } - public string _name { get; } - private int Count = 0; - public string Name => _name + Count; - private List<int> _generations { get; } - public IReadOnlyList<int> Generations => _generations; - private List<double>[] _initials { get; set; } - public IReadOnlyList<IReadOnlyList<double>> Initials => _initials; - private List<double> _fitnesses { get; set; } - public IReadOnlyList<double> Fitnesses => _fitnesses; - private int Size => Initials.Count; - private EnvironmentTransitionConfiguration CurrentConfig; - private SubSampler SubSampler { get; } - - private string File(string name) - { - return System.IO.Path.Combine(OutDir, Name + name); - } - - public void Sample(int generation, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement) - { - _generations.Add(generation); - for (int i = 0; i < Size; i++) - { - _initials[i].Add(genome.InitialState[i]); - } - - _fitnesses.Add(judgement.CombinedFitness); - - SubSampler?.Invoke(generation, genome, p, judgement, Name, OutDir, File); - } - - public void Start(EnvironmentTransitionConfiguration config, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement) - { - // reset/setup for next run - - Count++; - _generations.Clear(); - CurrentConfig = config; - - _initials = new List<double>[genome.Size]; - for (int i = 0; i < Size; i++) - { - _initials[i] = new List<double>(); - } - - _fitnesses = new List<double>(); - } - - public void Stop(int generations, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement) - { - PlotIstraj(); - PlotFitness(); - - MiscPlotting.PlotMatrix(Name + ": DTM", File("_dtm.pdf"), genome.TransMat, "Trait i", "Trait j"); - } - - private void PlotIstraj() - { - PlotModel plotModel = new PlotModel() { Title = Name + ": Trait Initial State Trajectories" }; - plotModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Bottom, Title = "Generation" }); - plotModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Title = "Initial Trait Values" }); - - int t = 0; - foreach (var traitTraj in Initials) - { - LineSeries ls = new LineSeries() { Title = "Trait" + t }; - - for (int i = 0; i < traitTraj.Count; i++) - { - ls.Points.Add(new DataPoint(Generations[i], traitTraj[i])); - } - - plotModel.Series.Add(ls); - - t++; - } - - plotModel.InvalidatePlot(true); - Plotting.OldOxyPlotting.ExportToPdf(plotModel, File("_istraj.pdf"), true, true); - } - - private void PlotFitness() - { - PlotModel plotModel = new PlotModel() { Title = Name + ": Fitness" }; - plotModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Bottom, Title = "Generation" }); - plotModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Title = "Fitness" }); - - LineSeries ls = new LineSeries(); - - for (int i = 0; i < _fitnesses.Count; i++) - { - ls.Points.Add(new DataPoint(Generations[i], _fitnesses[i])); - } - - plotModel.Series.Add(ls); - - plotModel.InvalidatePlot(true); - Plotting.OldOxyPlotting.ExportToPdf(plotModel, File("_fitness.pdf"), true, true); - } - } - - public static EnvironmentTransitionResults[] RunManyEnvironmentTransitionExperiments(ModelExecutionContext context, int count, DenseGenome genome, ITarget target, ISampler sampler, EnvironmentTransitionConfiguration config, bool oneTimeSampler) - { - EnvironmentTransitionResults[] results = new EnvironmentTransitionResults[count]; - - for (int i = 0; i < count; i++) - { - Console.WriteLine(i + "\t\\" + count); - - results[i] = RunEnvironmentTransitionExperiment(context, genome, target, sampler, config); - - if (i == 0 && oneTimeSampler) - sampler = null; - } - - return results; - } - - public static EnvironmentTransitionResults RunEnvironmentTransitionExperiment(ModelExecutionContext context, DenseGenome genome, ITarget target, ISampler sampler, EnvironmentTransitionConfiguration config) - { - var rand = context.Rand; - - // current genome and fitness - target.NextGeneration(rand, config.JudgementRules); - DenseGenome g = genome; - Phenotype p = g.Develop(context, config.DevelopmentRules); - MultiMeasureJudgement f = MultiMeasureJudgement.Judge(g, p, config.JudgementRules, target); - - int samples = config.Generations / config.SamplePeriod; - - // data - double[] fitnesses = new double[samples + 1]; - - // gen 0 - int si = 0; - sampler?.Start(config, g, p, f); - sampler?.Sample(si * config.SamplePeriod, g, p, f); - fitnesses[si] = f.CombinedFitness; - - for (; ++si <= samples;) - { - Experiment.SpinMk2(ref g, ref p, ref f, context, target, config.ReproductionRules, config.DevelopmentRules, config.JudgementRules, config.SamplePeriod); - - sampler?.Sample(si * config.SamplePeriod, g, p, f); - fitnesses[si] = f.CombinedFitness; - } - - sampler?.Stop(si * config.SamplePeriod, g, p, f); - return new EnvironmentTransitionResults(fitnesses, config); - } - - public static EnvironmentTransitionResults[] RunManyEnvironmentPopulationTransitionExperiments(ModelExecutionContext context, int count, DenseGenome genome, int populationSize, ITarget target, ISampler sampler, EnvironmentPopulationTransitionConfiguration config, bool oneTimeSampler, int exposureEpoch) - { - EnvironmentTransitionResults[] results = new EnvironmentTransitionResults[count]; - - for (int i = 0; i < count; i++) - { - Console.WriteLine(i + "\t\\" + count); - - results[i] = RunEnvironmentPopulationTransitionExperiment(context, genome, populationSize, target, sampler, config, exposureEpoch); - - if (i == 0 && oneTimeSampler) - sampler = null; - } - - return results; - } - - public static EnvironmentTransitionResults RunEnvironmentPopulationTransitionExperiment(ModelExecutionContext context, DenseGenome genome, int populationSize, ITarget target, ISampler sampler, EnvironmentPopulationTransitionConfiguration popConfig, int exposureEpoch) - { - var config = popConfig.EnvironmentTransitionConfiguration; - var populationSpinner = DefaultPopulationSpinner<DenseIndividual>.Instance; - - var rand = context.Rand; - - // current genome and fitness - ExposureInformation exposureInformation = new ExposureInformation(config.Generations); - target.NextExposure(rand, config.JudgementRules, exposureEpoch, ref exposureInformation); - if (exposureInformation.ExposureDuration < 0) - throw new Exception("ExposureDuration was negative"); - - target.NextGeneration(rand, config.JudgementRules); // this is a bit of a lie - DenseGenome g = genome; - Phenotype p = g.Develop(context, config.DevelopmentRules); - MultiMeasureJudgement f = MultiMeasureJudgement.Judge(g, p, config.JudgementRules, target); - - var templateIndividual = DenseIndividual.Develop(genome, context, config.DevelopmentRules, false); - Population<DenseIndividual> population = new Population<DenseIndividual>(templateIndividual, populationSize); - - int samples = config.Generations / config.SamplePeriod; - - // data - double[] fitnesses = new double[samples + 1]; - - // gen 0 - int si = 0; - sampler?.Start(config, g, p, f); - sampler?.Sample(si * config.SamplePeriod, g, p, f); - fitnesses[si] = f.CombinedFitness; - - for (; ++si <= samples;) - { - var res = populationSpinner.SpinPopulation(population, context, config.ReproductionRules, config.DevelopmentRules, config.JudgementRules, target, popConfig.SelectorPreparer, config.SamplePeriod, null, 0, false); - - // take median individual - var median = res.ArgMedian(ij => ij.Judgement.CombinedFitness); - g = median.Individual.Genome; - p = median.Individual.Phenotype; - f = median.Judgement; - - sampler?.Sample(si * config.SamplePeriod, g, p, f); - fitnesses[si] = f.CombinedFitness; - } - - sampler?.Stop(si * config.SamplePeriod, g, p, f); - return new EnvironmentTransitionResults(fitnesses, config); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Experiment.cs b/M4MCode/M4M_MkI/M4M.Old/Experiment.cs deleted file mode 100644 index 81ddcb9893abfe602a942a92498e8497b405d028..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Experiment.cs +++ /dev/null @@ -1,390 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Diagnostics; -using Linear = MathNet.Numerics.LinearAlgebra; // reasonable quality API reference here: https://numerics.mathdotnet.com/api/MathNet.Numerics.LinearAlgebra/ (inline isn't good enogh) -using RandomSource = MathNet.Numerics.Random.RandomSource; -using static M4M.Misc; -using static M4M.MiscPlotting; -using static M4M.Analysis; - -namespace M4M -{ - public class Experiment - { - /// <summary> - /// Returns the terminal genome - /// </summary> - public static TerminalInfo RunExperiment(RandomSource rand, ExperimentConfiguration config, string outDir, DenseGenome startingGenome = null, ExperimentFeedback feedback = null, string filePrefix = "", string titlePrefix = "", int bigSavePeriod = 200, int bigSaveCount = 100, bool appendTimeToOutDir = true, bool openTerminalsPlots = true, bool disableIO = false, int mutantAnalysisPeriod = 0, RandomSource ioRand = null, bool disableTransientIO = false) - { - ModelExecutionContext context = new ModelExecutionContext(rand); - - if (!disableIO) - { - if (appendTimeToOutDir) - outDir += "_" + NowTime; - System.IO.Directory.CreateDirectory(outDir); - } - - string file(string name) - { - return System.IO.Path.Combine(outDir, filePrefix + name); - } - - System.IO.StreamWriter open(string name) - { - return new System.IO.StreamWriter(file(name)); - } - - if (!disableIO) - { - // print out config - using (var cw = open("config.txt")) - { - cw.WriteLine("# Config for " + outDir); - - config.WriteOut(cw); - - cw.WriteLine(); - cw.WriteLine(); - cw.WriteLine("# Starting Genome Info"); - - if (startingGenome == null) - { - cw.WriteLine("(Default Genome)"); - } - else - { - string d = "(Default)"; - cw.WriteLine($"Size = {startingGenome.Size}"); - cw.WriteLine($"CustomTransMatMutator = {startingGenome.CustomInitialStateMutator?.Name ?? d}"); - cw.WriteLine($"CustomInitialStateMutator = {startingGenome.CustomTransMatMutator?.Name ?? d}"); - cw.WriteLine($"CustomDevelopmentStep = {startingGenome.CustomDevelopmentStep?.Name ?? d}"); - } - } - } - - // boring stuff - System.Diagnostics.Stopwatch sw = new Stopwatch(); - sw.Reset(); - sw.Start(); - - int size = config.Size; - DevelopmentRules drules = config.DevelopmentRules; - ReproductionRules rrules = config.ReproductionRules; - JudgementRules jrules = config.JudgementRules; - - // init genome - // TODO: determine proper way from paper - DenseGenome g = startingGenome ?? DenseGenome.CreateDefaultGenome(size); - double f = -1; // current fitness - - // dec target (init inside epoch loop) - int currentTargetIndex = 0; // always start with 0 for simplicity - ITarget currentTarget = null; - - // time remaining estimations - TimeSpan lastSampleDuration = default(TimeSpan); - int lastSampleEpoch = -1; - - // records - int maxRecordCount = 1000; - int recordPeriod = Clamp(config.Epochs / maxRecordCount, 1, int.MaxValue); - double[][] regulationCoefs = CreateEmpty<double>(config.Size * config.Size, config.Epochs / recordPeriod + 1); - double[] fitness = new double[config.Epochs / recordPeriod + 1]; - - if (!disableIO) - { - // plot targets - for (int i = 0; i < config.Targets.Count; i++) - { - if (config.Targets[i] is VectorTarget vt) - { - PlotVectorFolded(titlePrefix + " Target " + i, file("target" + i + ".pdf"), vt.Vector); - } - } - } - - // refit bigSavePeriod (max 10 big saves) - bigSavePeriod = Clamp(config.Epochs / 10, bigSavePeriod, int.MaxValue); - - Console.WriteLine(outDir); - Console.WriteLine(config.Epochs + "epochs "); - - int totalGenerationCount = 0; - - // for each epoch (start at 1 this time) - for (int epoch = 1; epoch <= config.Epochs; epoch++) - { - for (int ti = 0; ti < config.Targets.Count; ti++) - { - // select next currentTarget - currentTargetIndex = config.TargetCycler.Cycle(rand, currentTargetIndex, config.Targets.Count); - currentTarget = config.Targets[currentTargetIndex]; - - feedback?.StartTarget.Call(st => st(g, currentTarget, epoch, totalGenerationCount)); - - // update fitness - currentTarget.NextGeneration(rand, jrules); - Phenotype p = g.Develop(context, drules); - f = MultiMeasureJudgement.Judge(g, p, jrules, currentTarget).CombinedFitness; - - // mutate, judge, select cycle - Spin(ref g, ref p, ref f, context, currentTarget, rrules, drules, jrules, config.GenerationsPerTargetPerEpoch); - totalGenerationCount += config.GenerationsPerTargetPerEpoch; - - feedback?.EndTarget.Call(et => et(g, currentTarget, epoch, totalGenerationCount)); - } - - feedback?.EndEpoch.Call(ee => ee(g, epoch, totalGenerationCount)); - - // record stuff - if (epoch % recordPeriod == 0) - { - // remest - TimeSpan now = sw.Elapsed; - TimeSpan remest = lastSampleEpoch < 0 ? default(TimeSpan) : TimeSpan.FromSeconds((now - lastSampleDuration).TotalSeconds * (double)(config.Epochs - epoch) / (double)(epoch - lastSampleEpoch)); - lastSampleEpoch = epoch; - lastSampleDuration = now; - - // epoch summary - Console.WriteLine($"Epoch {epoch} ({(100.0 * epoch / config.Epochs).ToString("0.0")}%): f = {f}, t = {sw.ElapsedMilliseconds}ms, remest={remest}"); - - // record fitness - fitness[epoch / recordPeriod] = f; - - // record regulation coefs - int o = 0; - for (int i = 0; i < g.Size; i++) - { - for (int j = 0; j < g.Size; j++) - { - regulationCoefs[o][epoch / recordPeriod] = g.TransMat[i, j]; - o++; - } - } - } - - // no more than bigSaveCount or so of these - if ((epoch % bigSavePeriod == 0 || epoch % Clamp(config.Epochs / bigSaveCount, 1, int.MaxValue) == 0)) - { - if (!disableIO && !disableTransientIO) - { - // save genome - SaveGenome(file("genome" + epoch + ".dat"), g); - - // plot genome - PlotVectorFolded(titlePrefix + "Phenotype " + epoch, file("p" + epoch + ".pdf"), g.Develop(context, drules).Vector); - PlotVectorFolded(titlePrefix + "Epoch " + epoch, file("ps" + epoch + ".pdf"), g.Develop(context, drules).Vector, "Sub-trait", "Module"); - PlotMatrix(titlePrefix + "Development Transformation Matrix " + epoch, file("dtm" + epoch + ".pdf"), g.TransMat, "Trait i", "Trait j"); - PlotMatrix("Epoch " + epoch, file("dtms" + epoch + ".pdf"), g.TransMat, "Trait i", "Trait j"); - PlotVectorFolded(titlePrefix + "Intial State " + epoch, file("is" + epoch + ".pdf"), g.InitialState); - PlotVectorFolded(titlePrefix + "Epoch " + epoch, file("ism" + epoch + ".pdf"), g.InitialState, "Sub-trait", "Module"); - } - } - - // no more than 10 or so of these - if (epoch > 0 && epoch % bigSavePeriod == 0) - { - try - { - if (!disableIO && !disableTransientIO) - { - SaveTrajectories(file("regcoefs" + epoch + ".dat"), regulationCoefs, recordPeriod); - - PlotLines(titlePrefix + "Regulation Coefs " + epoch, file("rcs" + epoch + ".pdf"), regulationCoefs, 0, recordPeriod, xTitle: "Epoch", yTitle: "Weight"); - - PlotLine(titlePrefix + "Fitness " + epoch, file("fitness" + epoch + ".pdf"), fitness, 0, recordPeriod, xTitle: "Epoch", yTitle: "Fitness"); - - if (ioRand != null) - { - ITarget other = config.Targets.FirstOrDefault(t => t != currentTarget); - Experiment.RunTrace(ioRand, new FileStuff(outDir, "", "").CreateSubStuff("tr" + epoch), g, config, other); - } - } - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - } - - // mutant analysis is independantly cycled - if (mutantAnalysisPeriod > 0 && (epoch % mutantAnalysisPeriod == 0 || epoch == config.Epochs)) - { - Console.WriteLine("(MutantAnalysis for epoch " + epoch); - MutantAnalysis ma = new MutantAnalysis(g, 10000, context, rrules, drules, jrules, config.Targets[currentTargetIndex]); // 'current' target - ma.Plot(outDir, "MA" + epoch, titlePrefix + " MA" + epoch + ": "); - } - } - - feedback?.Finished.Call(_f => _f(g, config.Epochs, totalGenerationCount)); - - // traces - - - if (!disableIO) - { - // terminals - SaveTrajectories(file("regcoefs.dat"), regulationCoefs, recordPeriod); - - PlotLines(titlePrefix + "Regulation Coefs", file("rcs.pdf"), regulationCoefs, 0, recordPeriod, xTitle: "Epoch", yTitle: "Weight"); - if (openTerminalsPlots) - Plotting.OldOxyPlotting.ViewPdf(file("rcs.pdf")); - - PlotLine(titlePrefix + "Fitness", file("fitness.pdf"), fitness, 0, recordPeriod); - if (openTerminalsPlots) - Plotting.OldOxyPlotting.ViewPdf(file("fitness.pdf")); - - PlotMatrix(titlePrefix + "GRN", file("dtms.pdf"), g.TransMat, "Trait i", "Trait j"); - PlotMatrix(titlePrefix + "GRN", file("dtm.pdf"), g.TransMat, "Trait i", "Trait j"); - } - - return new TerminalInfo(g, f); - } - - public static void RunTrace(RandomSource rand, FileStuff stuff, DenseGenome genome, ExperimentConfiguration config, ITarget target) - { - ModelExecutionContext context = new ModelExecutionContext(rand); - - // perform a trace from one target (i.e. whatever was before) to another - int N = config.Size; - - List<double> traceFitness = new List<double>(); - List<GenomeJudgement> traceSamples = new List<GenomeJudgement>(); - - void gjFeedback(int generation, GenomeJudgement judgement) - { - traceFitness.Add(judgement.Judgement.CombinedFitness); - traceSamples.Add(judgement); - } - - // spin it - MultiMeasureJudgement fitness = default(MultiMeasureJudgement); - Phenotype phenotype = null; - Experiment.SpinMk2(ref genome, ref phenotype, ref fitness, context, target, config.ReproductionRules, config.DevelopmentRules, config.JudgementRules, config.GenerationsPerTargetPerEpoch, gjFeedback); - - PlotLine(stuff.TitlePrefix + "TrMeanFitness", stuff.File("trfitness"), traceFitness.ToArray(), 1, 1, "Generation", "Mean Fitness"); - - double[][] trPhenotypeState = Enumerable.Range(0, N).Select(i => traceSamples.Select(ij => ij.Phenotype.Vector[i]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "TrPhenotypicState", stuff.File("trpstate"), trPhenotypeState, 1, 1, "Generation", "Trait Expression"); - - double[][] trInitialState = Enumerable.Range(0, N).Select(i => traceSamples.Select(ij => ij.Genome.InitialState[i]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "TrInitialState", stuff.File("tristate"), trInitialState, 1, 1, "Generation", "Trait Expression"); - - double[][] trRcs = Enumerable.Range(0, N*N).Select(i => traceSamples.Select(ij => ij.Genome.TransMat[i / N, i % N]).ToArray()).ToArray(); - PlotLines(stuff.TitlePrefix + "TrRcs", stuff.File("trrcs"), trRcs, 1, 1, "Generation", "Regulation Coeficient"); - } - - public static void Spin(ref DenseGenome genome, ref Phenotype p, ref double fitness, ModelExecutionContext context, ITarget t, ReproductionRules rrules, DevelopmentRules drules, JudgementRules jrules, int generations) - { - var rand = context.Rand; - - DenseGenome g = genome; - double f = fitness; - - for (int i = 0; i < generations; i++) - { - // update Target - bool invalidatef = t.NextGeneration(rand, jrules); - - if (invalidatef) - { - f = MultiMeasureJudgement.Judge(g, p, jrules, t).CombinedFitness; - } - - // generate and judge - DenseGenome g2 = g.Mutate(context, rrules); - Phenotype p2 = g2.Develop(context, drules); - double f2 = MultiMeasureJudgement.Judge(g2, p2, jrules, t).CombinedFitness; - - // select - if (f2 > f) - { - g = g2; - f = f2; - } - } - - genome = g; - fitness = f; - } - - public static void SpinMk2(ref DenseGenome genome, ref Phenotype phenotype, ref MultiMeasureJudgement judgement, ModelExecutionContext context, ITarget t, ReproductionRules rrules, DevelopmentRules drules, JudgementRules jrules, int generations, Action<int, GenomeJudgement> judgementFeedback = null) - { - var rand = context.Rand; - - DenseGenome g = genome; - Phenotype p = phenotype; - MultiMeasureJudgement f = judgement; - - if (p == null) - { - t.NextGeneration(rand, jrules); - p = g.Develop(context, drules); - f = MultiMeasureJudgement.Judge(g, p, jrules, t); - } - - for (int i = 0; i < generations; i++) - { - // update Target - bool invalidatef = t.NextGeneration(rand, jrules); - - if (invalidatef) - { - f = MultiMeasureJudgement.Judge(g, p, jrules, t); - } - - // generate and judge - DenseGenome g2 = g.Mutate(context, rrules); - Phenotype p2 = g2.Develop(context, drules); - MultiMeasureJudgement f2 = MultiMeasureJudgement.Judge(g2, p2, jrules, t); - - // select - if (f2.CombinedFitness > f.CombinedFitness) - { - g = g2; - p = p2; - f = f2; - } - - if (judgementFeedback != null) - { - judgementFeedback?.Invoke(i, new GenomeJudgement(g, p, f)); - } - } - - genome = g; - phenotype = p; - judgement = f; - } - } - - public class GenomeJudgement - { - public GenomeJudgement(DenseGenome genome, Phenotype phenotype, MultiMeasureJudgement judgement) - { - Genome = genome; - Phenotype = phenotype; - Judgement = judgement; - } - - public DenseGenome Genome { get; } - public Phenotype Phenotype { get; } - public MultiMeasureJudgement Judgement { get; } - } - - public class TerminalInfo - { - public TerminalInfo(DenseGenome genome, double fitness) - { - Genome = genome; - Fitness = fitness; - } - - public DenseGenome Genome { get; } - public double Fitness { get; } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Extensions.cs b/M4MCode/M4M_MkI/M4M.Old/Extensions.cs deleted file mode 100644 index 874bad813031555c180d602100b3917d5bb6aca0..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Extensions.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Linear = MathNet.Numerics.LinearAlgebra; // reasonable quality API reference here: https://numerics.mathdotnet.com/api/MathNet.Numerics.LinearAlgebra/ (inline isn't good enogh) - -namespace M4M.Old -{ - public static class Extensions - { - public static Linear.Matrix<double> CreateGRS(string pattern, double g, double r, double s) - { - string lpattern = pattern.ToLower(); - double[] spattern = pattern.Select(c => c == new string(c, 1).ToLower()[0] ? 1.0 : -1.0).ToArray(); - int N = pattern.Length; - - double cf(int y, int x) - { - char f = lpattern[x]; // from - char t = lpattern[y]; // to - - double sign = spattern[x] * spattern[y]; - - if (t != f) - { - // no correlation (different modules) - return 0; - } - else - { - if (lpattern.IndexOf(f) == x) - { - // f is the snowflake - if (y == x) - { - // self-feed - return s * sign; - } - else - { - // cross-feed - return r * sign; - } - } - else - { - // not on the beam - return g * sign; - } - } - } - - return Linear.CreateMatrix.Dense<double>(N, N, cf); - } - - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/FearExperiments.cs b/M4MCode/M4M_MkI/M4M.Old/FearExperiments.cs deleted file mode 100644 index 566d9a32afffc815b4ec269dbb7b92ed80ec0fa1..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/FearExperiments.cs +++ /dev/null @@ -1,196 +0,0 @@ -using MathNet.Numerics.Random; -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading.Tasks; -using static M4M.Misc; - -namespace M4M -{ - public static class FearExperiments - { - public static void Fear4VaryZ(TextWriter console, string topdir, DensePopulationExperimentFeedbackFactory densePopulationExperimentFactory, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null) - { - string objectives = "Investigate a minimal experiment, with G constant, and a column constraint (size 4, solo/nosolo BEx), with a slight L2 regularisation" + - "" + - "The idea is that a slight L2 will force it to stay dense, as there is no benefit to hierarchy"; - topdir = System.IO.Path.Combine(topdir, @"Fear4VaryZ\standard"); - - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - List<Action> expActs = new List<Action>(); - - //foreach (double z in new[] { 0.0, 0.001, 0.01, 0.1 }) // proportion L2 (0.0 is no L2 (i.e. all L1)) - foreach (double z in new[] { 0.0, 0.000000001, 0.00000001, 0.0000001 }) // proportion L2 (0.0 is no L2 (i.e. all L1)) - { - foreach (bool solo in new[] { true, false }) - { - expActs.Add(() => - { - string prefix = $"z{z}solo{(solo?"T":"F")}"; - - try - { - FearRunners.RunFear(console, topdir: topdir, prefix: prefix, suffix: "", count: 1, - targetPackage: Modular.ModularTargetPackage.HighJudge4x1, λ: 0.02, K: 1000, epochs: 5000, bNoiseTerm: 0.001, - singleColumnMode: solo, popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.L1and2(z), - disableTransientIo: false, densePopulationExperimentFactory: densePopulationExperimentFactory, densePopulationExperimentFeedbackConfigurator: densePopulationExperimentFeedbackConfigurator); - } - catch (Exception ex) - { - console.WriteLine($"Run {prefix} crashed"); - console.WriteLine(ex); - } - } - ); - } - } - - Parallel.Invoke(expActs.ToArray()); - } - - public static void Fear4x4(TextWriter console, string topdir, DensePopulationExperimentFeedbackFactory densePopulationExperimentFactory, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null) - { - string objectives = "Investigate a 4x4MVG with Fear config, L1" + - "" + - "If this works, then I have to assume that all my work is wrong."; - topdir = System.IO.Path.Combine(topdir, @"Fear4x4\standard"); - - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - List<Action> expActs = new List<Action>(); - - foreach (double z in new[] { 0.0 }) // proportion L2 (0.0 is no L2 (i.e. all L1)) - { - foreach (bool solo in new[] { true, false }) - { - expActs.Add(() => - { - string prefix = $"z{z}solo{(solo?"T":"F")}"; - - try - { - FearRunners.RunFear(console, topdir: topdir, prefix: prefix, suffix: "", count: 1, - targetPackage: Modular.ModularTargetPackage.Original4x4, λ: 0.02, K: 1000, epochs: 5000, bNoiseTerm: 0.001, - singleColumnMode: solo, popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.L1and2(z), - disableTransientIo: false, densePopulationExperimentFactory: densePopulationExperimentFactory, densePopulationExperimentFeedbackConfigurator: densePopulationExperimentFeedbackConfigurator); - } - catch (Exception ex) - { - console.WriteLine($"Run {prefix} crashed"); - console.WriteLine(ex); - } - } - ); - } - } - - Parallel.Invoke(expActs.ToArray()); - } - } - - public static class FearRunners - { - public static void RunFear(TextWriter console, string topdir, string prefix, string suffix, Modular.ModularTargetPackage targetPackage, int epochs, int K, double λ, double bNoiseTerm, bool singleColumnMode, IRegularisationFunction<IGenome> regularisationFunction, int count = 10, bool disableTransientIo = true, DensePopulationExperimentFeedbackFactory densePopulationExperimentFactory = null, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null, int popSize = 1, int eliteCount = 1, bool hillclimberMode = true) - { - var nowTime = Misc.NowTime; - - List<DtmInfo> dtmInfos = new List<DtmInfo>(); - - topdir = topdir + "\\" + prefix + targetPackage.Name + suffix; - - int N = targetPackage.TargetSize; - - var fileStuff = FileStuff.CreateNow(topdir, "", prefix, true); - - RandomSource rand = new MersenneTwister(false); - RandomSource ioRand = new MersenneTwister(false); - ModelExecutionContext context = new ModelExecutionContext(rand); - - // main loop - - for (int i = 0; i < count; i++) - { - var drules = TypicalConfiguration.CreateStandardDevelopmentRules( - squash: DevelopmentRules.TanhHalf - ); - - var rrules = TypicalConfiguration.CreateStandardReproductionRules( - gMutationMagnitude: 0.0, - bMutationMagnitude: bNoiseTerm, - bMutationProbability: 1.0, - bMutationType: NoiseType.Uniform, - exclusiveBMutation: true - ); - - var jrules = TypicalConfiguration.CreateStandardJudgementRules( - regularisationFactor: λ, - regularisationFunction: regularisationFunction, - noiseFactor: 0.0 - ); - - var config = TypicalConfiguration.CreateConfig( - N: N, - targets: targetPackage.Targets, - targetCycler: Cyclers.Loop, - epochs: epochs, - generationsPerTargetPerEpoch: K, - gResetProb: 1.0, // make sure g matches s all the time (note: HiJ works as expected, because it uses a coef to scale, it doesn't change the vector) - gResetRange: new Range(-1.0, 1.0), // ignored by MatchTargetVector - drules: drules, - rrules: rrules, - jrules: jrules - ); - - var popConfig = TypicalConfiguration.CreatePopulationConfig( - config: config, - eliteCount: eliteCount, - hillclimberMode: hillclimberMode, - resetIndividualInitialStateOperation: PopulationResetOperations.MatchTargetVector - ); - - var pop = TypicalConfiguration.CreatePopulation( - context: context, - populationSize: popSize, - N: N, - drules: drules, - transMatMutator: singleColumnMode - ? Mutators.SingleColumn(targetPackage.TargetSize, targetPackage.ModuleCount) // column constraint (only one column updated at a time) - : Mutators.Columns2(targetPackage.TargetSize, targetPackage.ModuleCount) // column constraint - ); - - var stuff = fileStuff.CreateSubStuff("r" + i); - - densePopulationExperimentFactory = densePopulationExperimentFactory ?? CommonDensePopulationExperimentFeedback.SimpleCommonDensePopulationExperimentFeedback(console); - IDensePopulationExperimentFeedback denseFeedback = densePopulationExperimentFactory(ioRand, popConfig, null); - densePopulationExperimentFeedbackConfigurator?.Invoke(denseFeedback); - - var res1 = RunPopulationExperiment(console, rand, stuff, pop, popConfig, denseFeedback.Feedback, disableTransientIo, false); - } - } - - public static Population<DenseIndividual> RunPopulationExperiment(TextWriter console, RandomSource rand, FileStuff fileStuff, Population<DenseIndividual> initialPopulation, PopulationExperimentConfig<DenseIndividual> populationExperimentConfig, PopulationExperimentFeedback<DenseIndividual> feedback, bool disableTransientIO = true, bool disableAllIO = false) - { - // essentials - rand = rand ?? new MathNet.Numerics.Random.MersenneTwister(); - var context = new ModelExecutionContext(rand); - - var population = initialPopulation.Clone(); - - // go - PopulationExperimentRunners.RunExperiment<DenseIndividual>(console, "", - context: context, - population: population, - populationConfig: populationExperimentConfig, - stuff: fileStuff, - disableTransientIO: disableTransientIO, - disableAllIO: disableAllIO, - feedback: feedback); - - return population; - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/HModExperiments.cs b/M4MCode/M4M_MkI/M4M.Old/HModExperiments.cs deleted file mode 100644 index fc843dadbff57a0527284d94f1e63b3b37b7d896..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/HModExperiments.cs +++ /dev/null @@ -1,298 +0,0 @@ -using M4M.Modular; -using MathNet.Numerics.Random; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static M4M.Misc; -using Linear = MathNet.Numerics.LinearAlgebra; - -namespace M4M -{ - public class HModExperiments - { - // for far too long has I been running things... now I shall prepare them, so that I can run them later at will (or rather, on the HPC cluster) - public static PopulationExperiment<DenseIndividual> PrepareClassicM4M(string topdir, string name, ModularTargetPackage targetPackage, int epochs, double λ, int K, IRegularisationFunction<IGenome> regularisationFunction, bool exclusiveBMutation, bool appendTimestamp, int initialStateUpdates, bool binaryG, bool singleB = false, int T = 10) - { - topdir = System.IO.Path.Combine(topdir); - - int N = 16; - - double gNoiseTerm = binaryG ? 2 : 0.1; //p22 - NoiseType gNoiseType = binaryG ? NoiseType.Binary : NoiseType.Uniform; - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - NoiseType bNoiseType = NoiseType.Uniform; - - double bProb = 1.0 / 2; //p9/OLD - - var rand = new MersenneTwister(false); - var ioRand = new MersenneTwister(false); - - var popConfig = HModRunners.PrepareHillClimberConfig(targetPackage: targetPackage, regularisationFunction: regularisationFunction, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bMutationProbability: bProb, exclusiveBMutation: exclusiveBMutation, gResetProb: 0.0, populationResetOperation: PopulationResetOperations.None, gUpdateCount: initialStateUpdates, gNoiseType : gNoiseType, bNoiseType: bNoiseType, T: T); - var population = singleB - ? HModRunners.CreateSingleCellHillClimberPopulation(rand, popConfig) - : HModRunners.CreateStandardHillClimberPopulation(rand, popConfig); - - string subName = "classic" + name; - topdir = System.IO.Path.Combine(topdir, subName); - - var popExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(population, popConfig, topdir, "", name, false, appendTimestamp); - popExperiment.Save("starter", false); - PopulationExperimentRunners.WriteOutInitials(ioRand, popExperiment); // mosty for config.txt, so that a human/GradStudent can look it over - - return popExperiment; - } - - public static PopulationExperiment<DenseIndividual> PreparePerfectM4M(string topdir, string name, ModularTargetPackage targetPackage, int epochs, double λ, int K, IRegularisationFunction<IGenome> regularisationFunction, bool appendTimestamp, bool singleB) - { - topdir = System.IO.Path.Combine(topdir); - - int N = targetPackage.TargetSize; - - double gNoiseTerm = 0.0; - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0; - - var rand = new MersenneTwister(false); - var ioRand = new MersenneTwister(false); - - var popConfig = HModRunners.PrepareHillClimberConfig(targetPackage: targetPackage, regularisationFunction: regularisationFunction, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bMutationProbability: bProb, exclusiveBMutation: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.MatchTargetVector, gUpdateCount: 0); - var population = singleB - ? HModRunners.CreateSingleCellHillClimberPopulation(rand, popConfig) - : HModRunners.CreateStandardHillClimberPopulation(rand, popConfig); - - string subName = "perfect" + name; - topdir = System.IO.Path.Combine(topdir, subName); - - var popExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(population, popConfig, topdir, "", name, false, appendTimestamp); - popExperiment.Save("starter", false); - PopulationExperimentRunners.WriteOutInitials(ioRand, popExperiment); // mosty for config.txt, so that a human/GradStudent can look it over - - return popExperiment; - } - - public static PopulationExperiment<DenseIndividual> PreparePopulationM4M(string topdir, string name, Linear.Matrix<double> b0, ModularTargetPackage targetPackage, int epochs, double λ, int K, IRegularisationFunction<IGenome> regularisationFunction, bool exclusiveBMutation, bool appendTimestamp, int populationSize, int eliteCount, bool hillclimberMode, double bProb) - { - topdir = System.IO.Path.Combine(topdir); - - int N = 16; - - double gNoiseTerm = 0.1; //p22 - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - var rand = new MersenneTwister(false); - var ioRand = new MersenneTwister(false); - var context = new ModelExecutionContext(rand); - - var popConfig = HModRunners.PreparePopulationConfig(targetPackage: targetPackage, regularisationFunction: regularisationFunction, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bMutationProbability: bProb, exclusiveBMutation: exclusiveBMutation, gResetProb: 0.0, populationResetOperation: PopulationResetOperations.None, eliteCount: eliteCount, hillclimberMode: hillclimberMode); - var population = TypicalConfiguration.CreatePopulation(context, populationSize, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules, b0: b0); - - string subName = "population" + name; - topdir = System.IO.Path.Combine(topdir, subName); - - var popExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(population, popConfig, topdir, "", name, false, appendTimestamp); - popExperiment.Save("starter", false); - PopulationExperimentRunners.WriteOutInitials(ioRand, popExperiment); // mosty for config.txt, so that a human/GradStudent can look it over - - return popExperiment; - } - - public static PopulationExperiment<DenseIndividual> PreparePopulationM4M(string topdir, string name, ModularTargetPackage targetPackage, int epochs, double λ, int K, IRegularisationFunction<IGenome> regularisationFunction, bool exclusiveBMutation, bool appendTimestamp, int populationSize, int eliteCount, bool hillclimberMode, bool binaryG, bool columns) - { - topdir = System.IO.Path.Combine(topdir); - - int N = 16; - - double gNoiseTerm = binaryG ? 2 : 0.1; //p22 - NoiseType gNoiseType = binaryG ? NoiseType.Binary : NoiseType.Uniform; - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - NoiseType bNoiseType = NoiseType.Uniform; - - double bProb = 1.0 / 2; //p9/OLD - - var rand = new MersenneTwister(false); - var ioRand = new MersenneTwister(false); - - var popConfig = HModRunners.PreparePopulationConfig(targetPackage: targetPackage, regularisationFunction: regularisationFunction, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bMutationProbability: bProb, exclusiveBMutation: exclusiveBMutation, gResetProb: 0.0, populationResetOperation: PopulationResetOperations.None, eliteCount: eliteCount, hillclimberMode: hillclimberMode, gNoiseType : gNoiseType, bNoiseType: bNoiseType); - var population = columns - ? HModRunners.CreateColumnsPopulationPopulation(rand, popConfig, populationSize, targetPackage) - : HModRunners.CreateStandardPopulationPopulation(rand, popConfig, populationSize); - - string subName = "population" + name; - topdir = System.IO.Path.Combine(topdir, subName); - - var popExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(population, popConfig, topdir, "", name, false, appendTimestamp); - popExperiment.Save("starter", false); - PopulationExperimentRunners.WriteOutInitials(ioRand, popExperiment); // mosty for config.txt, so that a human/GradStudent can look it over - - return popExperiment; - } - - public static PopulationExperiment<DenseIndividual> PrepareFear(string topdir, string name, ModularTargetPackage targetPackage, int epochs, double λ, int K, IRegularisationFunction<IGenome> regularisationFunction, bool singleB, bool appendTimestamp) - { - topdir = System.IO.Path.Combine(topdir); - - int N = targetPackage.TargetSize; - - double gNoiseTerm = 0.0; - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0; // little sense having anything else - - var rand = new MersenneTwister(false); - var ioRand = new MersenneTwister(false); - - var popConfig = HModRunners.PrepareHillClimberConfig(targetPackage, regularisationFunction: regularisationFunction, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bMutationProbability: bProb, exclusiveBMutation: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.MatchTargetVector, gUpdateCount: 0); - var population = singleB - ? HModRunners.CreateSingleColumnHillClimberPopulation(rand, popConfig, targetPackage) - : HModRunners.CreateColumnsHillClimberPopulation(rand, popConfig, targetPackage); - - string subName = "fear" + name; - topdir = System.IO.Path.Combine(topdir, subName); - - var popExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(population, popConfig, topdir, "", name, false, appendTimestamp); - popExperiment.Save("starter", false); - PopulationExperimentRunners.WriteOutInitials(ioRand, popExperiment); // mosty for config.txt, so that a human/GradStudent can look it over - - return popExperiment; - } - } - - public static class HModRunners - { - public static Population<DenseIndividual> CreateStandardHillClimberPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), 1, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules); - } - - public static Population<DenseIndividual> CreateStandardPopulationPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig, int popSize) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), popSize, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules); - } - - public static Population<DenseIndividual> CreateColumnsPopulationPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig, int popSize, ModularTargetPackage targetPackage) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), popSize, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules, - transMatMutator: new Columns2TransMatMutator(targetPackage.TargetSize, targetPackage.ModuleCount)); - } - - public static Population<DenseIndividual> CreateSingleCellHillClimberPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), 1, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules, - transMatMutator: new SingleCellTransMatMutator()); - } - - public static Population<DenseIndividual> CreateColumnsHillClimberPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig, ModularTargetPackage targetPackage) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), 1, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules, - transMatMutator: new Columns2TransMatMutator(targetPackage.TargetSize, targetPackage.ModuleCount)); - } - - public static Population<DenseIndividual> CreateSingleColumnHillClimberPopulation(RandomSource rand, PopulationExperimentConfig<DenseIndividual> popConfig, ModularTargetPackage targetPackage) - { - return TypicalConfiguration.CreatePopulation(new ModelExecutionContext(rand), 1, popConfig.ExperimentConfiguration.Size, popConfig.ExperimentConfiguration.DevelopmentRules, - transMatMutator: new SingleColumnTransMatMutator(targetPackage.TargetSize, targetPackage.ModuleCount)); - } - - public static PopulationExperimentConfig<DenseIndividual> PrepareHillClimberConfig(ModularTargetPackage targetPackage, int epochs, int K, double λ, IRegularisationFunction<IGenome> regularisationFunction, bool exclusiveBMutation, int T = 10, double gNoiseTerm = 0.1, double bMutationProbability = 1.0, Linear.Vector<double> g0 = null, bool gFixed = false, Linear.Matrix<double> b0 = null, int popSize = 1, Range gClamp = null, ISquash squash = null, double bNoiseTerm = 0.001, int gUpdateCount = 1, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, double gResetProb = 0, Range gResetRange = null, IPopulationResetOperation<DenseIndividual> populationResetOperation = null, NoiseType gNoiseType = NoiseType.Uniform, NoiseType bNoiseType = NoiseType.Uniform) - { - int N = targetPackage.TargetSize; - - var drules = TypicalConfiguration.CreateStandardDevelopmentRules( - squash: DevelopmentRules.TanhHalf, - timeSteps: T - ); - - var rrules = TypicalConfiguration.CreateStandardReproductionRules( - gMutationMagnitude: gNoiseTerm, - gMutationType: gNoiseType, - gUpdates: gUpdateCount, - gClamping: gClamp, - bMutationMagnitude: bNoiseTerm, - bMutationProbability: bMutationProbability, - bMutationType: bNoiseType, - exclusiveBMutation: exclusiveBMutation - ); - - var jrules = TypicalConfiguration.CreateStandardJudgementRules( - regularisationFactor: λ, - regularisationFunction: regularisationFunction, - noiseFactor: 0.0 - ); - - var config = TypicalConfiguration.CreateConfig( - N: N, - targets: targetPackage.Targets, - targetCycler: Cyclers.Loop, - epochs: epochs, - generationsPerTargetPerEpoch: K, - gResetProb: gResetProb, - gResetRange: gResetRange ?? new Range(-1.0, 1.0), - drules: drules, - rrules: rrules, - jrules: jrules - ); - - var popConfig = TypicalConfiguration.CreatePopulationConfig( - config: config, - eliteCount: 1, - hillclimberMode: true, - resetIndividualInitialStateOperation: populationResetOperation ?? PopulationResetOperations.RandomIndependent - ); - - return popConfig; - } - - public static PopulationExperimentConfig<DenseIndividual> PreparePopulationConfig(ModularTargetPackage targetPackage, int epochs, int K, double λ, IRegularisationFunction<IGenome> regularisationFunction, bool exclusiveBMutation, int T = 10, double gNoiseTerm = 0.1, double bMutationProbability = 1.0, Linear.Vector<double> g0 = null, bool gFixed = false, Linear.Matrix<double> b0 = null, int popSize = 1, Range gClamp = null, ISquash squash = null, double bNoiseTerm = 0.001, int gUpdateCount = 1, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, double gResetProb = 0, Range gResetRange = null, IPopulationResetOperation<DenseIndividual> populationResetOperation = null, int eliteCount = 0, bool hillclimberMode = false, NoiseType gNoiseType = NoiseType.Uniform, NoiseType bNoiseType = NoiseType.Uniform) - { - int N = targetPackage.TargetSize; - - var drules = TypicalConfiguration.CreateStandardDevelopmentRules( - squash: DevelopmentRules.TanhHalf, - timeSteps: T - ); - - var rrules = TypicalConfiguration.CreateStandardReproductionRules( - gMutationMagnitude: gNoiseTerm, - gMutationType: gNoiseType, - gUpdates: gUpdateCount, - gClamping: gClamp, - bMutationMagnitude: bNoiseTerm, - bMutationProbability: bMutationProbability, - bMutationType: bNoiseType, - exclusiveBMutation: exclusiveBMutation - ); - - var jrules = TypicalConfiguration.CreateStandardJudgementRules( - regularisationFactor: λ, - regularisationFunction: regularisationFunction, - noiseFactor: 0.0 - ); - - var config = TypicalConfiguration.CreateConfig( - N: N, - targets: targetPackage.Targets, - targetCycler: Cyclers.Loop, - epochs: epochs, - generationsPerTargetPerEpoch: K, - gResetProb: gResetProb, - gResetRange: gResetRange ?? new Range(-1.0, 1.0), - drules: drules, - rrules: rrules, - jrules: jrules - ); - - var popConfig = TypicalConfiguration.CreatePopulationConfig( - config: config, - eliteCount: eliteCount, - hillclimberMode: hillclimberMode, - resetIndividualInitialStateOperation: populationResetOperation ?? PopulationResetOperations.RandomIndependent - ); - - return popConfig; - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/M4M.Old.csproj b/M4MCode/M4M_MkI/M4M.Old/M4M.Old.csproj deleted file mode 100644 index 2c36bb186d754b561a84316a0aa0698e8e72516b..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/M4M.Old.csproj +++ /dev/null @@ -1,108 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> - <PropertyGroup> - <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> - <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProjectGuid>{8B27E40D-9DB6-4179-A2BD-E9DE907509B0}</ProjectGuid> - <OutputType>Library</OutputType> - <AppDesignerFolder>Properties</AppDesignerFolder> - <RootNamespace>M4M.Old</RootNamespace> - <AssemblyName>M4M.Old</AssemblyName> - <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> - <FileAlignment>512</FileAlignment> - <TargetFrameworkProfile /> - </PropertyGroup> - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> - <DebugSymbols>true</DebugSymbols> - <DebugType>full</DebugType> - <Optimize>false</Optimize> - <OutputPath>bin\Debug\</OutputPath> - <DefineConstants>DEBUG;TRACE</DefineConstants> - <ErrorReport>prompt</ErrorReport> - <WarningLevel>4</WarningLevel> - </PropertyGroup> - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> - <DebugType>pdbonly</DebugType> - <Optimize>true</Optimize> - <OutputPath>bin\Release\</OutputPath> - <DefineConstants>TRACE</DefineConstants> - <ErrorReport>prompt</ErrorReport> - <WarningLevel>4</WarningLevel> - </PropertyGroup> - <ItemGroup> - <Reference Include="MathNet.Numerics"> - <HintPath>..\..\..\..\Libraries\NotMine\MathNet.Numerics.dll</HintPath> - </Reference> - <Reference Include="MigraDoc.DocumentObjectModel-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL"> - <HintPath>..\packages\PDFsharp-MigraDoc-gdi.1.50.5147\lib\net20\MigraDoc.DocumentObjectModel-gdi.dll</HintPath> - </Reference> - <Reference Include="MigraDoc.Rendering-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL"> - <HintPath>..\packages\PDFsharp-MigraDoc-gdi.1.50.5147\lib\net20\MigraDoc.Rendering-gdi.dll</HintPath> - </Reference> - <Reference Include="MigraDoc.RtfRendering-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL"> - <HintPath>..\packages\PDFsharp-MigraDoc-gdi.1.50.5147\lib\net20\MigraDoc.RtfRendering-gdi.dll</HintPath> - </Reference> - <Reference Include="NetState, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\..\NetState\NetState\bin\Release\netstandard2.0\NetState.dll</HintPath> - </Reference> - <Reference Include="OxyPlot, Version=2.0.0.0, Culture=neutral, PublicKeyToken=638079a8f0bd61e9, processorArchitecture=MSIL"> - <HintPath>..\packages\OxyPlot.Core.2.0.0\lib\net45\OxyPlot.dll</HintPath> - </Reference> - <Reference Include="OxyPlot.Pdf, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c167629db8cf8ad9, processorArchitecture=MSIL"> - <HintPath>..\packages\OxyPlot.Pdf.2.0.0\lib\net452\OxyPlot.Pdf.dll</HintPath> - </Reference> - <Reference Include="PdfSharp-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL"> - <HintPath>..\packages\PDFsharp-MigraDoc-gdi.1.50.5147\lib\net20\PdfSharp-gdi.dll</HintPath> - </Reference> - <Reference Include="PdfSharp.Charting-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL"> - <HintPath>..\packages\PDFsharp-MigraDoc-gdi.1.50.5147\lib\net20\PdfSharp.Charting-gdi.dll</HintPath> - </Reference> - <Reference Include="System" /> - <Reference Include="System.Core" /> - <Reference Include="System.Drawing" /> - <Reference Include="System.Xml.Linq" /> - <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> - <Reference Include="System.Data" /> - <Reference Include="System.Net.Http" /> - <Reference Include="System.Xml" /> - </ItemGroup> - <ItemGroup> - <Compile Include="Analysis.cs" /> - <Compile Include="DefaultDensePopulationFeedback.cs" /> - <Compile Include="Epistatics\Epistatics.cs" /> - <Compile Include="Evolvability.cs" /> - <Compile Include="Experiment.cs" /> - <Compile Include="Extensions.cs" /> - <Compile Include="FearExperiments.cs" /> - <Compile Include="HModExperiments.cs" /> - <Compile Include="MiscPlotting.cs" /> - <Compile Include="OldPlotting.cs" /> - <Compile Include="OldProgram.cs" /> - <Compile Include="OneModule.cs" /> - <Compile Include="OxyPlotting.cs" /> - <Compile Include="Properties\AssemblyInfo.cs" /> - <Compile Include="Reporting\Basics.cs" /> - <Compile Include="Reporting\Figure.cs" /> - <Compile Include="Reporting\Report.cs" /> - <Compile Include="Reporting\Reporting.cs" /> - <Compile Include="Sillyness.cs" /> - <Compile Include="Topology.cs" /> - </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\M4M.Model\M4M.Model.csproj"> - <Project>{6a470149-d199-4b60-9f76-0f01fbd587e0}</Project> - <Name>M4M.Model</Name> - </ProjectReference> - <ProjectReference Include="..\M4M.New\M4M.New.csproj"> - <Project>{1e7d2bdd-30e8-48f8-9636-af93f451ec38}</Project> - <Name>M4M.New</Name> - </ProjectReference> - </ItemGroup> - <ItemGroup> - <None Include="packages.config" /> - </ItemGroup> - <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -</Project> \ No newline at end of file diff --git a/M4MCode/M4M_MkI/M4M.Old/MiscPlotting.cs b/M4MCode/M4M_MkI/M4M.Old/MiscPlotting.cs deleted file mode 100644 index 112f5123559e863f3c906307c70a335b851f6538..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/MiscPlotting.cs +++ /dev/null @@ -1,159 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Diagnostics; -using Linear = MathNet.Numerics.LinearAlgebra; // reasonable quality API reference here: https://numerics.mathdotnet.com/api/MathNet.Numerics.LinearAlgebra/ (inline isn't good enogh) -using RandomSource = MathNet.Numerics.Random.RandomSource; -using static M4M.Misc; - -namespace M4M -{ - public static class MiscPlotting - { - // plotting stuff - public static void PlotVector(string name, string filename, Linear.Vector<double> vector, double plotSize = 1, double? min = null, double? max = null) - { - double[,] darr = vector.ToColumnMatrix().Transpose().ToArray(); - PlotHeatmap(name, filename, darr, false, false, plotSize: plotSize, min: min, max: max); - } - - public static void PlotVectorFolded(string name, string filename, Linear.Vector<double> vector, string xTitle = "X", string yTitle = "Y", double plotSize = 1, double? min = null, double? max = null) - { - int foldCount = (int)Math.Round(Math.Sqrt(vector.Count)); - Debug.Assert(foldCount * foldCount == vector.Count, $"Vector Length ({vector.Count}) must be a square number"); - - PlotVectorFolded(name, filename, vector, foldCount, xTitle, yTitle, plotSize, min: min, max: max); - } - - public static void PlotVectorFolded(string name, string filename, Linear.Vector<double> vector, int foldCount, string xTitle = "X", string yTitle = "Y", double plotSize = 1, double? min = null, double? max = null) - { - Debug.Assert(vector.Count % foldCount == 0, $"Vector Length ({vector.Count}) must be an integer divisor of the foldCount ({foldCount}"); - - double[][] darr = Unroll(vector.ToArray(), foldCount); - PlotHeatmap(name, filename, darr, false, false, xTitle, yTitle, plotSize: plotSize, min: min, max: max); - } - - public static void PlotMatrix(string name, string filename, Linear.Matrix<double> matrix, string yTitle = "Y", string xTitle = "X", bool labels = false, double plotSize = 1, double? min = null, double? max = null) - { - double[,] darr = matrix.Transpose().ToArray(); - PlotHeatmap(name, filename, darr, false, true, xTitle, yTitle, labels, plotSize, min: min, max: max); - } - - public static void PlotLines(string name, string filename, double[][] data, double x0 = 0, double dx = 1, string xTitle = "X", string yTitle = "Y", string[] seriesLabels = null, double plotSize = 1) - { - // plot - Plotting.OxyPlotModelShim plot = new Plotting.OxyPlotModelShim(name); - - plot.LinearX(xTitle); - plot.LinearY(yTitle); - - for (int i = 0; i < data.Length; i++) - { - double[] d = data[i]; - string label = seriesLabels != null ? seriesLabels[i] : ""; - plot.PlotLine(label, d, x0, dx); - } - - plot.ExportPdf(filename, plotSize); - } - - public static void PlotLines(string name, string filename, IDictionary<double, double>[] data, string xTitle = "X", string yTitle = "Y", string[] seriesLabels = null, double plotSize = 1) - { - // plot - Plotting.OxyPlotModelShim plot = new Plotting.OxyPlotModelShim(name); - - plot.LinearX(xTitle); - plot.LinearY(yTitle); - - for (int i = 0; i < data.Length; i++) - { - IDictionary<double, double> d = data[i]; - string label = seriesLabels != null ? seriesLabels[i] : ""; - plot.PlotLine(label, d.Select(kv => new Tuple<double, double>(kv.Key, kv.Value)).OrderBy(kv => kv.Item1)); - } - - plot.ExportPdf(filename, plotSize); - } - - public static void PlotLine(string name, string filename, double[] data, double x0 = 0, double dx = 1, string xTitle = "X", string yTitle = "Y", double plotSize = 1) - { - // plot - Plotting.OxyPlotModelShim plot = new Plotting.OxyPlotModelShim(name); - - plot.LinearX(xTitle); - plot.LinearY(yTitle); - - plot.PlotLine("", data, x0, dx); - - plot.ExportPdf(filename, plotSize); - } - - public static void PlotHeatmap(string name, string filename, double[][] darr, bool flipX = false, bool flipY = false, string xTitle = "X", string yTitle = "Y", bool labels = false, double plotSize = 1, double? min = null, double? max = null) - { - double[,] ndarr = new double[darr[0].Length, darr.Length]; - - for (int j = 0; j < darr.Length; j++) - { - for (int i = 0; i < darr[0].Length; i++) - { - ndarr[i, j] = darr[j][i]; - } - } - - PlotHeatmap(name, filename, ndarr, flipX, flipY, xTitle, yTitle, labels, plotSize, min: min, max: max); - } - - public static void PlotHeatmap(string name, string filename, double[,] darr, bool flipX = false, bool flipY = false, string xTitle = "X", string yTitle = "Y", bool labels = false, double plotSize = 1, double? min = null, double? max = null) - { - // pad - bool padX = false; - if (darr.GetLength(0) <= 1) - { - padX = true; - - var ndarr = new double[2, darr.GetLength(1)]; - for (int i = 0; i < darr.GetLength(1); i++) - { - ndarr[0, i] = darr[0, i]; - ndarr[1, i] = darr[0, i]; - } - darr = ndarr; - } - bool padY = false; - if (darr.GetLength(1) <= 1) - { - padY = true; - - var ndarr = new double[darr.GetLength(0), 2]; - for (int i = 0; i < darr.GetLength(0); i++) - { - ndarr[i, 0] = darr[i, 0]; - ndarr[i, 1] = darr[i, 0]; - } - darr = ndarr; - } - - // plot - Plotting.OxyPlotModelShim plot = new Plotting.OxyPlotModelShim(name); - - plot.LinearX(label : xTitle); - plot.LinearY(label : yTitle, flip: true); - plot.ColoursGrey(min : min, max : max); - double x0 = padX ? -1.0 : 0.0; - double x1 = padX ? 1.0 : darr.GetLength(0) - 1.0; - double y0 = padY ? -1.0 : 0.0; - double y1 = padY ? 1.0 : darr.GetLength(1) - 1.0; - var hmap = plot.PlotHeatmap("", darr, x0, x1, y0, y1); - plot.Sillyify(Clamp(darr.GetLength(0), 1, 10), "x"); - plot.Sillyify(Clamp(darr.GetLength(1), 1, 10), "y"); - - if (labels) - hmap.LabelFontSize = 0.2; - - - plot.ExportPdf(filename, plotSize); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/OldPlotting.cs b/M4MCode/M4M_MkI/M4M.Old/OldPlotting.cs deleted file mode 100644 index d7d22c9f6a5732ff270bce0bbc0fd8e15e362568..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/OldPlotting.cs +++ /dev/null @@ -1,240 +0,0 @@ -using M4M; -using System.Linq; - -namespace Plotting -{ - public class PdfExportReport - { - public PdfExportReport(string filename, double exportedWidth, double exportedHeight) - { - Filename = filename; - ExportedWidth = exportedWidth; - ExportedHeight = exportedHeight; - } - - public string Filename { get; } - public double ExportedWidth { get; } - public double ExportedHeight { get; } - } - - public static class OldOxyPlottingExtentions - { - public static OxyPlot.Axes.Axis GetAxis(this OxyPlot.PlotModel model, string key) - { - // surly there is a method/indexer for this? - return model.Axes.First(a => a.Key == key); - } - } - - public static class OldOxyPlotting - { - public static System.Diagnostics.Process ViewPdf(string name) - { - if (!name.EndsWith(".pdf")) - name += ".pdf"; - - try - { - return System.Diagnostics.Process.Start(name); - } - catch - { - return null; // high quality code - } - } - - public static void ExportToPdf(OxyPlot.PlotModel model, string ofname, bool bigger = true, bool noHalf = false, bool forceAspect = false, PostProcessPlot postProcessor = null) - { - ExportToPdf(model, ofname, 1.0, bigger, forceAspect, postProcessor); - if (!noHalf) - ExportToPdf(model, ofname + "_half", 0.5, bigger, forceAspect, postProcessor); - } - - public static void DummyExport(OxyPlot.PlotModel model) - { - OxyPlot.Pdf.PdfExporter exporter = new OxyPlot.Pdf.PdfExporter(); - double Width = 297 / 25.4 * 72 * 1.0; // A4 - double Height = 210 / 25.4 * 72 * 1.0; - - exporter.Width = Width; - exporter.Height = Height; - - model.InvalidatePlot(true); - - using (var fs = new System.IO.MemoryStream()) - { - exporter.Export(model, fs); - } - } - - public static PdfExportReport ExportToPdf(OxyPlot.PlotModel model, string ofname, double size, bool bigger, bool forceAspect, PostProcessPlot postProcessor) - { - double width = 297 / 25.4 * 72 * size; // A4 - double height = 210 / 25.4 * 72 * size; - - return ExportToPdf(model, ofname, width, height, bigger, forceAspect, postProcessor); - } - - public static PdfExportReport ExportToPdf(OxyPlot.PlotModel model, string ofname, double width, double height, bool bigger, bool forceAspect, PostProcessPlot postProcessor) - { - double fc = 1.2; - double prc = 2.0; - if (model.Axes.Count > 2) - prc = 0.0; - - model.Padding = new OxyPlot.OxyThickness(0.0, 0.0, model.Padding.Right * prc, 0.0); // Yan says there shouldn't be any whitespace - model.TitleFontSize *= fc; - model.DefaultFontSize *= fc; - - if (bigger) - { - foreach (var s in model.Series) - { - if (s is OxyPlot.Series.LineSeries) - ((OxyPlot.Series.LineSeries)s).StrokeThickness *= 2.0; - } - model.LegendFontSize *= fc; - } - - ofname = ofname.EndsWith(".pdf") ? ofname : ofname + ".pdf"; - - OxyPlot.Pdf.PdfExporter exporter = new OxyPlot.Pdf.PdfExporter(); - - exporter.Width = width; - exporter.Height = height; - - model.InvalidatePlot(true); - - void doPlot() - { - using (var fs = new System.IO.FileStream(ofname, System.IO.FileMode.Create)) - { - exporter.Export(model, fs); - } - } - - void doForceAspect() - { - if (model.PlotArea.Width > model.PlotArea.Height) - { - width = width - model.PlotArea.Width + model.PlotArea.Height; - } - else - { - height = height + model.PlotArea.Width - model.PlotArea.Height; - } - - exporter.Width = width; - exporter.Height = height; - } - - doPlot(); - - if (forceAspect) - { - doForceAspect(); - doPlot(); - } - - if (postProcessor != null) - { - postProcessor(model); - doPlot(); - - if (forceAspect) - { - doForceAspect(); - doPlot(); - } - } - - return new PdfExportReport(ofname, width, height); - } - - public static OxyPlot.PlotModel RecolourLineSeries(OxyPlot.PlotModel model, OxyPlot.OxyColor from, OxyPlot.OxyColor to) - { - OxyPlot.Series.LineSeries[] ss = model.Series.Where(s => s is OxyPlot.Series.LineSeries).Select(s => s as OxyPlot.Series.LineSeries).ToArray(); - - for (int i = 0; i < ss.Length; i++) - { - ss[i].Color = OxyPlot.OxyColor.Interpolate(from, to, (double)i / (ss.Length > 1 ? ss.Length - 1 : 1)); - } - - return model; - } - - public static OxyPlot.PlotModel LinearAxes(string title, string xlabel, string ylabel, string ylabel2 = null) - { - OxyPlot.PlotModel model = new OxyPlot.PlotModel() { Title = title }; - - model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Key = "x", Title = xlabel, Position = OxyPlot.Axes.AxisPosition.Bottom, StartPosition = 0, EndPosition = 1 }); - model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Key = "y", Title = ylabel, Position = OxyPlot.Axes.AxisPosition.Left, StartPosition = 0, EndPosition = 1 }); - if (ylabel2 != null) - model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Key = "y2", Title = ylabel2, Position = OxyPlot.Axes.AxisPosition.Right, StartPosition = 0, EndPosition = 1 }); - - return model; - } - - public static OxyPlot.PlotModel PlotFreeLines(OxyPlot.PlotModel model, double strokeWidth, System.Collections.Generic.IEnumerable<System.Tuple<string, System.Collections.Generic.IEnumerable<System.Tuple<double, double>>>> values, string yAxisKey = "y") - { - foreach (System.Tuple<string, System.Collections.Generic.IEnumerable<System.Tuple<double, double>>> vs in values) - { - string sname = vs.Item1; - System.Collections.Generic.IEnumerable<System.Tuple<double, double>> vals = vs.Item2; - - //OxyPlot.Series.StairStepSeries series = new OxyPlot.Series.StairStepSeries(); - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries(); - series.Title = sname; - - foreach (System.Tuple<double, double> d in vals) - { - series.Points.Add(d == null ? OxyPlot.DataPoint.Undefined : new OxyPlot.DataPoint(d.Item1, d.Item2)); - } - - series.StrokeThickness = strokeWidth; - series.YAxisKey = yAxisKey; - - model.Series.Add(series); - } - - return model; - } - - public static OxyPlot.PlotModel PlotFunc(OxyPlot.PlotModel model, double strokeWidth, string label, System.Func<double, double> func, double x0, double x1, double n, string yAxisKey = "y") - { - string sname = label; - - OxyPlot.Series.FunctionSeries series = new OxyPlot.Series.FunctionSeries(func, x0, x1, n); - series.Title = sname; - - series.StrokeThickness = strokeWidth; - series.YAxisKey = yAxisKey; - - model.Series.Add(series); - - return model; - } - - public static OxyPlot.PlotModel PlotLine(OxyPlot.PlotModel model, double strokeWidth, string label, System.Collections.Generic.IEnumerable<System.Tuple<double, double>> values, string yAxisKey = "y") - { - string sname = label; - System.Collections.Generic.IEnumerable<System.Tuple<double, double>> vals = values; - - //OxyPlot.Series.StairStepSeries series = new OxyPlot.Series.StairStepSeries(); - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries(); - series.Title = sname; - - foreach (System.Tuple<double, double> d in vals) - { - series.Points.Add(d == null ? OxyPlot.DataPoint.Undefined : new OxyPlot.DataPoint(d.Item1, d.Item2)); - } - - series.StrokeThickness = strokeWidth; - series.YAxisKey = yAxisKey; - - model.Series.Add(series); - - return model; - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/OldProgram.cs b/M4MCode/M4M_MkI/M4M.Old/OldProgram.cs deleted file mode 100644 index 001ef5c3dd7af31d7b480555bdfd1f318d769b22..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/OldProgram.cs +++ /dev/null @@ -1,2947 +0,0 @@ -using OxyPlot; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Linear = MathNet.Numerics.LinearAlgebra; -using RandomSource = MathNet.Numerics.Random.RandomSource; -using static M4M.Misc; -using static M4M.MiscPlotting; -using static M4M.Analysis; -using OxyPlot.Axes; -using OxyPlot.Series; -using M4M.Modular; - -namespace M4M.Old -{ - public class Program - { - // should really be generic in the string term, and take a ToString func - public static void WriteStuff(System.IO.StreamWriter writer, IDictionary<Tuple<double, double>, string> stuff, string lTitle, string tTitle) - { - writer.Write(lTitle + "\\" + tTitle); - foreach (var t in stuff.Keys.Select(k => k.Item2).Distinct().OrderBy(k => k)) - { - writer.Write("\t"); - writer.Write(t); - } - - foreach (var l in stuff.Keys.Select(k => k.Item1).Distinct().OrderBy(k => k)) - { - writer.WriteLine(); - writer.Write(l); - foreach (var t in stuff.Keys.Select(k => k.Item2).Distinct().OrderBy(k => k)) - { - writer.Write("\t"); - writer.Write(stuff[new Tuple<double, double>(l, t)].ToString()); - } - } - } - - private static void ConfigureCulture() - { - System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; - } - - private static void ConfigureMathNet(bool quiet) - { - MathNet.Numerics.Control.UseSingleThread(); - //bool t = MathNet.Numerics.Control.TryUseNativeCUDA(); - //if (t) - // Console.WriteLine("CUDA"); - - if (MathNet.Numerics.Control.TryUseNativeOpenBLAS() && !quiet) - Console.WriteLine("OpenBLAS"); - } - - private static void PalinHCTest() - { - Console.WriteLine("# HC"); - MatrixPool.EnableMatrixPools = true; - - var iorand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var rand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var context = new ModelExecutionContext(rand); - - var exp = HModExperiments.PrepareClassicM4M("C:/M4MTest/hc", "hc", Modular.ModularTargetPackage.Original4x4, 1000, 0.2, 20000, JudgementRules.L1Equivalent, false, false, 1, false, false, 10); - var feedback = new CommonDensePopulationExperimentFeedback(System.Console.Out, iorand, exp.PopulationConfig, false, false, 1, 1, null, 1000, 100000, 10000000, 0, 1); - exp.Population.PopulationParallelism = 1; - - System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); - sw.Start(); - PopulationExperimentRunners.RunEpochs(System.Console.Out, "", 1, context, exp, null); - sw.Stop(); - - var matPool = context.GetMatrixPool(16, 16); - Console.WriteLine("CacheHits: " + matPool.CacheHits); - Console.WriteLine("CacheMisses: " + matPool.CacheMisses); - Console.WriteLine("CacheCaps: " + matPool.CacheCaps); - - Console.WriteLine("HC" + ": " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); - } - - private static void ParallelPopulationTest(int tasks) - { - Console.WriteLine("# P" + tasks); - - var iorand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var rand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var context = new ModelExecutionContext(rand); - - var exp = HModExperiments.PreparePopulationM4M("C:/M4MTest/p" + tasks, "p" + tasks, Modular.ModularTargetPackage.Original4x4, 1000, 0.2, 20000, JudgementRules.L1Equivalent, false, false, 16, 1, true, false, false); - var feedback = new CommonDensePopulationExperimentFeedback(System.Console.Out, iorand, exp.PopulationConfig, false, false, 1, 1, null, 1000, 100000, 10000000, 0, 1); - exp.Population.PopulationParallelism = tasks; - - System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); - sw.Start(); - PopulationExperimentRunners.RunEpochs(System.Console.Out, "", 1, context, exp, null); - sw.Stop(); - - var matPool = context.GetMatrixPool(16, 16); - Console.WriteLine("CacheHits: " + matPool.CacheHits); - Console.WriteLine("CacheMisses: " + matPool.CacheMisses); - Console.WriteLine("CacheCaps: " + matPool.CacheCaps); - - Console.WriteLine("P" + tasks + ": " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); - } - - public static void Main(string[] args) - { -#if false - while (true) - { - PalinHCTest(); - ParallelPopulationTest(1); - ParallelPopulationTest(2); - //ParallelPopulationTest(4); - //ParallelPopulationTest(8); - } - Console.ReadKey(true); - return; -#endif - - //var exp = PopulationExperiment<DenseIndividual>.Load(@"C:\M4MResults\cbbnk\mmsolinstep4x4_long\medk1\epoch10000save.dat"); - //exp.RunEpoch(new MathNet.Numerics.Random.MersenneTwister(false), null); - - //return; - - bool quiet = args.Contains("quiet"); - - if (!quiet) - Console.WriteLine("~~ M4M Framework Running ~~"); - ConfigureCulture(); - ConfigureMathNet(quiet); - - Cli cli = Cli.PrepareDefaultCli( - plotExporter: new PdfPlotExporter() - ); - CliPrompt cliPrompt = new CliPrompt(cli); - cliPrompt.Run(System.Console.Out, args); - - //TimeGeneration(); - //TimeTanh(); - //StateTesting(); - //Console.ReadKey(); - - //HModStuff(); - //CbbnkStuff(); - //FearStuff(); - } - - private static void TimeTanh() - { - int n = 100000; - int rr = 1000; - double[] x = new double[n]; - double[] y = new double[n]; - - Random rnd = new Random(); - for (int i = 0; i < n; i++) - x[i] = rnd.NextDouble() + 0.2; - - // warmup - for (int i = 0; i < n; i++) - y[i] = Math.Tanh(x[i]); - - System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); - sw.Restart(); - - for (int r = 0; r < rr; r++) - { - for (int i = 0; i < n; i++) - y[i] = Math.Tanh(x[i]); - } - - sw.Start(); - Console.WriteLine(sw.ElapsedMilliseconds); - Console.WriteLine(sw.ElapsedMilliseconds / (double)(n * rr)); - Console.WriteLine(y.Sum()); - } - - private static void TimeGeneration() - { - int rr = 100; - int n = 20000 / rr; - - var rand = new MathNet.Numerics.Random.MersenneTwister(false); - var context = new ModelExecutionContext(rand); - var drules = TypicalConfiguration.CreateStandardDevelopmentRules(DevelopmentRules.TanhHalf); - var rrules = TypicalConfiguration.CreateStandardReproductionRules(0.1, 0.1 / (15 * 15), 1.0, false); - var jrules = TypicalConfiguration.CreateStandardJudgementRules(0.1, JudgementRules.L1Equivalent, 0.0); - var s = ModularTargetPackage.Original4x4.Targets[0]; - s.NextGeneration(rand, jrules); - - // warmup - DenseIndividual di = DenseIndividual.Develop(DenseGenome.CreateDefaultGenomeDense(16, customDevelopmentStep: new DefaultDevelopmentStep(16, 0.0)), context, drules, false); - IndividualJudgement<DenseIndividual> dij = new IndividualJudgement<DenseIndividual>(di, MultiMeasureJudgement.Judge(di.Genome, di.Phenotype, jrules, s)); - - for (int i = 0; i < n; i++) - { - s.NextGeneration(rand, jrules); - - var di2 = di.Mutate(context, drules, rrules); - var dij2 = new IndividualJudgement<DenseIndividual>(di2, MultiMeasureJudgement.Judge(di2.Genome, di2.Phenotype, jrules, s)); - - if (dij2.Judgement.CombinedFitness > dij.Judgement.CombinedFitness) - { - di = di2; - dij = dij2; - } - } - - // go - - for (int q = 0; q < 10; q++) - { - // re-prep - di = DenseIndividual.Develop(DenseGenome.CreateDefaultGenomeDense(16, customDevelopmentStep: new DefaultDevelopmentStep(16, 0.0)), context, drules, false); - dij = new IndividualJudgement<DenseIndividual>(di, di.Judge(jrules, s)); - - Population<DenseIndividual> pop = new Population<DenseIndividual>(di, 1); - - System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); - sw.Restart(); - - for (int r = 0; r < rr; r++) - { - for (int i = 0; i < n; i++) - { - //s.NextGeneration(rand, jrules); - - pop.CyclePopulation(context, jrules, s, rrules, drules, null, 1, true, null); - - // simulate t16 (i.e. development) - //double x = 0.99; - //for (int j = 0; j < 10 * 16; j++) - //{ - // double y = Math.Tanh(x); - // for (int k = 0; k < 16; k++) - // x *= x; - //} - - //var p2 = di.Genome.Develop(rand, drules); - - //var j = di.Judge(jrules, s); - //var g2 = di.Genome.Mutate(rand, rrules); - //var p2 = g2.Develop(rand, drules); - //var j2 = MultiMeasureJudgement.Judge(g2, p2, jrules, s); - - //dij = new IndividualJudgement<DenseIndividual>(di, di.Judge(jrules, s)); - //var di2 = di.Mutate(rand, drules, rrules); - //var dij2 = new IndividualJudgement<DenseIndividual>(di2, di2.Judge(jrules, s)); - - //if (dij2.Judgement.CombinedFitness > dij.Judgement.CombinedFitness) - //{ - // di = di2; - // dij = dij2; - //} - } - - //new RandomIndependantPopulationResetOperation().Reset(rand, pop, new Misc.Range(-1, 1), drules, s); - } - - sw.Start(); - Console.WriteLine(sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(sw.ElapsedMilliseconds / (double)(n * rr) + "ms/gen"); - Console.WriteLine(1000000 * sw.ElapsedMilliseconds / (double)(n * rr) + "ns/gen"); - Console.WriteLine(di.Genome.InitialState[0]); - } - } - - public static void StateTesting() - { - DenseGenome dg = DenseGenome.CreateDefaultGenome(16, new Columns2TransMatMutator(16, 4), null, new DefaultDevelopmentStep(16, 0.0)); - var popconfig = PrepareConfig(Epistatics.BBNKTargetPackages.CbbnkStep4x4Variable(0.5, 1.0, 0.1), 5000, 1000, 0.2); - var context = new ModelExecutionContext(new MathNet.Numerics.Random.MersenneTwister(false)); - var pop = new Population<DenseIndividual>(DenseIndividual.Develop(DenseGenome.CreateDefaultGenome(16), context, popconfig.ExperimentConfiguration.DevelopmentRules, false), 1); - var extracts = pop.ExtractAll().ToList(); - - using (var ms = new System.IO.MemoryStream()) - { - State.GraphSerialisation.Write(dg, ms); - State.GraphSerialisation.Write(popconfig, ms); - State.GraphSerialisation.Write(extracts, ms); - State.GraphSerialisation.Write(pop, ms); - - ms.Position = 0; - - var dg2 = State.GraphSerialisation.Read<DenseGenome>(ms); - var popconfig2 = State.GraphSerialisation.Read<PopulationExperimentConfig<DenseIndividual>>(ms); - var meh = State.GraphSerialisation.Read<IReadOnlyList<DenseIndividual>>(ms); - var pop2 = State.GraphSerialisation.Read<Population<DenseIndividual>>(ms); - - Console.ReadKey(true); - } - } - - public static PopulationExperimentConfig<DenseIndividual> PrepareConfig(Epistatics.EpistaticTargetPackage targetPackage, int epochs, int K, double λ, double gNoiseTerm = 0.6, double bNoiseTerm = 0.00001, int eliteCount = 1, bool hillclimberMode = true, IRegularisationFunction<IGenome> regularisationFunction = null, double bMutationProbability = 1.0, bool exclusiveBMutation = false, bool binaryG = false, double gResetProb = 1.0) - { - int N = targetPackage.TargetSize; - - var drules = TypicalConfiguration.CreateStandardDevelopmentRules( - squash: DevelopmentRules.TanhHalf - ); - - var rrules = TypicalConfiguration.CreateStandardReproductionRules( - gMutationMagnitude: gNoiseTerm, - gMutationType: binaryG ? NoiseType.Binary : NoiseType.Gaussian, - bMutationMagnitude: bNoiseTerm, - bMutationProbability: bMutationProbability, - bMutationType: NoiseType.Uniform, - exclusiveBMutation: exclusiveBMutation - ); - - var jrules = TypicalConfiguration.CreateStandardJudgementRules( - regularisationFactor: λ, - regularisationFunction: regularisationFunction, - noiseFactor: 0.0 - ); - - var config = TypicalConfiguration.CreateConfig( - N: N, - targets: targetPackage.Targets, - targetCycler: Cyclers.Loop, - epochs: epochs, - generationsPerTargetPerEpoch: K, - gResetProb: gResetProb, - gResetRange: new Range(-1.0, 1.0), - drules: drules, - rrules: rrules, - jrules: jrules - ); - - var popConfig = TypicalConfiguration.CreatePopulationConfig( - config: config, - eliteCount: eliteCount, - hillclimberMode: hillclimberMode, - resetIndividualInitialStateOperation: gResetProb == 0 ? PopulationResetOperations.None : (binaryG ? PopulationResetOperations.RandomBinaryIndependent : PopulationResetOperations.RandomIndependent) - ); - - return popConfig; - } - - public static void HModStuff() - { - //HModExperiments.M4MPerfectG(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 2500, plotPeriod: 1000, tracePeriod: 10000), null); - //HModExperiments.ClassicM4M(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 2500, plotPeriod: 1000, tracePeriod: 10000), null); - } - - public static void CbbnkStuff() - { - //CbbnkExperiments.MMSOLinStep(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000)); - //CbbnkExperiments.MMSOLinStep3x2(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000)); - //CbbnkExperiments.MMSOLinStep2x3(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000)); - //CbbnkExperiments.MMSOLinStep3x3(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000), IsWatchPopConfigor()); - //CbbnkExperiments.MMSOLinStep4x4(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000), IsWatchPopConfigor - //CbbnkExperiments.MMSOLinStep4x4Long(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 2500, plotPeriod: 1000, tracePeriod: 10000), IsWatchPopConfigor()); - - // continuations - //var oldDir = @"C:\M4MExperiments\MMSOLinStep4x4\shortblock1\q0.8a2ω0.8λ1CbbnkStep4x4P0.8_0.5_0.1_3MVG_20180830T112908\r0"; - //CbbnkExperiments.MMSOLinStep4x4Continue(oldDir, 1.0, DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 2000, tracePeriod: 2000), IsWatchPopConfigor()); - - var zeroDir = @"C:\M4MExperiments\ZERO\MMSOLinStep4x4Long\block2\M_G0.65M_B1.5E-05CbbnkStep4x4P0.8_0.5_0.1_3MVG_20180903T233033\r0"; - var zeroExperiment = PopulationExperiment<DenseIndividual>.Load(zeroDir + @"\epoch0savestart.dat"); - zeroExperiment = PopulationExperimentRunners.PrepareExperiment<DenseIndividual>(zeroExperiment.Population, zeroExperiment.PopulationConfig, zeroDir + "rerun"); - var rand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var ioRand = new MathNet.Numerics.Random.MersenneTwister(0, false); - var context = new ModelExecutionContext(rand); - var feedback = DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 2500, plotPeriod: 1000, tracePeriod: 10000).Invoke(ioRand, zeroExperiment.PopulationConfig, null); - PopulationExperimentRunners.WriteOutInitials(rand, zeroExperiment); - PopulationExperimentRunners.RunEpochs(System.Console.Out, "", zeroExperiment.PopulationConfig.ExperimentConfiguration.Epochs, context, zeroExperiment, feedback.Feedback); - } - - public static void FearStuff() - { - //FearExperiments.Fear4VaryZ(@"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000), IsWatchPopConfigor()); - FearExperiments.Fear4x4(System.Console.Out, @"C:\M4MExperiments\", DefaultDensePopulationExperimentFeedback.SimpleDefaultDensePopulationExperimentFeedback(savePeriod: 500, plotPeriod: 1000, tracePeriod: 2000)); - } - - static void OldMain(string[] args) - { - throw new Exception("Should not be using OldMain"); - - //var g = Analysis.LoadGenome(@"C:\M4MExperiments\MMSOLinVaryHighQAB\uncruelshort7\q0.5a5CbbnkCruel2x2P1_0.5_2\EpistaticPopTestBBNKCruelCbbnkCruel2x2P1_0.5_2_λ0.2EpiFalsemG0.6mB1E-05E5000K1000T10PF.200G1_20180818T005656\genome200.dat"); - - - //return; - - //ConfigureMathNet(false); - - //TestExperiment(); - //L1SpikyTargetExperiments(); - //L1SpikyTargetCountingExperiments(); - //DiffModExperiment(); - - //EvolvabilityTest(); - //LeaderTest(); - - //IdentityExperiment(); - //for(int i=0;i<4;i++) - // ColumnExperiment(); - - //HorrifyingStuff(); - - //ToplogyExperiments(); - - //TestExperiment_SoloTarget(); // for MA - - //CutoffTargetExperiment(); - - //DistanceFitnessExperiments(); - - //HierarchyForProfitExperiments(); // come back to this - - //FourTwoFourWheelers(); - - //Ands(); - - //Paushes(); - - //SharingExperiments(); - - //PopulationTests(); - //Fear(FileStuff.CreateNow("fear", "", "")); // as expected... - - //Ev2Tests(); - - //MorePopulationStuff(); - - //GradientMeasuring(); - - //Ev2Tests(); - - //WeirdStuff(); - - //int N = 16; - - // LNo and L1 are fine, L2 (overfits) - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.ConstantEquivalent, λ: 0, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - // these are all just slower versions of the above, all going to overfit - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 5_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 10_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 40_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - // doesn't seem to go wrong... - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 0.38, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - // ... tighten λ: nothing - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38.0/N, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - // ... run it long: nothing - //PopNotExperiment(targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 0.38, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - //SupportingFigures(); - - // nice runs - //PopNotExperiment(customPrefix: "L1 λ=0.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.00, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - // need a long one - //PopNotExperiment(customPrefix: "L1 λ=0.8 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.8, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - /* - var drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - var expected = ModularTargetPackage.Targets4x4Total.ToDictionary(t => t, t => 1.0 / 16); - var gno = LoadGenomes(@"C:\WORK\RAW\raw\Code\Other\M4M_MkI\M4M\bin\x64\Release\PopNotOrg4x4λ0Noise0.1E150K20000CF10_20180524T144040", drules, expected).ToDictionary(sg => sg.Epoch, sg => sg); - var gl1 = LoadGenomes(@"C:\WORK\RAW\raw\Code\Other\M4M_MkI\M4M\bin\x64\Release\PopNotOrg4x4λ0.22Noise0.1E150K20000CF10_20180524T115521", drules, expected).ToDictionary(sg => sg.Epoch, sg => sg); - - IDictionary<double, double> entropy(IDictionary<int, SavedGenome> dict) => dict.ToDictionary(kv => (double)kv.Key, kv => kv.Value.GenomeDistibutionInfo.Entropy); - PlotLines("Entropy", "entropy.pdf", new[] { entropy(gno), entropy(gl1) }, "Epoch", "Entropy", new[] { "L1 λ = 0.00", "L1 λ = 0.22" }, 0.6); - - IDictionary<double, double> error(IDictionary<int, SavedGenome> dict) => dict.ToDictionary(kv => (double)kv.Key, kv => kv.Value.GenomeDistibutionInfo.Error); - PlotLines("Error", "error.pdf", new[] { error(gno), error(gl1) }, "Epoch", "Chi-Squared Error", new[] { "L1 λ = 0.00", "L1 λ = 0.22" }, 0.6); - */ - - //Type2EvolvabilityTest4(1); - //Type2EvolvabilityTest4(2); - //Type2EvolvabilityTest4(4); - - //Epistatics.BbnkStuff.Cbbnk2x2TestVeryLong(null, null, false); - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1(null, null, false); - //Epistatics.BbnkStuff.Cbbnk2x2DivergenceTests(); - - //Epistatics.BbnkStuff.Cbbnk2x2GNoiseTests(); - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1("BBNK", null, null, false, 0.6); - //Epistatics.BbnkStuff.Cbbnk3x3TestRatherLongG1("BBNK", null, null, true, gNoiseTerm: 0.5, λ : 0.2); - //Epistatics.BbnkStuff.Cbbnk3x3TestRatherLongG1("BBNK", null, null, true, gNoiseTerm: 0.5, λ : 0.3); - //Epistatics.BbnkStuff.Cbbnk3x3TestRatherLongG1("BBNK", null, null, true, gNoiseTerm: 0.5, λ : 0.1); - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongVariableG("C:\\M4MExperiments\\BBNKMVG", null, null, true, gNoiseTerm: 0.6, λ: 0.2); - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongVariableB("C:\\M4MExperiments\\BBNKMVG", null, null, true, λ: 0.2); - - //MiscPlotting.PlotMatrix("MC10x6", "C:\\M4MExperiments\\MC\\mc10x6", Epistatics.MCTarget.PrepareMCMatrixCheckCheck(10, 6, 0.33)); - - //var phenotype = new Phenotype(Epistatics.MCTarget.PrepareMCIdealVectorCheckCheck(10, 6)); - //var target = Epistatics.MCTargetPackages.MC(10, 6, 0.1).Targets[0]; - //target.NextGeneration(null, new JudgementRules(0, null, 0)); - //Console.WriteLine(target.Judge(phenotype)); - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(2, 2, 0.1), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.002); - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(2, 2, 0.1), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.0002); - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(10, 6, 0.1), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.002); - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(10, 6, 0.1), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.0002); - - // this has a higher lambda, and M_B - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(10, 6, 0.01), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.004, 0.00005); - - // back to the old model - //Epistatics.MCStuff.MCTestRatherLongG(Epistatics.MCTargetPackages.MCCheckOnCheck(10, 6, 0.01), "C:\\M4MExperiments\\MC", null, null, false, 0.6, 0.0002, 0.00001); - - // approximately Dave's config - //Epistatics.MCStuff.MCTestG(Epistatics.MCTargetPackages.MCCheckOnCheck(10, 6, 0.01), "C:\\M4MExperiments\\MC", null, null, true, 0.0, 0.0000, 0.00005, 200, 20000, 20); - //Epistatics.MCStuff.MCTestG(Epistatics.MCTargetPackages.MCCheckOnPlain(10, 6, 0.01), "C:\\M4MExperiments\\MC", null, null, true, 0.0, 0.0000, 0.00005, 200, 20000, 20); - - // 3x2 - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel3x2Variable(0.5), "C:\\M4MExperiments\\BBNKMVG3x2", null, null, true, λ: 0.10, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel3x2Variable(0.5), "C:\\M4MExperiments\\BBNKMVG3x2", null, null, true, λ: 0.15, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel3x2Variable(0.5), "C:\\M4MExperiments\\BBNKMVG3x2", null, null, true, λ: 0.20, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel3x2Variable(0.5), "C:\\M4MExperiments\\BBNKMVG3x2", null, null, true, λ: 0.25, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel3x2Variable(0.5), "C:\\M4MExperiments\\BBNKMVG3x2", null, null, true, λ: 0.30, K: 1000, epochs: 500000); - - // 2x3 - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.10, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.15, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.20, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.25, K: 1000, epochs: 500000); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.30, K: 1000, epochs: 500000); - - // high λ, low M_G - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.30, K: 1000, epochs: 100000, gNoiseTerm: 0.4); - - // high λ, L1 - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.40, K: 1000, epochs: 20000, gNoiseTerm: 0.6); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.50, K: 1000, epochs: 20000, gNoiseTerm: 0.6); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.60, K: 1000, epochs: 20000, gNoiseTerm: 0.6); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3", null, null, true, λ: 0.70, K: 1000, epochs: 20000, gNoiseTerm: 0.6); - - // high λ, LHalf (only λ = 0.01 shows any promise) - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.010, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.020, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.030, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.040, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.050, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.060, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.BbnkStuff.CbbnkGeneralG(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.070, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNK", Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.010, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNK", Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.015, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNK", Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.015, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent); - - // pre-determined start points - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNK", Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3L1", null, null, true, λ: 0.2, K: 1000, epochs: 500000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent, resetIndividualInitialStateOperation: Epistatics.EpistaticStuff.ResetIndividualInitialStateOperationSampleSimilar(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5).Targets.Select(t => ((VectorTarget)t).Vector * -0.5))); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNK", Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5), "C:\\M4MExperiments\\BBNKMVG2x3LHalf", null, null, true, λ: 0.015, K: 1000, epochs: 500000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.LHalfEquivalent, resetIndividualInitialStateOperation: Epistatics.EpistaticStuff.ResetIndividualInitialStateOperationSampleSimilar(Epistatics.BBNKTargetPackages.CbbnkCruel2x3Variable(0.5).Targets.Select(t => ((VectorTarget)t).Vector * -0.5))); - - // Kind (not Cruel) - both form dense totalty: λ clearly not high enough... hopefully - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.02, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // higher λ - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.4, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.5, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.6, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // 2x2r6 - // dense - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - // fail - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG2x2", null, null, true, λ: 0.4, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - // fail - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG2x2", null, null, true, λ: 0.6, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // nope - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.6, K: 1000, epochs: 50000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.7, K: 1000, epochs: 50000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.8, K: 1000, epochs: 50000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.9, K: 1000, epochs: 50000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // this is meant to work... - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKind", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), "C:\\M4MExperiments\\BBNKCruelMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), "C:\\M4MExperiments\\BBNKCruelMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), "C:\\M4MExperiments\\BBNKCruelMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // ... but it doesn't ... - // ... but this does: C:\M4MExperiments\BBNKCruel2x2\EpistaticPopTestRatherLongGCbbnkCruel2x2P0.5_2_λ0.2EpiFalsemG0.6mB1E-05E50000K1000T10PF.200G1_20180704T120108 - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1("C:\\M4MExperiments\\BBNKCruel2x2", null, null, false, 0.6); - - // only thing that is different is number of epochs, and the targets (SO CHECK THE TARGETS) - // but these don't work... - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), "C:\\M4MExperiments\\BBNKCruel2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2NonVariable(0.5), "C:\\M4MExperiments\\BBNKCruelMVG2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // why are these not the same?! - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1("C:\\M4MExperiments\\BBNKCruel2x2", null, null, false, 0.6); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), "C:\\M4MExperiments\\BBNKCruel2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // OK... both are non-determinisctic - //for (int i = 0; i < 5; i++) - // Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), "C:\\M4MExperiments\\BBNKCruel2x2", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), "C:\\M4MExperiments\\BBNKCruel2x2", null, null, false, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // I hate my life - //for (int i = 0; i < 5; i++) - // Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1("C:\\M4MExperiments\\BBNKCruel2x2", null, null, true, 0.6); - - // still hate it - //for (int i = 0; i < 5; i++) - // Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), "C:\\M4MExperiments\\BBNKCruel2x2", null, null, false, λ: 0.2, K: 1000, epochs: 100000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - // still hate it still - //for (int i = 0; i < 5; i++) - // Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2NonVariable(0.5), "C:\\M4MExperiments\\BBNKCruel2x2NVG", null, null, false, λ: 0.2, K: 1000, epochs: 100000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - //for (int i = 0; i < 5; i++) - // Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKCruel", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), "C:\\M4MExperiments\\BBNKCruel2x2MVG", null, null, false, λ: 0.2, K: 1000, epochs: 100000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent); - - - // no! - //Epistatics.EpistaticStuff.GeneralExpistaticExperiment("BBNKKindComplements", Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5), "C:\\M4MExperiments\\BBNKKindMVG3x3", null, null, true, λ: 0.2, K: 1000, epochs: 20000, gNoiseTerm: 0.6, regularisationFunction: JudgementRules.L1Equivalent, resetIndividualInitialStateOperation: Epistatics.EpistaticStuff.ResetIndividualInitialStateOperationSampleSimilar(Epistatics.BBNKTargetPackages.CbbnkKind3x3Variable(0.5).Targets.Select(t => ((VectorTarget)t).Vector * -0.5), "Complements")); - - - - // nice runs (all nice) - //PopNotExperiment(customPrefix: "L1 λ=0.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.00, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.00 BEx ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.00, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: true); - //PopNotExperiment(customPrefix: "L1 λ=0.22 BEx ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: true); - - // λ=0.22 BEx long run, varying R_B, and BEx - //PopNotExperiment(customPrefix: "L1 λ=0.22 BEx ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 2000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: true); - //PopNotExperiment(customPrefix: "L1 λ=0.22 R_P=1.0 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 2000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: false); - //PopNotExperiment(customPrefix: "L1 λ=0.22 R_P=0.5 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 2000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0, exclusiveB: false); - - // even longer run - //PopNotExperiment(customPrefix: "L1 λ=0.22 BEx ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: true); - //PopNotExperiment(customPrefix: "L1 λ=0.5 BEx ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.5, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, exclusiveB: true); - //PopNotExperiment(customPrefix: "L1 λ=0.22 P_B=1 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0, exclusiveB: false); - - //var rcs022 = LoadRegCoefs(@"C:\WORK\RAW\raw\Code\Other\M4M_MkI\M4M\bin\x64\Release\PopNotOrg4x4λ0.22Noise0.1E5000K20000CF10_20180706T123748\regcoefs.dat"); - //void eprcs022(int notEpoch) - //{ - // PlotVectorFolded($"Epoch {notEpoch * 5}", $"meh/eprcs{notEpoch * 5}.pdf", Linear.CreateVector.DenseOfEnumerable(rcs022.Select(t => t[notEpoch])), "Trait j", "Trait i", min: -1.8, max: 2.1); - //} - //eprcs022(0); - //eprcs022(2); - //eprcs022(15); - //eprcs022(150); - //eprcs022(600); - //eprcs022(1000); - - //PopNotExperiment(customPrefix: "L1 λ=0.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.00, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - // K /= 10, Epochs *= 10 (not really/very slow) - //PopNotExperiment(customPrefix: "L1 λ=0.11 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.11, K: 2_000, epochs: 15000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 2_000, epochs: 15000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=0.8 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.8, K: 2_000, epochs: 15000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - //PopNotExperiment(customPrefix: "L1 λ=1.0 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 1.0, K: 2_000, epochs: 15000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2); - - //Ev2PopTests(); - - //Epistatics.BbnkStuff.Cbbnk2x2TestRatherLongG1("C:\\M4MExperiments\\BBNKCruel2x2", null, null, false, 0.6); - - - // back to the madness - // OK, we got some hmods for each of these. - //string sf = "B1"; - //ManyCbbnks(sf, Epistatics.BBNKTargetPackages.CbbnkCruel2x2(0.5), 100000); - //ManyCbbnks(sf, Epistatics.BBNKTargetPackages.CbbnkCruel2x2NonVariable(0.5), 100000); - //ManyCbbnks(sf, Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), 100000); - - //string pf = "Bω0.5"; - //ManyCbbnks(pf, "λ0.20", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), 100000, λ: 0.20); - //ManyCbbnks(pf, "λ0.25", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), 100000, λ: 0.25); - //ManyCbbnks(pf, "λ0.30", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), 100000, λ: 0.30); - - //string pf = "Bω0.25"; - //ManyCbbnks(pf, "λ0.20", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.25), λ: 0.20); - //ManyCbbnks(pf, "λ0.25", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.25), λ: 0.25); - //ManyCbbnks(pf, "λ0.30", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.25), λ: 0.30); - - //string pf = "Bω0.4"; - //ManyCbbnks(pf, "λ0.20", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.4), λ: 0.20); - //ManyCbbnks(pf, "λ0.25", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.4), λ: 0.25); - //ManyCbbnks(pf, "λ0.30", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.4), λ: 0.30); - - //ManyCbbnks("λ0.20", "Bω0.5", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.5), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.45", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.45), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.4", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.4), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.35", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.35), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.3", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.3), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.25", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.25), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.2", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(0.2), λ: 0.20, count: 1); - - //ManyCbbnks("λ0.20", "Bω0.5", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.5), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.45", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.45), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.4", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.4), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.35", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.35), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.3", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.3), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.25", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.25), λ: 0.20, count: 1); - //ManyCbbnks("λ0.20", "Bω0.2", Epistatics.BBNKTargetPackages.CbbnkKind2x2Variable(0.2), λ: 0.20, count: 1); - - //var g = LoadGenome(@"C:\M4MExperiments\CbbnkCruel2x2P0.5_2\EpistaticPopTestBBNKCruelCbbnkCruel2x2P0.5_2_λ0.2EpiFalsemG0.6mB1E-05E50000K1000T10PF.200G1_20180718T110943\terminalGenome.dat"); - //var dtm = g.TransMat; - //var dtmInfo = new DtmInfo(dtm, DtmClassification.DiscerneTrueModules(dtm, dtm.Enumerate().Max(e => Math.Abs(e)) / 10.0)); - //Console.WriteLine(dtmInfo.Description); - - - //var g = LoadGenome(@"C:\M4MExperiments\Bω0.4CbbnkCruel2x2P0.4_4MVGλ0.25\EpistaticPopTestBBNKCruelCbbnkCruel2x2P0.4_4MVG_λ0.25EpiFalsemG0.6mB1E-05E25000K1000T10PF.200G1_20180720T171120\terminalgenome.dat"); - //var dtm = g.TransMat; - //var dtmInfo = new DtmInfo(dtm, DtmClassification.DiscerneTrueModules(dtm, dtm.Enumerate().Max(e => Math.Abs(e)) / 10.0)); - //Console.WriteLine(dtmInfo.Description); - - //double ψ = 0.5; - //foreach (var ω in new[] { 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.5 }) - //{ - // ManyCbbnks("Block1_", "Bω" + ω.ToString("0.00"), Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(ω, ψ), λ: 0.20, count: 10); - //} - - //double ψ = 0.5; - //foreach (var ω in new[] { 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.5 }) - //{ - // ManyCbbnks("Block2_", "Bω" + ω.ToString("0.00"), Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(ω, ψ), λ: 0.20, count: 10); - //} - - //double ψ = 0.5; - //double ω = 0.5; - //ManyCbbnks("BiasBlock1_", "Bω" + ω.ToString("0.00"), Epistatics.BBNKTargetPackages.CbbnkCruel2x2VariablePositiveBias(ω, ψ), λ: 0.20, count: 10, disableTransientIo: false); - //ManyCbbnks("BiasBlock1_", "Bω" + ω.ToString("0.00"), Epistatics.BBNKTargetPackages.CbbnkCruel2x2VariableNegativeBias(ω, ψ), λ: 0.20, count: 10, disableTransientIo: false); - - - - // a few variations on a nice run - // template - //PopNotExperiment(customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 150, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.TanhHalf); - // LHalf - not sure... - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "LHalf λ=0.01 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.LHalfEquivalent, λ: 0.01, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.TanhHalf); - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "LHalf λ=0.015 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.LHalfEquivalent, λ: 0.015, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.TanhHalf); - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "LHalf λ=0.02 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.LHalfEquivalent, λ: 0.02, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.TanhHalf); - // MMSO - looks like a slow ride to hierarchy: should do a long run, possibly higher lambda (NOTE: wrong λ were used!!!!!) - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd); - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "L1 λ=0.11 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd); - //PopNotExperiment(outDir: @"C:\M4MExperiments\VBlock1", customPrefix: "L1 λ=0.33 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 5000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd); - - //DocTests(); - - // A new era of being orangised: - //RunMMSOLong(); - //RunMMSOMMSOShort(); - //ClassicCbbnkCuel(); - //RunMMSOR_B1(); - //HeavyFuelRuns(); - //OneKCbbnkCruel(); - //ClassicM4M(); - //ClassicM4MLong(); - //M4MPerfectG(); - //OneKCbbnkCruelBiased(); - //MMSOVaryA(); - //MMSOLinVaryQAB(); // q=5 seems to give good results for a=5 and a=2 - //MMSOLinVaryHighQHighA(); - //M4MPerfectGSingleModule(); - - //Fear(FileStuff.CreateNow(@"C:\M4MExperiments\Fear\Fear", "Fear ", "Fear ")); - - //List<string> classifications = new List<string>(); - //foreach (var dir in System.IO.Directory.EnumerateDirectories(@"C:\M4MExperiments\ClassicCbbnkCuel\CbbnkCruel2x2P0.5_0.5_4MVG")) - //{ - // var g = LoadGenome(dir + "/terminalgenome.dat"); - // classifications.Add(DtmClassification.Type1Classify(g.TransMat)); - //} - - //using (var writer = new System.IO.StreamWriter(@"C:\M4MExperiments\ClassicCbbnkCuel\CbbnkCruel2x2P0.5_0.5_4MVG\classifications.txt")) - //{ - // foreach (var classification in classifications) - // { - // writer.WriteLine(classification); - // } - - // writer.WriteLine(); - - // var groups = classifications.GroupBy(v => v); - // foreach (var grp in groups) - // { - // writer.WriteLine(grp.Key + "\t" + grp.Count()); - // } - //} - - //Console.Beep((int)Pitch("Eb3"), 200); - //Console.Beep((int)Pitch("Bb4"), 200); - //Console.Beep((int)Pitch("F4"), 200); - //Console.Beep((int)Pitch("Bb3"), 200); - //Console.Beep((int)Pitch("F3"), 200); - //Console.Beep((int)Pitch("D3"), 200); - //Console.ReadKey(true); - } - - private static void DocTests() - { - Reporting.Report report = new Reporting.Report("Everyone likes \n WOLVES", "Ergeaux M.", "Today"); - - var s1 = new Reporting.Section("Ehst"); - report.Add(s1); - - var s11 = new Reporting.Section("Seht"); - s1.Add(s11); - s11.Add(new Reporting.RawParagraph("Hello. This is a paragraph")); - - var il = new Reporting.ItemList(); - s11.Add(il); - il.Add(new Reporting.RawInlineText("Hello1")); - il.Add(new Reporting.RawInlineText("Hello2")); - - var fig = new Reporting.Figure(); - s11.Add(fig); - fig.Caption = "Twee wolven!"; - fig.Graphics.Add(new Reporting.FigureGraphic("C:/WORK/RAW/raw/Documents/Templates/Report/figures/aidanAndDenali", "0.8\\linewidth")); - - var s2 = new Reporting.Section("Voll"); - report.Add(s2); - s2.Add(new Reporting.RawParagraph("This is also a paragarph.")); - - report.RenderTex(@"C:\M4MExperiments\meh.tex", true); - report.WriteOut(@"C:\M4MExperiments\meh.txt"); - System.Diagnostics.Process.Start(@"C:\M4MExperiments\meh.txt"); - } - - private static void ClassicM4M() - { - string objectives = "Some classic (low mutation rate) based on configs from 17G03"; - string topdir = @"C:\M4MExperiments\ClassicM4M"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - int epochs = 150; - - double gNoiseTerm = 0.1; //p22 - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0 / 2; //p9/OLD - - Action[] expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "OLD LNo", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.ConstantEquivalent, λ: 0.00, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - () => PopNotExperiment(outDir: topdir, customPrefix: "OLD L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - () => PopNotExperiment(outDir: topdir, customPrefix: "OLD L2 λ=38 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - }; - - Parallel.Invoke(expActs); - - bProb = 1.0 / 15; //p22 - - expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "17G03 LNo", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.ConstantEquivalent, λ: 0.00, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - () => PopNotExperiment(outDir: topdir, customPrefix: "17G03 L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - () => PopNotExperiment(outDir: topdir, customPrefix: "17G03 L2 λ=38 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L2Equivalent, λ: 38, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - }; - - Parallel.Invoke(expActs); - } - - private static void ClassicM4MLong() - { - string objectives = "Some long classic (low mutation rate) based on configs from 17G03"; - string topdir = @"C:\M4MExperiments\ClassicM4MLong"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - int epochs = 10000; - - double gNoiseTerm = 0.1; //p22 - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0 / 2; //p9/OLD - - Action[] expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "OLD L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: epochs, noRandomIO: true, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false), - }; - - Parallel.Invoke(expActs); - } - - private static void M4MPerfectG() - { - string objectives = "Verify that setting G=S, and M_G=0 still leads to hierarchy"; - string topdir = @"C:\M4MExperiments\M4MPerfectG\longruns\"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - int epochs = 50000; - - double gNoiseTerm = 0; - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0 / 2; //p9/OLD - - foreach (double λ in new[] { 0.4 }) - { - Action[] expActs = new Action[] - { - //() => PopNotExperiment(outDir: topdir, customPrefix: $"PT L1 λ={λ} ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveB: false, noRandomIO: true), - //() => PopulationTest(superDir: topdir, customPrefix: $"PT L1 λ={λ} ", dirPrefix: "PT_", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: false, disableTransientIO: false, - // epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true), - () => PopulationTest(superDir: topdir, customPrefix: $"PTPG L1 λ={λ} ", dirPrefix: "PTPG_", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: false, disableTransientIO: false, - epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.MatchTargetVector), - }; - - Parallel.Invoke(expActs); - } - } - - private static void M4MPerfectGSingleModule() - { - string objectives = "Verify that setting G=S, and M_G=0 still leads to hierarchy with a single module"; - string topdir = @"C:\M4MExperiments\M4MPerfectGSingleModule\ohdear\"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 4; - int epochs = 1000; - - double gNoiseTerm = 0.1; //p22 - double bNoiseTerm = 0.1 / (15 * N * N); //p22 - - double bProb = 1.0 / 2; //p9/OLD - - List<Action> expActs = new List<Action>(); - foreach (double λ in new[] { 0.075 }) - { - //expActs.Add(() => PopulationTest(superDir: topdir, customPrefix: $"PTPGSM L1 λ={λ} ", dirPrefix: "PTPGSM_", targetPackage: ModularTargetPackage.Standard4x1, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: true, disableTransientIO: false, - // epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.MatchTargetVector)); - expActs.Add(() => PopulationTest(superDir: topdir, customPrefix: $"PTSM L1 λ={λ} ", dirPrefix: "PTSM_", targetPackage: ModularTargetPackage.Standard4x1, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: false, disableTransientIO: false, - epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 0.0, populationResetOperation: PopulationResetOperations.None)); - expActs.Add(() => PopulationTest(superDir: topdir, customPrefix: $"PTSMBEx L1 λ={λ} ", dirPrefix: "PTSMBEx_", targetPackage: ModularTargetPackage.Standard4x1, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: true, disableTransientIO: false, - epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 0.0, populationResetOperation: PopulationResetOperations.None)); - expActs.Add(() => PopulationTest(superDir: topdir, customPrefix: $"PTZSM L1 λ={λ} ", dirPrefix: "PTZSM_", targetPackage: ModularTargetPackage.Standard4x1, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: false, disableTransientIO: false, - epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.Zero)); - expActs.Add(() => PopulationTest(superDir: topdir, customPrefix: $"PTZSMBEx L1 λ={λ} ", dirPrefix: "PTZSMBEx_", targetPackage: ModularTargetPackage.Standard4x1, regularisationFunction: JudgementRules.L1Equivalent, λ: λ, K: 20_000, epochs: epochs, gNoiseTerm: gNoiseTerm, bNoiseTerm: bNoiseTerm, bProb: bProb, exclusiveBMutation: true, disableTransientIO: false, - epigenetic: false, popSize: 1, eliteCount: 1, hillclimberMode: true, gResetProb: 1.0, populationResetOperation: PopulationResetOperations.Zero)); - - Parallel.Invoke(expActs.ToArray()); - } - } - - private static void HeavyFuelRuns(bool scoping = true) - { - if (scoping) - { - string objectives = "Scoping examples of a population resulting in a strong hierarchy... ideally we would also show that they switch faster over time... that's what trs are for, I suppose"; - string topdir = @"C:\M4MExperiments\HeavyFuelRuns\Scoping4"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - //int N = 16; - - // scope it out... - Action[] expActs1 = new Action[] - { - // this one I C&P'd from the past works... but it's a 3x3... and 3x3 is for chumps - //() => PopulationTest(superDir: topdir, customPrefix: "old", targetPackage: ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 1000, K: 600, columns: false, T: 10, popSize: 20), - //() => PopulationTest(superDir: topdir, customPrefix: "oldLong", targetPackage: ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 5000, K: 600, columns: false, T: 10, popSize: 20), - - () => PopulationTest(superDir: topdir, customPrefix: "new4x4", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0010, epochs: 50000, K: 600, columns: false, T: 10, popSize: 20), - () => PopulationTest(superDir: topdir, customPrefix: "new4x4", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0005, epochs: 50000, K: 600, columns: false, T: 10, popSize: 20), - () => PopulationTest(superDir: topdir, customPrefix: "new4x4", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0002, epochs: 50000, K: 600, columns: false, T: 10, popSize: 20), - () => PopulationTest(superDir: topdir, customPrefix: "new4x4", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0001, epochs: 50000, K: 600, columns: false, T: 10, popSize: 20), - }; - - Parallel.Invoke(expActs1); - - // scope it out... - //Action[] expActs2 = new Action[] - //{ - // () => PopulationTest(superDir: topdir, customPrefix: "SB P5 λ=0.10 K=600", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.10, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.1 / (15 * N * N), epochs: 5000, K: 600, columns: false, T: 10, g0: null, b0: null, popSize: 5), - // () => PopulationTest(superDir: topdir, customPrefix: "SB P5 λ=0.10 K=1000", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.10, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.1 / (15 * N * N), epochs: 5000, K: 1000, columns: false, T: 10, g0: null, b0: null, popSize: 5), - // () => PopulationTest(superDir: topdir, customPrefix: "SB P5 λ=0.18 K=600", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.18, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.1 / (15 * N * N), epochs: 5000, K: 600, columns: false, T: 10, g0: null, b0: null, popSize: 5), - // () => PopulationTest(superDir: topdir, customPrefix: "SB P5 λ=0.18 K=1000", targetPackage: ModularTargetPackage.Standard4x4, λ: 0.18, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.1 / (15 * N * N), epochs: 5000, K: 1000, columns: false, T: 10, g0: null, b0: null, popSize: 5), - //}; - - //Parallel.Invoke(expActs2); - } - else - { - string objectives = "Produce examples of a population resulting in a strong hierarchy... ideally we would also show that they switch faster over time... that's what trs are for, I suppose"; - string topdir = @"C:\M4MExperiments\HeavyFuelRuns\Proper"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - // need to scope out some sensible things to do here before we try running them - } - } - - private static void ClassicCbbnkCuel() - { - string objectives = "Produce detailed samples of (theorectically) unbiased CbbnkCruel runs with traditional parameters, in order to provide a new foundation in advance of further investiation"; - string topdir = @"C:\M4MExperiments\ClassicCbbnkCuel"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ψ = 0.5; - double ω = 0.5; - ManyCbbnks(topdir, "", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(ω, ψ), λ: 0.20, count: 50, disableTransientIo: false); - } - - private static void OneKCbbnkCruel() - { - string objectives = "Produce detailed samples of (theorectically) early dynamics of unbiased CbbnkCruel runs with traditional parameters"; - string topdir = @"C:\M4MExperiments\OneKCbbnkCruel3"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ψ = 0.5; - double ω = 0.5; - ManyCbbnks(topdir, "", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(ω, ψ), λ: 0.20, K: 1000, epochs: 1000, count: 50, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor()); - } - - private static DensePopulationExperimentFeedbackConfigurator IsWatchPopConfigor() - { - return popConfig => - { - ITarget lastTarget = null; - - List<KeyValuePair<long, IndividualJudgement<DenseIndividual>>> bestSamples = new List<KeyValuePair<long, IndividualJudgement<DenseIndividual>>>(); - List<KeyValuePair<long, IndividualJudgement<DenseIndividual>>> medianSamples = new List<KeyValuePair<long, IndividualJudgement<DenseIndividual>>>(); - List<long> targetChanges = new List<long>(); - List<KeyValuePair<long, double>> correls = new List<KeyValuePair<long, double>>(); - List<KeyValuePair<long, double>> correlBiases = new List<KeyValuePair<long, double>>(); - - int samplePeriod = 1000; - - void judged(FileStuff stuff, IReadOnlyList<IndividualJudgement<DenseIndividual>> populationJudgements, int epochCount, long generationCount, ITarget target) - { - lastTarget = target; - - if (generationCount % samplePeriod == 0) - { - //bestSamples.Add(new KeyValuePair(generationCount, populationJudgements.ArgMax(j => j.Judgement.CombinedFitness))); - //medianSamples.Add(new KeyValuePair(generationCount, populationJudgements.ArgMedian(j => j.Judgement.CombinedFitness))); - bestSamples.Add(new KeyValuePair<long, IndividualJudgement<DenseIndividual>>(generationCount, populationJudgements.ArgMax(j => j.Judgement.CombinedFitness))); - medianSamples.Add(new KeyValuePair<long, IndividualJudgement<DenseIndividual>>(generationCount, populationJudgements.ArgMedian(j => j.Judgement.CombinedFitness))); - } - } - - void endTarget(FileStuff stuff, Population<DenseIndividual> population, ITarget target, int epoch, long generationCount, IReadOnlyList<IndividualJudgement<DenseIndividual>> oldPopulationJudgements) - { - targetChanges.Add(generationCount); - - // analuse IS, work out correlation - var phVec = population.PeekAll()[0].Phenotype.Vector; - var ph0 = phVec.First(); - var ph1 = phVec.Last(); - - var correl = Math.Sign(ph0 * ph1); - correls.Add(new KeyValuePair<long, double>(generationCount, correl)); - - int mas = 50; - if (correls.Count > mas) - { - double movingAverage = correls.Skip(correls.Count - mas).Average(s => s.Value); - correlBiases.Add(new KeyValuePair<long, double>(generationCount, movingAverage)); - } - } - - void finished(FileStuff stuff, Population<DenseIndividual> population, int epochCount, long generationCount) - { - PlotModel model = new PlotModel() { Title = stuff.Title("CTr") }; - - model.Axes.Add(new LinearAxis() { Key = "Left", Position = AxisPosition.Left, Title = "Fitness" }); - model.Axes.Add(new LinearAxis() { Key = "Right", Position = AxisPosition.Right, Title = "Correl" }); - model.Axes.Add(new LinearAxis() { Key = "X", Position = AxisPosition.Bottom, Title = "Generations" }); - - void plotLine<TSample>(string title, OxyColor color, List<KeyValuePair<long, TSample>> samples, Func<TSample, double> metric, string yaxis) - { - LineSeries ls = new LineSeries() { Title = title, Color = color, YAxisKey = yaxis }; - - foreach (var sample in samples) - { - ls.Points.Add(new DataPoint((double)sample.Key, metric(sample.Value))); - } - - model.Series.Add(ls); - } - - //plotLine("Median Fitness (Combined)", OxyColors.Purple, medianSamples, s => s.Judgement.CombinedFitness, "Left"); - //plotLine("Median Fitness (Benefit)", OxyColors.Blue, medianSamples, s => s.Judgement.Benefit, "Left"); - plotLine("Median Fitness (Cost)", OxyColors.Red, medianSamples, s => s.Judgement.Cost, "Left"); - - plotLine("Phenotype Correls (mas50)", OxyColors.Orange, correlBiases, s => s, "Right"); - - //foreach (int ts in targetChanges) - // model.Annotations.Add(new OxyPlot.Annotations.LineAnnotation() { Type = OxyPlot.Annotations.LineAnnotationType.Vertical, X = (double)ts, Color = OxyColors.Gray, LineStyle = LineStyle.Dash }); - - Plotting.OldOxyPlotting.ExportToPdf(model, stuff.File("CTr.pdf")); - } - - popConfig.Feedback.Judged.Register(judged); - popConfig.Feedback.EndTarget.Register(endTarget); - popConfig.Feedback.Finished.Register(finished); - }; - } - - private static void OneKCbbnkCruelBiased() - { - string objectives = "Produce detailed samples of early dynamics of biased CbbnkCruel runs with traditional parameters and various population configurations"; - string topdir = @"C:\M4MExperiments\OneKCbbnkCruelBiased"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - //double ψ = 0.5; - //double ω = 0.5; - //ManyCbbnks(topdir, "HF", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.20, K: 1000, epochs: 2000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - // popSize: 5, eliteCount: 1, hillclimberMode: false); - //ManyCbbnks(topdir, "HT", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.20, K: 1000, epochs: 2000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - // popSize: 5, eliteCount: 1, hillclimberMode: true); - //ManyCbbnks(topdir, "HF21", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.20, K: 1000, epochs: 2000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - // popSize: 2, eliteCount: 1, hillclimberMode: false); - //ManyCbbnks(topdir, "HT11", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.25, K: 1000, epochs: 2000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - // popSize: 1, eliteCount: 1, hillclimberMode: true); - //ManyCbbnks(topdir, "HT11MMSO", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.2, K: 1000, epochs: 2000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - // popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.MMSO1); - // MMSO example is interesting... should do some more long runs of it with varying A - } - - private static void MMSOVaryA() - { - string objectives = "Produce long-run samples of MMSO with varying a=b (keeping gradient at x=0 constant of 1)"; - string topdir = @"C:\M4MExperiments\MMSOVaryAB"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ψ = 0.5; - double ω = 0.5; - - // gradient a 0 kept constant at 1 - foreach (double a in new[] { 0.1, 0.2, 0.5, 1.0, 2.0, 5.0 }) - ManyCbbnks(topdir, "VAeqB", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.2, K: 1000, epochs: 20000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.MMSO(a, a)); - } - - private static void MMSOLinVaryQAB() - { - string objectives = "Produce long-run samples of MMSO+L1 with varying a=qb (keeping gradient at x=0 constant of 1)"; - string topdir = @"C:\M4MExperiments\MMSOLinVaryQAB"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ψ = 0.5; - double ω = 0.5; - - List<Action> expActs = new List<Action>(); - - // gradient a 0 kept constant at 1 - foreach (double q in new[] { 0.1, 0.2, 0.3, 0.4, 0.5 }) // proportion linear - { - foreach (double a in new[] { 0.1, 0.2, 0.5, 1.0, 2.0, 5.0 }) // shape of MMSO - { - expActs.Add(() => - { - try - { - ManyCbbnks(topdir, $"q{q}a{a}", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.2, K: 1000, epochs: 50000, count: 5, disableTransientIo: true, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.MMSOLin(a, a * (1 - q), q)); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - } - ); - } - } - - Parallel.Invoke(expActs.ToArray()); - } - - private static void MMSOLinVaryHighQHighA() - { - string objectives = "Produce even longer runs of MMSO+L1 with high q and high a"; - string topdir = @"C:\M4MExperiments\MMSOLinVaryHighQAB\uncruelshort7"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ω = 1.0; // Slow cut-off // 1.0 -> no cutoff - double ψ = 0.5; // Slow gradient - - List<Action> expActs = new List<Action>(); - - // gradient a 0 kept constant at 1 - foreach (double q in new[] { 0.5 }) // proportion linear (1.0 is no MMSO: the control) - { - foreach (double a in new[] { 2.0, 3.0, 4.0, 5.0 }) // shape of MMSO - { - expActs.Add(() => - { - try - { - ManyCbbnks(topdir, $"q{q}a{a}", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2(ω, ψ), λ: 0.2, K: 1000, epochs: 10000, count: 20, disableTransientIo: false, densePopulationExperimentFeedbackConfigurator: IsWatchPopConfigor(), - popSize: 1, eliteCount: 1, hillclimberMode: true, regularisationFunction: JudgementRules.MMSOLin(a, a * (1 - q), q)); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - } - ); - } - } - - Parallel.Invoke(expActs.ToArray()); - } - - private static void CbbnkNot() - { - // this is pointless... it just forms a 1/4 immediately because there is no correlation - string objectives = "See what happens when we disable the asymetry in the payoff function: is there is no asynmetry, there can be no bias... right?"; - string topdir = @"C:\M4MExperiments\CbbnkNot"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - double ψ = 1.0; - double ω = 1.0; - ManyCbbnks(topdir, "", "", Epistatics.BBNKTargetPackages.CbbnkCruel2x2Variable(ω, ψ), λ: 0.20, count: 50, disableTransientIo: false); - } - - private static void RunMMSOMMSOShort() - { - string objectives = "Verify whether MMSO if a viable regularisation function (it doesn't have the undeseriable feature of having an infinite gradient at 0, which LHalf has; but it is asymptotic like tanh)"; - string topdir = @"C:\M4MExperiments\MMSO_RegShort"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - Action[] expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.MMSO1, λ: 0.22, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd, disableTransientIO: true), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.33 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.MMSO1, λ: 0.33, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd, disableTransientIO: true), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.44 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.MMSO1, λ: 0.44, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd, disableTransientIO: true), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.55 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.MMSO1, λ: 0.55, K: 20_000, epochs: 1000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd, disableTransientIO: true), - }; - - Parallel.Invoke(expActs); - } - private static void RunMMSOR_B1() - { - string objectives = "Determine whether R_B=1 changes the shape of the trajectories"; - string topdir = @"C:\M4MExperiments\MMSOR_B1"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - Action[] expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "MMSO L1 λ=1.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 1.00, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0, squash: DevelopmentRules.MichaelisMentenSymOdd), - () => PopNotExperiment(outDir: topdir, customPrefix: "MMSO L1 λ=2.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 2.00, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0, squash: DevelopmentRules.MichaelisMentenSymOdd), - }; - - Parallel.Invoke(expActs); - } - - private static void RunMMSOLong() - { - string objectives = "Determine whether MichaelisMenthon (SymOdd) can produce hierarchy"; - string topdir = @"C:\M4MExperiments\MMSO"; - EnsureDirectory(topdir); - System.IO.File.WriteAllText(topdir + @"\objectives.txt", objectives); - - int N = 16; - Action[] expActs = new Action[] - { - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.22 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.22, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.33 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.33, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.44 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.44, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=0.55 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 0.55, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd), - () => PopNotExperiment(outDir: topdir, customPrefix: "L1 λ=2.00 ", targetPackage: ModularTargetPackage.Original4x4, regularisationFunction: JudgementRules.L1Equivalent, λ: 2.00, K: 20_000, epochs: 25000, noRandomIO: true, bNoiseTerm: 0.1 / (15 * N * N), bProb: 1.0 / 2, squash: DevelopmentRules.MichaelisMentenSymOdd), - }; - - Parallel.Invoke(expActs); - - // ja, wir haben hierarchy - } - - public static void SayInts(int[] ints) - { - string intToKey(int i) - { - i--; - string[] table = new[] { "C", "E", "G", "B" }; - return table[i % 4] + ((i + 1) / 4 + 3); - } - - string[] keys = ints.Select(i => intToKey(i)).ToArray(); - int[] pitches = keys.Select(k => (int)Pitch(k)).ToArray(); - - foreach (var p in pitches) - { - Console.Beep(p, 200); - } - } - - private static void ManyCbbnks(string topdir, string prefix, string suffix, Epistatics.EpistaticTargetPackage target, int epochs = 100000, int K = 1000, double λ = 0.2, double gNoiseTerm = 0.6, int count = 10, bool disableTransientIo = true, DensePopulationExperimentFeedbackConfigurator densePopulationExperimentFeedbackConfigurator = null, int popSize = 1, int eliteCount = 1, bool hillclimberMode = true, IRegularisationFunction<IGenome> regularisationFunction = null, bool plotRegFunc = true) - { - var nowTime = Misc.NowTime; - - regularisationFunction = regularisationFunction ?? JudgementRules.L1Equivalent; - - if (plotRegFunc) - { - int N = target.TargetSize; - - string regfname = topdir + "\\reg" + regularisationFunction.Name + suffix + nowTime + ".pdf"; - double regx0 = -5.0; - double regx1 = +5.0; - double regdx = 0.05; - - List<double> regdat = new List<double>(); - - var regGenome = DenseGenome.CreateDefaultGenomeDense(N); - Linear.Matrix<double> regB = Linear.CreateMatrix.Dense<double>(N, N); - - for (double x = regx0; x <= regx1; x += regdx) - { - for (int i = 0; i < N; i++) - for (int j = 0; j < N; j++) - regB[i, j] = x; - - regGenome.CopyOverTransMat(regB); - regdat.Add(regularisationFunction.ComputeCost(regGenome)); - } - - PlotLine("Reg " + regularisationFunction.Name, regfname, regdat.ToArray(), regx0, regdx, "Element Connection Weight (total/N^{2})", "Total Cost"); - } - - void saySummary(DtmInfo dtmInfo) - { - string summary = dtmInfo.Summary; - int[] ints = summary.Split(new[] { '/', ',', '_', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray(); - - SayInts(ints); - } - - List<DtmInfo> dtmInfos = new List<DtmInfo>(); - - topdir = topdir + "\\" + prefix + target.Name + suffix; - - for (int i = 0; i < count; i++) - { - Console.Title = i + " " + topdir; - - var res1 = Epistatics.EpistaticStuff.GeneralEpistaticExperiment("BBNKCruel", target, topdir, null, null, disableTransientIo, λ: λ, K: K, epochs: epochs, gNoiseTerm: gNoiseTerm, regularisationFunction: regularisationFunction, densePopulationExperimentFeedbackConfigurator: densePopulationExperimentFeedbackConfigurator, popSize: popSize, eliteCount: eliteCount, hillclimberMode: hillclimberMode); - var dtm = res1.ExtractAll()[0].Genome.TransMat; - - var dtmInfo = new DtmInfo(dtm, DtmClassification.DiscerneTrueModules(dtm, dtm.Enumerate().Max(e => Math.Abs(e)) / 10.0)); - dtmInfos.Add(dtmInfo); - - Console.WriteLine(dtmInfo.Description); - - try - { - saySummary(dtmInfo); - } - catch - { - Console.Beep(440, 500); - } - } - - StringBuilder sb = new StringBuilder(); - - foreach (var dtmInfo in dtmInfos) - sb.AppendLine(dtmInfo.Summary); - - var dtmSummaries = sb.ToString(); - System.IO.File.WriteAllText(topdir + "\\dtmSummaries" + suffix + nowTime + ".txt", dtmSummaries); - Console.WriteLine(dtmSummaries); - - Console.Title = "- " + topdir; - Console.WriteLine("Finished ManyCbbnk."); - } - - private static void SupportingFigures() - { - var o4x4 = ModularTargetPackage.Original4x4; - FileStuff stuff = FileStuff.CreateNow(o4x4.Name, "", ""); - int ti = 1; - foreach (var t in o4x4.Targets) - PlotVectorFolded($"Target {ti++}", stuff.File(t.FriendlyName), t.Vector, "Sub-trait j", "Module i", 0.6); - } - - private static void WeirdStuff() - { - // And9 - //PopulationTest(ModularTargetPackage.And9, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 10, popSize: 5, tracePeriod: 2500, decayRate: 0.2); // nomral: bleh - //PopulationTest(ModularTargetPackage.And9, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 10, popSize: 5, tracePeriod: 2500, decayRate: 0.2, gOpenEntries: new[] { 0, 1, 2, 3, 4, 5 }); // constrain G: learns AND! :D - //PopulationTest(ModularTargetPackage.And9, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 10000, K: 600, columns: false, T: 10, popSize: 5, tracePeriod: 500, decayRate: 0.2, gOpenEntries: new[] { 0, 1, 2, 3, 4, 5 }); // run it for longer: eh, much the same - //PopulationTest(ModularTargetPackage.And9, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0001, epochs: 10000, K: 500, columns: false, T: 10, popSize: 10, tracePeriod: 500, decayRate: 0.2, gOpenEntries: new[] { 0, 1, 2, 3, 4, 5 }); // larger population, low BM: miserable failure... no hierarchy, lumped c and b together, how disappointing (is this beacause it takes too long to evolve, or something nefarious?) - //PopulationTest(ModularTargetPackage.And9, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 10000, K: 600, columns: false, T: 10, popSize: 10, tracePeriod: 500, decayRate: 0.2, gOpenEntries: new[] { 0, 1, 2, 3, 4, 5 }); // same larger population, default BM: eh, much the same, reaches something sensible sooner, doesn't seem to improve much - - // Bool12 - PopulationTest(ModularTargetPackage.Bool12, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 10000, K: 600, columns: false, T: 10, popSize: 10, tracePeriod: 500, decayRate: 0.2, gOpenEntries: new[] { 0, 1, 2, 3, 4, 5 }); // - } - - private static void MorePopulationStuff() - { - // high T - we arn't allowed high T without columns!!! what is wrong with you?! - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 1000, K: 600, columns: false, T: 10, popSize: 20, tracePeriod: 500); - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 10, K: 600, columns: false, T: 10, popSize: 20, tracePeriod: 5); - - // max out decay term instead - nope... initial states disconnected from phenotypic states - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 1000, K: 600, columns: false, T: 1, popSize: 20, tracePeriod: 100, decayRate: 0.9); - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 1000, K: 600, columns: false, T: 1, popSize: 20, tracePeriod: 100, decayRate: 0.99); - - //Task[] tasks = { - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 10, popSize: 50, tracePeriod: 100, decayRate: 0.2), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.6), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 10, popSize: 50, tracePeriod: 100, decayRate: 0.6), TaskCreationOptions.LongRunning) - //}; - - //Task[] tasks = { - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, squash: DevelopmentRules.TanhTen), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: true, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.6, squash: DevelopmentRules.TanhTen), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, squash: DevelopmentRules.TanhTen), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.6, squash: DevelopmentRules.TanhTen), TaskCreationOptions.LongRunning), - //}; - - //foreach (var t in tasks) - // t.Start(); - //Task.WaitAll(tasks); - - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0001, epochs: 10000, K: 600, columns: false, T: 1, popSize: 250, tracePeriod: 100, decayRate: 0.2, squash: DevelopmentRules.TanhHalf); // fail (atlast, not in timeframe) - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, bNoiseTerm: 0.0001, epochs: 10000, K: 400, columns: false, T: 1, popSize: 250, tracePeriod: 100, decayRate: 0.2, squash: DevelopmentRules.TanhHalf); // fail (atleast, not in timeframe) - - // Release/Ereht - //Task[] tasks = { - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, superDir: "Ehreht", gUpdateCount: 1), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, superDir: "Ehreht", gUpdateCount: 2), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, superDir: "Ehreht", gUpdateCount: 4), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, superDir: "Ehreht", gUpdateCount: 8), TaskCreationOptions.LongRunning), - //}; - - // Debug/Tacky - //Task[] tasks = { - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.1, superDir: "Tacky", gUpdateCount: 1), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.2, superDir: "Tacky", gUpdateCount: 1), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.4, superDir: "Tacky", gUpdateCount: 1), TaskCreationOptions.LongRunning), - // new Task(() => PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 2500, K: 600, columns: false, T: 1, popSize: 50, tracePeriod: 100, decayRate: 0.8, superDir: "Tacky", gUpdateCount: 1), TaskCreationOptions.LongRunning), - //}; - - //foreach (var t in tasks) - // t.Start(); - //Task.WaitAll(tasks); - } - - private static double Pitch(string name) - { - Dictionary<string, int> keys = new Dictionary<string, int> - { - { "A", 0 }, - { "A#", 1 }, - { "Bb", 1 }, - { "B", 2 }, - { "C", 3 }, - { "C#", 4 }, - { "Db", 4 }, - { "D", 5 }, - { "D#", 6 }, - { "Eb", 6 }, - { "E", 7 }, - { "F", 8 }, - { "F#", 9 }, - { "Gb", 9 }, - { "G", 10 }, - { "G#", 11 }, - { "Ab", 11 }, - }; - - bool adjusted = name.Contains("#") || name.Contains("b"); - string keyName = name.Substring(0, adjusted ? 2 : 1); - int octave = int.Parse(name.Substring(keyName.Length)); - - double a4 = 440; - - int diff = (octave - 4) * 12 + keys[keyName]; - - double l12 = Math.Exp(Math.Log(2) / 12); - return a4 * Math.Pow(l12, diff); - } - - - private static void GradientMeasuring()// int T, int k, double s, double r, double q, double ys, double yr, double λ, double innerSquash) - { - // need to measure Δf/Δs and Δf/Δq for varyious s, q, ys, and yr, for varying T; eh, don't forget r, I suppose - - var model = new PlotModel { Title = "Gradients" }; - //model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Δs/Δq" }); - model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "τ2" }); - - model.PlotAreaBorderColor = OxyColors.Transparent; - - int k = 3; - //MultiMeasureJudgement J(int T, double s, double q, double yDiff, double λ) - //{ - // return Judge(T, k, s, s, q, -1 + yDiff, -1, λ, 0.5, 1.0); - //} - - double ΔsΔq(int T, double s, double q, double y0, double yDiff, double λ, double τ2) - { - var dc = 0.00000001; - var z = Judge(T, k, s, s, q, y0 + yDiff, y0, λ, 0.5, 1.0, τ2).CombinedFitness; - var Δs = Judge(T, k, s + dc, s + dc, q, y0 + yDiff, y0, λ, 0.5, 1.0, τ2).CombinedFitness; - var Δq = Judge(T, k, s, s, q + dc / k, y0 + yDiff, y0, λ, 0.5, 1.0, τ2).CombinedFitness; - return (Δs - z) / (Δq - z); - } - - double[] y0s = { -1.0, -0.5, 0.0, 0.5, 1.0 }; - int yk = 0; - foreach (double y0 in y0s) - { - var yaxe = new LinearAxis { Position = AxisPosition.Left, Title = "Δs/Δq (" + y0 + ")", Key = "yk" + yk, StartPosition = 0.01 + (double)yk++ / y0s.Length, EndPosition = -0.01 + (double)yk / y0s.Length, AxislineColor = OxyColors.Black }; - model.Axes.Add(yaxe); - for (int T = 2; T <= 10; T++) - { - var c1 = OxyColor.FromHsv((T - 1) / 11.0, 0.9, 0.9); - var s0 = 3; - model.Series.Add(new FunctionSeries(x => ΔsΔq(T, s0, s0, y0, 0.1, 0.1, x), 0, 1, 1000) { Title = "f" + T, Color = c1, YAxisKey = yaxe.Key, RenderInLegend = yk == 1 }); - foreach (double ds in new[] { 0.5, 1.0, 1.5, 2.0, 2.5 }) - model.Series.Add(new FunctionSeries(x => ΔsΔq(T, s0 + ds, s0 - ds / k, y0, 0.1, 0.1, x), 0, 1, 1000) { Color = c1, LineStyle = LineStyle.Dash, YAxisKey = yaxe.Key }); - } - - FudgeYAxis(model, yaxe); - } - - //model.RenderingDecorator = rc => new XkcdRenderingDecorator(rc); - - string fname = "bleh.pdf"; - Plotting.OldOxyPlotting.ExportToPdf(model, fname); - System.Diagnostics.Process.Start(fname); - } - - static void FudgeYAxis(PlotModel model, LinearAxis yaxis) - { - model.InvalidatePlot(true); - - var min = model.Series.OfType<FunctionSeries>().Where(s => s.YAxisKey == yaxis.Key).Min(s => s.Points.Min(p => p.Y)); - var max = model.Series.OfType<FunctionSeries>().Where(s => s.YAxisKey == yaxis.Key).Min(s => s.Points.Max(p => p.Y)); - - //double dd = (max - min); - //yaxis.Minimum = min - dd * 0.1; - //yaxis.Maximum = max + dd * 0.1; - //yaxis.MajorStep = dd / 4.0; - - Plotting.OxyPlotModelShim.Sillyify(5, yaxis, min, max); - } - - private static MultiMeasureJudgement Judge(int T, int k, double s, double r, double q, double ys, double yr, double λ, double innerSquash, double e, double τ2) - { - var g = OneModule.PrepareG(k, ys, yr); - var b = OneModule.PrepareB(k, s, r, q); - DenseGenome genome = new DenseGenome(g, b); - DevelopmentRules drules = new DevelopmentRules(T, 1.0, τ2, new TanhSquash(innerSquash)); - var phenotype = genome.Develop(null, drules); // hopefully it will crash if it tries any non-determinism - JudgementRules jrules = new JudgementRules(λ, JudgementRules.L1Equivalent, 0.0); - VectorTarget target = OneModule.PrepareE(k, e, e); - target.NextGeneration(null, jrules); - return MultiMeasureJudgement.Judge(genome, phenotype, jrules, target); - } - - private static void Ev2Tests() - { - Type2EvolvabilityTest4(1); - Type2EvolvabilityTest4(2); - Type2EvolvabilityTest4(4); - Type2EvolvabilityTest4(8); - Type2EvolvabilityTest4(16); - } - - private static void Ev2PopTests() - { - //Type2PopulationEvolvabilityTest4(1, 1); - //Type2PopulationEvolvabilityTest4(1, 2); - Type2PopulationEvolvabilityTest4(1, 4); // this one is beautiful enough - //Type2PopulationEvolvabilityTest4(1, 8); - //Type2PopulationEvolvabilityTest4(1, 16); - } - - private class DistanceFitnessJudger : IVectorTargetJudger - { - public DistanceFitnessJudger(double magnitude) - { - Magnitude = magnitude; - Name = "DistanceFitnessJudger" + magnitude; - } - - public string Name { get; } - - public string Description => $"DistanceFitnessJudger (Magnitude={Magnitude})"; - - public double Magnitude { get; } - - public double Judge(Linear.Vector<double> vector, Phenotype phenotype) - { - return vector.Zip(phenotype.Vector, (vi, pi) => vi * Magnitude - Math.Abs(vi * Magnitude - pi)).Sum(); - } - } - - public static void DistanceFitnessExperiments() - { - for (double mag = 0.86; mag < 0.9; mag += 0.01) - { - DistanceFitnessExperiment(mag); - } - } - - public static void DistanceFitnessExperiment(double mag = 0.85) - { - VectorTarget[] mtargets = new[] - { - new VectorTarget("++++", "3_3", new DistanceFitnessJudger(mag)), - }; - - // config - int N = mtargets[0].Size; - int epochs = 30000; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.2, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "DF1Module" + mag; - Experiment.RunExperiment(rand, config, "DistanceFitness_" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false); - } - - private static void ToplogyExperiments() - { - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - string outDir = "Topevol"; - - TopologicalExperiment(rand, outDir, "Flat", TopologicalGenome.Empty(4, GeneralTopology.Flat)); - TopologicalExperiment(rand, outDir, "Identity", TopologicalGenome.Empty(4, GeneralTopology.Identity)); - TopologicalExperiment(rand, outDir, "Column", TopologicalGenome.Empty(4, GeneralTopology.Column)); - } - - private static void TopologicalExperiment(RandomSource rand, string outDir, string prefix, TopologicalGenome genome) - { - TopologicalGenome.RunExperiment(outDir + "_" + prefix, prefix, prefix + ": ", rand, genome, 1000, 50, 10, DevelopmentRules.TanhHalf.Delegate, 0.001, 0.2); - } - - private static void HorrifyingStuff() - { - // first, check EnumerateParameterisations is working - //CheckEnumerateParameterisationsIsWorking(); - // it is - - int T = 10; - double dd = 0.05; - //TopologyStuff.SolveTopology("FlatTop", GeneralTopology.Flat, 0, 10, dd, T, DevelopmentRules.TanhHalf.Value); - //TopologyStuff.SolveTopology("IdentityTop", GeneralTopology.Identity, 0, 10, dd, T, DevelopmentRules.TanhHalf.Value); - TopologyStuff.SolveTopology("ColumnTop", GeneralTopology.Column, 0, 10, dd, T, DevelopmentRules.TanhHalf.Delegate, 200); - } - - private static void CheckEnumerateParameterisationsIsWorking() - { - // first, check EnumerateParameterisations is working - - var plotModel = Plotting.OldOxyPlotting.LinearAxes("EnumerateParameterisations", "Meh", "Weights"); - - int n = 4; - - OxyPlot.Series.LineSeries[] ls = new OxyPlot.Series.LineSeries[n]; - for (int i = 0; i < n; i++) - ls[i] = new OxyPlot.Series.LineSeries() { Title = "" + i }; - - double x = 0; - foreach (var p in Combinatorics.EnumerateOrderedParameterisations(n, 1.0, 10)) - { - for (int i = 0; i < n; i++) - ls[i].Points.Add(new DataPoint(x, p[i])); - - x++; - } - - for (int i = 0; i < n; i++) - plotModel.Series.Add(ls[i]); - plotModel.InvalidatePlot(true); - - Plotting.OldOxyPlotting.ExportToPdf(plotModel, "EnumerateParameterisations.pdf"); - } - - public static void ColumnExperiment() - { - // sooooo much duplication... - VectorTarget[] targets = new[] - { - new VectorTarget("++++", "plus"), // standard - }; - - // config - int N = targets[0].Size; - int epochs = 1000; - int K = 50; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.2, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // TransMat-Constrained Empty Genome - Linear.Vector<double> emptyIS = Linear.CreateVector.Dense<double>(N, 0.0); - Linear.Matrix<double> emptyGRN = Linear.CreateMatrix.SparseDiagonal<double>(N, 0.0); - IReadOnlyList<MatrixEntryAddress> column0 = Enumerable.Range(0, N).Select(idx => new MatrixEntryAddress(idx, 0)).ToArray(); - DenseGenome emptyIdentity = new DenseGenome(emptyIS, emptyGRN, null, column0); - - // go - string catchyName = "StdCol"; - Experiment.RunExperiment(rand, config, "Column_" + catchyName, startingGenome: emptyIdentity, titlePrefix: catchyName + " ", openTerminalsPlots: false); - } - - public static void IdentityExperiment() - { - VectorTarget[] targets = new[] - { - new VectorTarget("++++", "plus"), // standard - }; - - // config - int N = targets[0].Size; - int epochs = 1000; - int K = 50; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.2, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // TransMat-Constrained Empty Genome - Linear.Vector<double> emptyIS = Linear.CreateVector.Dense<double>(N, 0.0); - Linear.Matrix<double> emptyGRN = Linear.CreateMatrix.SparseDiagonal<double>(N, 0.0); - IReadOnlyList<MatrixEntryAddress> diagonals = Enumerable.Range(0, N).Select(idx => new MatrixEntryAddress(idx, idx)).ToArray(); - DenseGenome emptyIdentity = new DenseGenome(emptyIS, emptyGRN, null, diagonals); - - // go - string catchyName = "StdId"; - Experiment.RunExperiment(rand, config, "Identity_" + catchyName, startingGenome: emptyIdentity, titlePrefix: catchyName + " ", openTerminalsPlots: false); - } - - public static void LeaderTest() - { - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(1.0, JudgementRules.L1Equivalent, 0.0); - - double l = 0.3; - double s = 0.9; - - int N = 4; - double[,] dtm = new double[4, 4] { - { 0, 0, 0, 0}, - { l, l, l, l}, - { 0, 0, s, 0}, - { 0, 0, 0, 0} - }; - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - ModelExecutionContext context = new ModelExecutionContext(rand); - - VectorTarget target = new VectorTarget("++++", "t"); - DenseGenome genome = new DenseGenome(target.Vector, MathNet.Numerics.LinearAlgebra.CreateMatrix.DenseOfArray(dtm)); - Phenotype phenotype = genome.Develop(context, drules); - target.NextGeneration(rand, jrules); - MultiMeasureJudgement judgement = MultiMeasureJudgement.Judge(genome, phenotype, jrules, target); - - string title = $"l{l}s{s}λ{jrules.RegularisationFactor}"; - string topDir = title + "_" + Misc.NowTime; - - if (!System.IO.Directory.Exists(topDir)) - { - System.IO.Directory.CreateDirectory(topDir); - } - - System.IO.File.WriteAllText(topDir + "/config.txt", $"l = {l}, s = {s}, λ = {jrules.RegularisationFactor}"); - - Console.WriteLine(topDir); - - double[][] trajectories = null; - int step = 100000; - int maxGenerations = 10000000; - double[][] regulationCoefs = Misc.CreateEmpty<double>(N * N, maxGenerations / step + 1); - for (int generation = 0; generation < maxGenerations;) - { - if (generation != 0) - { - Experiment.SpinMk2(ref genome, ref phenotype, ref judgement, context, target, rrules, drules, jrules, step); - generation += step - 1; - } - - Console.WriteLine(generation); - MiscPlotting.PlotMatrix(title + ": DTM " + generation, topDir + "/dtm" + generation + ".pdf", genome.TransMat, "Feedee", "Feeder", true); - genome.DevelopWithTrajectories(rand, drules, ref trajectories); - string[] seriesLabels = Enumerable.Range(0, N).Select(n => "Trait " + n).ToArray(); - MiscPlotting.PlotLines(title + ": Traj " + generation, topDir + "/traj" + generation + ".pdf", trajectories, 0, 1, "Developmental Time Step", "Trait Weight", seriesLabels); - - int o = 0; - for (int i = 0; i < N; i++) - { - for (int j = 0; j < N; j++) - { - regulationCoefs[o][generation / step] = genome.TransMat[i, j]; - o++; - } - } - - generation++; - } - - MiscPlotting.PlotLines(title + "; Regulation Coefs", topDir + "/rcs.pdf", regulationCoefs, 0, step, "Generation", "Reg Coefs"); - } - - public static void EvolvabilityTest() - { - for (int gv = /*1*/5; gv <= 10; gv++) - EvolvabilityTest4(gv); - } - - public static void EvolvabilityTest4(int gvecUpdates) - { - VectorTarget oldTarget = new VectorTarget("+---", "o"); - VectorTarget newTarget = new VectorTarget("-+++", "n"); - DenseGenome fox = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.25, 0.25, 0.25)); - DenseGenome husky4 = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.0, 1.0, 1.0)); - - EvolvabilityTest("evtest4_" + "g" + gvecUpdates, 500, 5, 20, oldTarget, newTarget, fox, husky4, gvecUpdates); - } - - public static void EvolvabilityTest162mod() - { - VectorTarget oldTarget = new VectorTarget("+---+---+-------", "o"); - VectorTarget newTarget = new VectorTarget("-+++-++++-------", "n"); - DenseGenome fox = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("AaaaBbbbCcccdddd", 0.25, 0.25, 0.25)); - DenseGenome husky4 = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("AaaaBbbbCcccdddd", 0.0, 1.0, 1.0)); - - EvolvabilityTest("evtest162mod", 2000, 10, 200, oldTarget, newTarget, fox, husky4); - } - - public static void Type2EvolvabilityTest4(int gvecUpdates) - { - VectorTarget oldTarget = new VectorTarget("+---", "o"); - VectorTarget newTarget = new VectorTarget("-+++", "n"); - DenseGenome fox = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.25, 0.25, 0.25)); - DenseGenome husky4 = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.0, 1.0, 1.0)); - - EvolvabilityTest("evtest4type3_" + "g" + gvecUpdates, 500, 5, 10, oldTarget, newTarget, fox, husky4, gvecUpdates, 0.1 / Math.Sqrt(gvecUpdates), true); - } - - public static void EvolvabilityTest(string prefix, int generations, int samplePeriod, int mutantSamplePeriod, VectorTarget oldTarget, VectorTarget newTarget, DenseGenome fox, DenseGenome husky4, int gvecUpdates = 1, double gMutationMagnitude = 0.1, bool noMA = false) - { - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - //ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - ReproductionRules rrules = new ReproductionRules(gMutationMagnitude, 0, 0, false, gvecUpdates); // disable B mutation - JudgementRules jrules = new JudgementRules(0.0, JudgementRules.ConstantEquivalent, 0.0); - - Evolvability.EnvironmentTransitionConfiguration config = new Evolvability.EnvironmentTransitionConfiguration(generations, samplePeriod, drules, rrules, jrules); - - int repeats = 1000; - - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - ModelExecutionContext context = new ModelExecutionContext(rand); - - StringBuilder maTable = new StringBuilder(); - - int unsulliedMutantAnalysisPeriod = mutantSamplePeriod; - int mutantAnalysisPeriod = (int)Math.Ceiling((double)unsulliedMutantAnalysisPeriod / config.SamplePeriod) * config.SamplePeriod; - void subSampler(int generation, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement, string name, string outDir, Func<string, string> file) - { - if (!noMA && generation % mutantAnalysisPeriod == 0) - { - Console.WriteLine("(MutantAnalysis" + generation + ")"); - MutantAnalysis ma = new MutantAnalysis(genome, 100000, context, rrules, drules, jrules, newTarget); - ma.Plot(outDir, name + "MA" + generation, name + " MA" + generation + ": "); - MiscPlotting.PlotVectorFolded(name + " IS" + generation + ": ", file("IS" + generation), genome.InitialState); - - int pCount = ma.FitnessChanges.Count(df => df > 0); - double pAvg = pCount > 0 ? ma.FitnessChanges.Where(df => df > 0).Average() * pCount / (double)ma.FitnessChanges.Length : 0.0; - maTable.AppendLine(generation + "\t" + pAvg + "\t" + pCount); - } - } - - string topDir = prefix + "_" + Misc.NowTime; - - Evolvability.SimpleSampler fs = new Evolvability.SimpleSampler(topDir, "Dense", subSampler); - Evolvability.SimpleSampler hs = new Evolvability.SimpleSampler(topDir, "Column", subSampler); - - maTable.AppendLine("Dense"); - var fr = Evolvability.RunManyEnvironmentTransitionExperiments(context, repeats, fox, newTarget, fs, config, true); - maTable.AppendLine("Column"); - var hr = Evolvability.RunManyEnvironmentTransitionExperiments(context, repeats, husky4, newTarget, hs, config, true); - - Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string> ft = new Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string>(fr, OxyColors.Blue, "Dense"); - Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string> ht = new Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string>(hr, OxyColors.Red, "Column"); - - Evolvability.PlotAreas(new[] { ht, ft }, topDir + "\\ehst.pdf", prefix + " Fitness Distribution (Ehst)", 1); - Evolvability.PlotAreas(new[] { ht, ft }, topDir + "\\dfjs.pdf", $"Distribution of Fitness Trajectories (C_{{G}} = {gvecUpdates})", 1); - - using (var cw = new System.IO.StreamWriter(topDir + "\\config.txt")) - { - cw.WriteLine("# Config for " + topDir); - - config.WriteOut(cw); - } - - System.IO.File.WriteAllText(topDir + "\\matab.txt", maTable.ToString()); - } - - - public static void Type2PopulationEvolvabilityTest4(int gvecUpdates, int populationSize) - { - VectorTarget oldTarget = new VectorTarget("+---", "o"); - VectorTarget newTarget = new VectorTarget("-+++", "n"); - DenseGenome fox = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.25, 0.25, 0.25)); - DenseGenome husky4 = new DenseGenome(oldTarget.Vector, Extensions.CreateGRS("Aaaa", 0.0, 1.0, 1.0)); - - PopulationEvolvabilityTest("evptest4type3_" + "g" + gvecUpdates + "p" + populationSize, 500, 5, 100, oldTarget, newTarget, fox, husky4, populationSize, gvecUpdates, 0.1 / Math.Sqrt(gvecUpdates), true); - } - - public static void PopulationEvolvabilityTest(string prefix, int generations, int samplePeriod, int mutantSamplePeriod, VectorTarget oldTarget, VectorTarget newTarget, DenseGenome fox, DenseGenome husky4, int populationSize, int gvecUpdates = 1, double gMutationMagnitude = 0.1, bool noMA = false) - { - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - //ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - ReproductionRules rrules = new ReproductionRules(gMutationMagnitude, 0, 0, false, gvecUpdates); // disable B mutation - JudgementRules jrules = new JudgementRules(0.0, JudgementRules.ConstantEquivalent, 0.0); - - Evolvability.EnvironmentTransitionConfiguration config = new Evolvability.EnvironmentTransitionConfiguration(generations, samplePeriod, drules, rrules, jrules); - Evolvability.EnvironmentPopulationTransitionConfiguration popConfig = new Evolvability.EnvironmentPopulationTransitionConfiguration(config, SelectorPreparers<DenseIndividual>.Ranked); - - int repeats = 1000; - - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - ModelExecutionContext context = new ModelExecutionContext(rand); - - StringBuilder maTable = new StringBuilder(); - - int unsulliedMutantAnalysisPeriod = mutantSamplePeriod; - int mutantAnalysisPeriod = (int)Math.Ceiling((double)unsulliedMutantAnalysisPeriod / config.SamplePeriod) * config.SamplePeriod; - void subSampler(int generation, DenseGenome genome, Phenotype p, MultiMeasureJudgement judgement, string name, string outDir, Func<string, string> file) - { - if (!noMA && generation % mutantAnalysisPeriod == 0) - { - Console.WriteLine("(MutantAnalysis" + generation + ")"); - MutantAnalysis ma = new MutantAnalysis(genome, 100000, context, rrules, drules, jrules, newTarget); - ma.Plot(outDir, name + "MA" + generation, name + " MA" + generation + ": "); - MiscPlotting.PlotVectorFolded(name + " IS" + generation + ": ", file("IS" + generation), genome.InitialState); - - int pCount = ma.FitnessChanges.Count(df => df > 0); - double pAvg = pCount > 0 ? ma.FitnessChanges.Where(df => df > 0).Average() * pCount / (double)ma.FitnessChanges.Length : 0.0; - maTable.AppendLine(generation + "\t" + pAvg + "\t" + pCount); - } - } - - string topDir = prefix + "_" + Misc.NowTime; - - Evolvability.SimpleSampler fs = new Evolvability.SimpleSampler(topDir, "Dense", subSampler); - Evolvability.SimpleSampler hs = new Evolvability.SimpleSampler(topDir, "Column", subSampler); - - int exposureEpoch = 0; - - maTable.AppendLine("Dense"); - var fr = Evolvability.RunManyEnvironmentPopulationTransitionExperiments(context, repeats, fox, populationSize, newTarget, fs, popConfig, true, exposureEpoch); - maTable.AppendLine("Column"); - var hr = Evolvability.RunManyEnvironmentPopulationTransitionExperiments(context, repeats, husky4, populationSize, newTarget, hs, popConfig, true, exposureEpoch); - - Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string> ft = new Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string>(fr, OxyColors.Blue, "Dense"); - Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string> ht = new Tuple<Evolvability.EnvironmentTransitionResults[], OxyColor, string>(hr, OxyColors.Red, "Column"); - - Evolvability.PlotAreas(new[] { ht, ft }, topDir + "\\ehstp.pdf", prefix + " Fitness Distribution (Ehst)", 1); - Evolvability.PlotAreas(new[] { ht, ft }, topDir + "\\dfjsp.pdf", $"Distribution of Fitness Trajectories, Population of {populationSize} (C_{{G}} = {gvecUpdates})", 1); - - using (var cw = new System.IO.StreamWriter(topDir + "\\config.txt")) - { - cw.WriteLine("# Config for " + topDir); - - popConfig.WriteOut(cw); - } - - System.IO.File.WriteAllText(topDir + "\\matab.txt", maTable.ToString()); - } - - public static void DiffModExperiment() - { - VectorTarget[] mtargets = new[] - { - new VectorTarget("+------", "a"), - new VectorTarget("-++----", "b"), - new VectorTarget("---++++", "c"), - }; - - // config - int N = mtargets[0].Size; - int epochs = 10000; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.1, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "DiffMods"; - Experiment.RunExperiment(rand, config, "DiffMods_" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false); - } - - public static void L1SpikyTargetCountingExperiments() - { - Dictionary<Tuple<double, double>, string> Terminals = new Dictionary<Tuple<double, double>, string>(); - - string topDir = "STCExp2"; - - int repeats = 100; - double huskyThreshold = 0.6; - - int[] beamCheck = new int[4]; - int[] huskyBeamCheck = new int[4]; - - foreach (double ε in new[] { 0.0, 0.01, 0.02, 0.05, 0.10 }) - { - foreach (var l in new[] { 0.0, 0.1, 0.2, 0.3 }) - { - int zeroHuskies = 0; - int huskies = 0; - for (int i = 0; i < repeats; i++) - { - var t = L1SpikyTargetExperiment(topDir, "ST:", ε, l, 0.001, 200, i > 0); // print one copy - var ma = new ModuleAnalysis(t.Genome); - if (ma.Huskyness > huskyThreshold) - { - huskies++; - - if (ma.BeamIndex == 0) // ε is at zero - { - zeroHuskies++; - } - - huskyBeamCheck[ma.BeamIndex]++; - } - - beamCheck[ma.BeamIndex]++; - } - - Terminals.Add(new Tuple<double, double>(ε, l), $"{zeroHuskies}/{huskies}"); - } - } - - // print out stuff - using (var cw = new System.IO.StreamWriter(System.IO.Path.Combine(topDir, "STCReport.txt"))) - { - cw.WriteLine("Repeats: " + repeats); - cw.WriteLine("Husky Threshold: " + huskyThreshold); - cw.WriteLine("BeamCheck: " + string.Join(", ", beamCheck)); - cw.WriteLine("HuskyBeamCheck: " + string.Join(", ", huskyBeamCheck)); - WriteStuff(cw, Terminals, "ε", "L"); - } - } - - public static void L1SpikyTargetExperiments() - { - Dictionary<Tuple<double, double>, ModuleAnalysis> Terminals = new Dictionary<Tuple<double, double>, ModuleAnalysis>(); - - string topDir = "STExp2"; - - foreach (double ε in new[] { 0.0, 0.1, 0.2, 0.3, 1.0 }) - { - foreach (var l in new[] { 0.1, 0.2, 0.3 }) - { - var t = L1SpikyTargetExperiment(topDir, "ST:", ε, l, 0.001, 200); - Terminals.Add(new Tuple<double, double>(ε, l), new ModuleAnalysis(t.Genome)); - } - } - - string cstr(ModuleAnalysis ma) - { - return ma.BeamIndex + "@" + ma.Huskyness + "/" + ma.SnowflakeBeamRatio; - } - - // print out stuff - using (var cw = new System.IO.StreamWriter(System.IO.Path.Combine(topDir, "STReport.txt"))) - { - cw.WriteLine("beamIndex@huskyness/snowlakeBeamRatio"); - WriteStuff(cw, Terminals.ToDictionary(k => k.Key, k => cstr(k.Value)), "ε", "L"); - } - } - - public static TerminalInfo L1SpikyTargetExperiment(string topDir, string titlePrefix, double ε, double lambda = 0.0, double M_B = 0.001, int epochs = 200, bool disableIO = false) - { - if (!System.IO.Directory.Exists(topDir)) - { - System.IO.Directory.CreateDirectory(topDir); - } - - VectorTarget[] mtargets = new[] - { - new VectorTarget(new[] { 1.0 + ε, 1.0, 1.0, 1.0 }, "T+"), - new VectorTarget(new[] { -1.0 - ε, -1.0, -1.0, -1.0 }, "T-"), - }; - - // config - int N = mtargets[0].Size; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, M_B, 1, false); - JudgementRules jrules = new JudgementRules(lambda, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "L1_" + lambda.ToString("0.00") + "_E" + ε.ToString("0.00"); - string dir = System.IO.Path.Combine(topDir, "Exp_" + catchyName); - var terminals = Experiment.RunExperiment(rand, config, dir, titlePrefix: titlePrefix + catchyName + " ", bigSaveCount: 5, openTerminalsPlots: false, disableIO: disableIO); - - return terminals; - } - - public static void FoxHuskyExperiments() - { - Dictionary<Tuple<double, double>, TerminalInfo> Terminals = new Dictionary<Tuple<double, double>, TerminalInfo>(); - - string topDir = "FHExp1"; - - foreach (var mb in new[] { 0.01, 0.001, 0.0001 }) - foreach (var l in new[] { 0.1, 0.2, 0.3, 0.32, 0.35 }) - { - var t = FoxHuskyExperiment(topDir, "FHL:", l, mb); - Terminals.Add(new Tuple<double, double>(mb, l), t); - } - - // print out stuff - using (var cw = new System.IO.StreamWriter(System.IO.Path.Combine(topDir, "FHReport.txt"))) - { - WriteStuff(cw, Terminals.ToDictionary(k => k.Key, k => k.Value.Fitness.ToString()), "M_B", "L"); - } - } - - public static TerminalInfo FoxHuskyExperiment(string topDir, string titlePrefix, double lambda, double M_B) - { - if (!System.IO.Directory.Exists(topDir)) - { - System.IO.Directory.CreateDirectory(topDir); - } - - VectorTarget[] mtargets = new[] - { - new VectorTarget("++--", "3_2"), - new VectorTarget("++++", "3_3"), - }; - - // config - int N = mtargets[0].Size; - int epochs = 1000; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, M_B, 1, false); - JudgementRules jrules = new JudgementRules(lambda, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "L1_" + lambda.ToString("0.00") + "_MB" + M_B.ToString("0.0000"); - string dir = System.IO.Path.Combine(topDir, "Exp_" + catchyName); - var terminals = Experiment.RunExperiment(rand, config, dir, titlePrefix: titlePrefix + catchyName + " ", bigSaveCount: 5, openTerminalsPlots: false); - - return terminals; - } - - - - public static void TestExperiment_SoloTarget() - { - VectorTarget[] mtargets = new[] - { - new VectorTarget("++++", "3_3"), - }; - - // config - int N = mtargets[0].Size; - int epochs = 300; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.0, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "Solo4"; - Experiment.RunExperiment(rand, config, "SoloExp_" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false, mutantAnalysisPeriod: 20); - } - - public static void TestExperiment() - { - VectorTarget[] allMTargets = new[] - { - new VectorTarget(" ", "0_0"), // netural - - new VectorTarget(" --", "1_0"), // what happens when we only select on one module at a time? - new VectorTarget(" ++", "1_1"), - - new VectorTarget("-- ", "2_0"), - new VectorTarget("++ ", "2_1"), - - new VectorTarget("----", "3_0"), - new VectorTarget("--++", "3_1"), - new VectorTarget("++--", "3_2"), - new VectorTarget("++++", "3_3"), - }; - - //Target[] allMTargets = new[] - //{ - // new Target(" ", "0_0"), // netural - - // new Target(" -- ", "1_0"), // what happens when we only select on one module at a time? - // new Target(" ++ ", "1_1"), - - // new Target("-- ", "2_0"), - // new Target("++ ", "2_1"), - - // new Target("---- +++", "3_0"), - // new Target("--++ ---", "3_1"), - // new Target("++-- ---", "3_2"), - // new Target("++++ +++", "3_3"), - //}; - - VectorTarget[] mtargets = new[] - { - allMTargets[5], - allMTargets[6], - - allMTargets[7], - allMTargets[8], - }; - - // config - int N = mtargets[0].Size; - int epochs = 300; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(0.0, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "SlowB"; - Experiment.RunExperiment(rand, config, "TestExp_" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false); - } - - public static void HierarchyForProfitExperiments() - { - // first, let's ignore gv and k - //HierarchyForProfitExperiment(300, 1, false, 0.1, 20_000); - //HierarchyForProfitExperiment(300, 1, true, 0.1, 20_000); - - // ok, reduce K a lot - //HierarchyForProfitExperiment(300, 1, false, 0.1, 2_000); - //HierarchyForProfitExperiment(300, 1, true, 0.1, 2_000); - - // eerrmm, go all out - HierarchyForProfitExperiment(1000, 4, false, 0.1, 2_000); - HierarchyForProfitExperiment(1000, 4, true, 0.1, 2_000); - } - - public static void HierarchyForProfitExperiment(int epochs, int gv, bool intraModuleColumnConstraint, double λ, int K = 20_000) - { - VectorTarget[] allMTargets = new[] - { - // Typ1Typ1Typ2Typ3 - new VectorTarget("----------------", "llll"), - new VectorTarget("----++++++++----", "lhhl"), - new VectorTarget("++++++++--------", "hhll"), - new VectorTarget("++++----++++----", "hlhl"), - }; - - VectorTarget[] mtargets = new[] - { - allMTargets[0], - allMTargets[1], - allMTargets[2], - allMTargets[3], - }; - - // config - int N = mtargets[0].Size; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.1 / (15 * N * N), 1, false, gv); - JudgementRules jrules = new JudgementRules(λ, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - ITransMatMutator tmm = intraModuleColumnConstraint ? Mutators.Columns(16, 4) : null; - - DenseGenome genome = DenseGenome.CreateDefaultGenomeDense(N, tmm); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = $"gv{gv}λ{λ}c{intraModuleColumnConstraint}"; - Experiment.RunExperiment(rand, config, "HFP_" + catchyName, titlePrefix: "HFP" + catchyName + " ", openTerminalsPlots: false, startingGenome: genome); - } - - public static void FourTwoFourWheelers() - { - //foreach (double λ in new[] { 0.1, 0.2, 0.5, 1.0, 5.0, 0.0 }) - foreach (double λ in new[] { 0.0, 0.01, 0.1, 1.0 }) - FourTwoFourWheeler(λ); - } - - private class ClampedJudger : IVectorTargetJudger - { - public string Name => "ClampedJudger"; - - public string Description => $"ClampedJudger (Magnitude={Magnitude})"; - - public double Magnitude { get; } - - public double Judge(Linear.Vector<double> vector, Phenotype phenotype) - { - return vector.DotProduct(phenotype.Vector.Map(te => ReproductionRules.ClampZeroOne.Clamp(te), Linear.Zeros.Include)); - } - } - - public static void FourTwoFourWheeler(double λ) - { - IVectorTargetJudger clampedJudger = new ClampedJudger(); - - VectorTarget[] mtargets = new[] - { - new VectorTarget("+--- ", "1.of4in9", clampedJudger), - new VectorTarget("-+-- ", "2.of4in9", clampedJudger), - new VectorTarget("--+- ", "3.of4in9", clampedJudger), - new VectorTarget("---+ ", "4.of4in9", clampedJudger), - }; - - // config - int N = mtargets[0].Size; - int epochs = 300; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false, 1); - JudgementRules jrules = new JudgementRules(λ, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "4in9PoziCJλ" + λ; - Experiment.RunExperiment(rand, config, "FourTwoFour" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false, bigSaveCount: 50); - } - - public static void Ands() - { - foreach (double λ in new[] { 0.0, 0.01, 0.1, 1.0 }) - And(λ); - } - - public static void And(double λ) - { - VectorTarget[] mtargets = new[] - { - new VectorTarget("---------", "L0R0"), - new VectorTarget("+++------", "L1R0"), - new VectorTarget("---+++---", "L0R1"), - new VectorTarget("+++++++++", "L1R1"), - }; - - // config - int N = mtargets[0].Size; - int epochs = 300; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false, 1); - JudgementRules jrules = new JudgementRules(λ, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.RandomNoRepeat, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "Andλ" + λ; - Experiment.RunExperiment(rand, config, "And" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false, bigSaveCount: 50); - } - - public static void Paushes() - { - //foreach (var λ in new[] { 0.0, 0.01, 0.1, 1.0 }) - foreach (var λ in new[] { 0.0, 0.01, 0.1, 0.2, 0.3, 0.5, 0.8, 1.0 }) - Paush(λ); - } - - public static void Paush(double λ) - { - // I think this is basically the changes needed... I don't need to write any new code! :D - - VectorTarget[] mtargets = new[] - { - new VectorTarget("++++++---", "L1R0"), - new VectorTarget("------+++", "L0R1"), - new VectorTarget("---------", "L0R0"), - new VectorTarget("+++---+++", "L1R1"), // don't need a special judger, because drules and rrules ensure the Phenotype will only contain posistive trait expressions - }; - - // config - int N = mtargets[0].Size; - int epochs = 3000; - int K = 100; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.MichaelisMentenPaush); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false, 1, ReproductionRules.ClampZeroOne); - JudgementRules jrules = new JudgementRules(λ, JudgementRules.L1Equivalent, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, mtargets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "LMRShortK_λ" + λ; - Experiment.RunExperiment(rand, config, "Paush" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false, bigSaveCount: 20, bigSavePeriod: 1000); - } - - - public static void SharingExperiments() - { - SharingExperiment(0.01, true, JudgementRules.L2Equivalent); - SharingExperiment(0.001, true, JudgementRules.L2Equivalent); - SharingExperiment(0.1, true, JudgementRules.L2Equivalent); - //SharingExperiment(0.1, true, JudgementRules.L2Equivalent); - //SharingExperiment(0.1, false, JudgementRules.L2Equivalent); - //SharingExperiment(1.0, true, JudgementRules.L2Equivalent); - //SharingExperiment(1.0, false, JudgementRules.L2Equivalent); - } - - public static void SharingExperiment(double λ, bool sharing, IRegularisationFunction<IGenome> regularisationFunction) - { - VectorTarget[] targets = new[] - { - new VectorTarget("----", "L0R0"), - new VectorTarget("--++", "L0R1"), - new VectorTarget("++++", "L1R1"), - new VectorTarget("++--", "L1R0"), - }; - - // config - int N = targets[0].Size; - int epochs = 300; - int K = 1000; - - DevelopmentRules drules = new DevelopmentRules(10, 1.0, 0.2, DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(0.1, 0.001, 1, false); - JudgementRules jrules = new JudgementRules(λ, regularisationFunction, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - DenseGenome genome = DenseGenome.CreateDefaultGenome(N, customDevelopmentStep: sharing ? Development.SharingDevelopmentStep(N) : Development.DefaultDevelopmentStep(N)); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - - // go - string catchyName = "S" + sharing + "λ" + λ + regularisationFunction.Name; - Experiment.RunExperiment(rand, config, "Sharing_" + catchyName, titlePrefix: catchyName + " ", openTerminalsPlots: false, bigSavePeriod: 30, bigSaveCount: 3, startingGenome: genome); - } - - public static void PopulationTests() - { - // TODO: varying gClamp - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 5000, K: 1000, columns: false, T: 1, gClamp: new Range(-0.5, 0.5)); // same config: nope - // - - // 3x3 - //PopulationTest(ModularTargetPackage.Standard3x3, 0.1, false, 0.1, 2000, 600, false, 1, 1, 0, false); // nah... - //PopulationTest(ModularTargetPackage.Standard3x3, 0.1, false, 0.1, 5000, 600, true, 1, 1, 0, false); // eh... - //PopulationTest(ModularTargetPackage.Standard3x3, 0.1, false, 0.1, 5000, 800, true, 1, 1, 0, false); // yes, fuzzy - //PopulationTest(ModularTargetPackage.Standard3x3, 0.1, false, 0.1, 5000, 1000, true, 1, 1, 0, false); // yes - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 5000, K: 1200, columns: true, T: 1); // just about - // - - // col -> !col, config otherwise the same - var colSample = LoadGenome(@"..\Release\PopTestStd3x3_λ0.1EpiFalseNoise0.1E5000K1200CT1PF_20180424T195904\genome5000.dat"); - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 5000, K: 1000, columns: false, T: 1, g0: colSample.InitialState, b0: colSample.TransMat); // same config: nope - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 5000, K: 1000, columns: false, T: 1, g0: colSample.InitialState, b0: colSample.TransMat, popSize: 25); // larger population: switches faster - //PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, noiseTerm: 0.1, epochs: 5000, K: 500, columns: false, T: 1, g0: colSample.InitialState, b0: colSample.TransMat, popSize: 25); // larger population with shorter K: - - PopulationTest(ModularTargetPackage.Standard3x3, λ: 0.1, epigenetic: false, gNoiseTerm: 0.1, epochs: 5000, K: 600, columns: true, T: 1, g0: colSample.InitialState, b0: colSample.TransMat, popSize: 200); // larger population with shorter K: - // - - - // TODO: run a few of these for good luck - //for (int i = 0; i < 5; i++) - //{ - // PopulationTest(ModularTargetPackage.Standard3x3, 0.1, false, 0.1, 5000, 1000, true, 1, 1, null, false); // yes - //} - // - - // s8 - //PopNotExperiment(ModularTargetPackage.Single8, 0.1, 0.1, 1000, 500, false, 10, false, 1, 0, true, false); - //PopulationTest(ModularTargetPackage.Single8, 0.1, false, 0.1, 1000, 500, false, 10, 1, 0, false); - // - - - // COLUMN - - // 2x2 - // Column Time - //PopulationTest(0.2, false, 0.1, 5000, 100, true); // nothing - //PopulationTest(0.2, false, 0.1, 5000, 125, true); // jumped the pit of dispair, too late to see any progress - //PopulationTest(0.2, false, 0.1, 5000, 200, true); // hierarchy - //PopulationTest(0.2, false, 0.1, 5000, 500, true); // hierarchy (1 switch) - //PopulationTest(0.2, false, 0.1, 5000, 1000, true); // hierarhcy (many switches) - - // pop-not equivalents (shouldn't evolve hierarchy, because it can't exploit it) - //PopNotExperiment(0.2, 0.1, 5000, 100, true); // nothing - //PopNotExperiment(0.2, 0.1, 5000, 125, true); // nothing - //PopNotExperiment(0.2, 0.1, 5000, 200, true); // nothing - //PopNotExperiment(0.2, 0.1, 5000, 500, true); // ... iffy hierarchy... - //PopNotExperiment(0.2, 0.1, 5000, 1000, true); // seems to evolve hierarchy... and then give up on it - // end 2x2 - - // 4x4 - //PopulationTest(0.2, false, 0.1, 10000, 500, true); // no - //PopulationTest(0.2, false, 0.1, 10000, 800, true); // yes - //PopulationTest(0.2, false, 0.1, 10000, 1000, true); // yes - //PopulationTest(0.2, false, 0.1, 10000, 1500, true); // yes, subordinates generally complete switching - - //PopNotExperiment(0.2, 0.1, 10000, 500, true); // not happening (can't get past 0.5 fitness: too much B? L1 divides by N^2, so no, probably not) - //PopNotExperiment(0.2, 0.1, 10000, 800, true); // not happening (can't get past 0.5 fitness: too much B?) - //PopNotExperiment(0.2, 0.1, 10000, 1000, true); // not happening (can't get past 0.5 fitness: too much B?) - //PopNotExperiment(0.2, 0.1, 10000, 1500, true); // not happening (can't get past 0.5 fitness: too much B?) - //end 4x4 - - // 4x1 - //PopulationTest(0.2, false, 0.1, 2000, 250, true); // no - //PopulationTest(0.2, false, 0.1, 2000, 500, true); // yes - //PopulationTest(0.2, false, 0.1, 2000, 800, true); // yes - //PopulationTest(0.2, false, 0.1, 2000, 1000, true); // yes - - //PopNotExperiment(0.2, 0.1, 2000, 250, true); // no - //PopNotExperiment(0.2, 0.1, 2000, 500, true); // not clear - //PopNotExperiment(0.2, 0.1, 2000, 800, true); // looks like it... - //PopNotExperiment(0.2, 0.1, 2000, 1000, true); // yeah... probably - //PopNotExperiment(0.2, 0.1, 2000, 1600, true, 10, true, 0.5); // very much so - //PopNotExperiment(0.2, 0.1, 2000, 3200, true, 10, true, 0.5); // yes - //PopNotExperiment(0.2, 0.1, 2000, 6400, true, 10, true, 0.5); // yes - //PopNotExperiment(0.2, 0.1, 1000, 100000, true, 10, true, 0.5); // yes - //PopNotExperiment(0.2, 0.1, 2000, 1600, true, 10, true, 0.5); // very much so - //PopNotExperiment(0.02, 0.1, 100000, 1600, true, 1, true, 0.5); // oh dear - //PopNotExperiment(0.02, 0.0, 100000, 1600, true, 1, true, 0.5, 1.0); // hmm - //PopNotExperiment(0.02, 0.0, 100000, 1000, true, 1, true, 1, 1.0, true); // oh dear (nightmare) - //end 4x1 - - // 4x1: not cols, T = 1 - //PopulationTest(0.2, false, 0.1, 2000, 800, false, 1); // no... in 2k epochs, atleast - //PopulationTest(ModularTargetPackage.Standard4x1, 0.02, false, 0.1, 5000, 800, false, 1); // kinda... - //PopulationTest(ModularTargetPackage.Standard4x1, 0.02, false, 0.1, 5000, 400, false, 1); // absolutely - //PopulationTest(ModularTargetPackage.Standard2x2, 0.02, false, 0.1, 5000, 400, false, 1); // run it longer - //PopulationTest(ModularTargetPackage.HighJudge4x1, 0.02, false, 0.1, 5000, 800, false, 1); // errr... - //PopulationTest(ModularTargetPackage.HighJudge4x1, 0.02, false, 0.1, 5000, 400, false, 1, 1.0, 1.0, true); // not really... - //PopulationTest(ModularTargetPackage.Standard2x2, 0.02, false, 0.1, 25000, 400, false, 1); - // - - // 4x4s: not cols, T = 1 - //PopulationTest(ModularTargetPackage.Standard4x4, 0.02, false, 0.1, 20000, 800, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.2, false, 0.1, 10000, 1600, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.4, false, 0.1, 1000, 1600, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.8, false, 0.1, 1000, 1600, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.2, false, 0.1, 1000, 1000, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.4, false, 0.1, 1000, 1000, false, 1); - //PopulationTest(ModularTargetPackage.Standard4x4, 0.8, false, 0.1, 1000, 1000, false, 1); - // - - // a 'traditional' 4x1 run (looking for negative RCSing) (note: using continous B) - //PopNotExperiment(0.2, 0.1, 1000, 1600, false, 10, false, 1.0); // not noticable... but we do have ISes going the wrong way - - // 4x1, T=1 (TODO: work out params) - //PopulationTest(0.2, false, 0.1, 5000, 250, true, 1); - //PopulationTest(0.2, false, 0.1, 5000, 500, true, 1); - //PopulationTest(0.2, false, 0.1, 5000, 800, true, 1); - //PopulationTest(0.2, false, 0.1, 5000, 1000, true, 1); - - //PopNotExperiment(0.2, 0.1, 5000, 250, true, 1); - //PopNotExperiment(0.2, 0.1, 5000, 500, true, 1); - //PopNotExperiment(0.2, 0.1, 5000, 800, true, 1); - //PopNotExperiment(0.2, 0.1, 5000, 1000, true, 1); - //end 4x1, T=1 - - // TODO: run these columns with an f-sparse initial genome - - // NOT COLUMN - - //PopulationTest(0.2, false, 0.1, 5000, 1000); // iffy hierarchy, but switches in ~50 generations - //PopulationTest(0.2, false, 0.02, 5000, 1000); // beautiful hierarchy, but takes 400 generations to switch - //PopulationTest(0.2, false, 0.1, 5000, 100); // just noise (can't evolve properly) - //PopulationTest(0.2, false, 0.1, 5000, 200); // good hierarchy, subordinate initial expression doesn't quite switch (nice) - //PopulationTest(0.2, false, 0.1, 5000, 150); // reasonable hierarchy, subrodinate initial expressions don't really switch (v. nice) - - // IDEA: it looks like we can encourage a better hierarhcy by switching more often ---> evolving to be evolvable?! - - //PopulationTest(0.2, false, 0.1, 5000, 125); - //PopulationTest(0.2, false, 0.1, 5000, 125); - //PopulationTest(0.2, false, 0.1, 5000, 500); - //PopulationTest(0.2, false, 0.1, 5000, 1000); - - // OK, looks like the previous runs were not indictaive of a clear-cut reality - - //PopulationTest(0.2, false, 0.1, 5000, 150); - //PopulationTest(0.2, false, 0.1, 5000, 200); - //PopulationTest(0.2, false, 0.1, 5000, 150); - //PopulationTest(0.2, false, 0.1, 5000, 200); - //PopulationTest(0.2, false, 0.1, 5000, 500); - - // more noise - - //PopulationTest(0.2, false, 0.2, 5000, 200); - //PopulationTest(0.2, false, 0.3, 5000, 200); - //PopulationTest(0.2, false, 0.4, 5000, 200); - //PopulationTest(0.2, false, 0.2, 5000, 500); - //PopulationTest(0.2, false, 0.3, 5000, 500); - //PopulationTest(0.2, false, 0.4, 5000, 500); - - //PopulationTest(0.2, false, 0.2, 5000, 500); - - // low noise - - //PopulationTest(0.2, false, 0.05, 5000, 100); // excepting ~200gen switch time - //PopulationTest(0.2, false, 0.05, 5000, 200); - //PopulationTest(0.2, false, 0.05, 5000, 300); - //PopulationTest(0.2, false, 0.05, 5000, 500); - - //foreach (double nt in new[] { 0.001, 0.01, 0.1, 0.2, 0.5, 1.0 }) - // PopulationTest(0.0, true, nt); - - //PopulationTest(0.0, false); - //PopulationTest(0.0, true, 1.0); - //PopulationTest(0.0, true, 2.0); - //PopulationTest(0.1, false); - //PopulationTest(0.1, true); - //PopulationTest(1.0, false); - //PopulationTest(1.0, true); - } - - public static void PopulationTest(ModularTargetPackage targetPackage, double λ, bool epigenetic, double gNoiseTerm = 0.1, int epochs = 5000, int K = 1000, bool columns = false, int T = 10, double bProb = 1.0, Linear.Vector<double> g0 = null, bool gFixed = false, bool paretoSelection = false, Linear.Matrix<double> b0 = null, int popSize = 5, Range gClamp = null, int tracePeriod = 500, double decayRate = 0.2, ISquash squash = null, string superDir = "", double bNoiseTerm = 0.001, int gUpdateCount = 1, ICycler targetCycler = null, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, double gResetProb = 0, Range gResetRange = null, IRegularisationFunction<IGenome> regularisationFunction = null, string customPrefix = null, bool exclusiveBMutation = false, int eliteCount = 0, bool hillclimberMode = false, bool disableTransientIO = false, IPopulationResetOperation<DenseIndividual> populationResetOperation = null, string dirPrefix = "") - { - bool disableIO = false; - disableTransientIO = disableIO || disableTransientIO; - - VectorTarget[] targets = targetPackage.Targets; - - // config - int N = targetPackage.TargetSize; - int M = targetPackage.ModuleCount; - - gClamp = gClamp ?? new Range(-1.0, 1.0); - regularisationFunction = regularisationFunction ?? JudgementRules.L1Equivalent; - populationResetOperation = populationResetOperation ?? PopulationResetOperations.RandomIndependent; - - DevelopmentRules drules = new DevelopmentRules(T, 1.0, decayRate, squash ?? DevelopmentRules.TanhHalf); - ReproductionRules rrules = new ReproductionRules(gFixed ? 0.0 : (epigenetic ? 0.0 : gNoiseTerm), bNoiseTerm, bProb, exclusiveBMutation, gUpdateCount, gClamp); - JudgementRules jrules = new JudgementRules(λ, regularisationFunction, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, targetCycler ?? Cyclers.Loop, epochs, K, gResetProb, gResetRange, false, drules, rrules, jrules); - - var mutator = columns ? Mutators.Columns2(N, M) : null; - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(); - RandomSource ioRand = new MathNet.Numerics.Random.MersenneTwister(); - ModelExecutionContext context = new ModelExecutionContext(rand); - - g0 = g0 ?? Linear.CreateVector.Dense(N, 0.0); - b0 = b0 ?? Linear.CreateMatrix.Dense(N, N, 0.0); - - DenseIndividual CreateDefaultIndividual(ModelExecutionContext _context) - { - var customDevelopmentStep = Development.DefaultDevelopmentStep(N, epigenetic ? Math.Sqrt(gNoiseTerm) : 0.0); - var genome = new DenseGenome(g0, b0, gOpenEntries, bOpenEntries, mutator, null, null, null, customDevelopmentStep, null, false); - var individual = DenseIndividual.Develop(genome, _context, drules, epigenetic); - return individual; - } - - // population stuff - ISelectorPreparer<DenseIndividual> selector = paretoSelection - ? SelectorPreparers<DenseIndividual>.Pareto - : SelectorPreparers<DenseIndividual>.Ranked; - Population<DenseIndividual> population = new Population<DenseIndividual>(context, _context => CreateDefaultIndividual(_context), popSize); - PopulationExperimentConfig<DenseIndividual> popConfig = new PopulationExperimentConfig<DenseIndividual>(config, selector, eliteCount, hillclimberMode, null, populationResetOperation, DefaultPopulationSpinner<DenseIndividual>.Instance); - PopulationExperimentFeedback<DenseIndividual> feedback = new DefaultDensePopulationExperimentFeedback(ioRand, popConfig, disableTransientIO, disableIO, Math.Max(1, epochs / 25), 500, tracePeriod, null, Math.Max(1, epochs / 1000), populationResetOperation).Feedback; - - // go - string expName = $"PopTest{targetPackage.Name}"; - string subName = "λ" + λ + "Epi" + epigenetic + "mG" + gNoiseTerm + "mB" + bNoiseTerm + "E" + epochs + "K" + K + "C" + (columns ? "T" : "F") + T + "P" + (paretoSelection ? "T" : "F") + decayRate.ToString(".000") + "G" + gUpdateCount; - PopulationExperimentRunners.RunExperiment<DenseIndividual>(System.Console.Out, "", context, population, popConfig, outDir: System.IO.Path.Combine(superDir, dirPrefix + expName + "_" + subName), disableTransientIO: disableTransientIO, feedback: feedback, filePrefix: "", titlePrefix: customPrefix ?? (subName + " ")); - } - - // doesn't support Epi - public static void PopNotExperiment(ModularTargetPackage targetPackage, double λ, double gNoiseTerm = 0.1, int epochs = 5000, int K = 1000, bool columns = false, int T = 10, bool exclusiveB = false, double bProb = 1.0, Linear.Vector<double> g0 = null, bool noRandomIO = false, bool gFixed = false, Linear.Matrix<double> b0 = null, Range gClamp = null, double decayRate = 0.2, double bNoiseTerm = 0.001, int gUpdateCount = 1, ICycler targetCycler = null, IReadOnlyList<int> gOpenEntries = null, IReadOnlyList<MatrixEntryAddress> bOpenEntries = null, IRegularisationFunction<IGenome> regularisationFunction = null, string customPrefix = null, ISquash squash = null, string outDir = "", bool disableTransientIO = false) - { - bool disableIO = false; - - VectorTarget[] targets = targetPackage.Targets; - - // config - int N = targetPackage.TargetSize; - int M = targetPackage.ModuleCount; - - gClamp = gClamp ?? new Range(-1.0, 1.0); - regularisationFunction = regularisationFunction ?? JudgementRules.L1Equivalent; - squash = squash ?? DevelopmentRules.TanhHalf; - - DevelopmentRules drules = new DevelopmentRules(T, 1.0, decayRate, squash); - ReproductionRules rrules = new ReproductionRules(gFixed ? 0.0 : gNoiseTerm, bNoiseTerm, bProb, exclusiveB, gUpdateCount, gClamp); - JudgementRules jrules = new JudgementRules(λ, regularisationFunction, 0.0); - - ExperimentConfiguration config = new ExperimentConfiguration(N, targets, targetCycler ?? Cyclers.Loop, epochs, K, 0, null, false, drules, rrules, jrules); - - ITransMatMutator tmm = columns ? Mutators.Columns2(N, M) : null; - - g0 = g0 ?? Linear.CreateVector.Dense(N, 0.0); - b0 = b0 ?? Linear.CreateMatrix.Dense(N, N, 0.0); - - var genome = new DenseGenome(g0, b0, gOpenEntries, bOpenEntries, tmm, null, null, null, null, null, false); - - // essentials - RandomSource rand = new MathNet.Numerics.Random.MersenneTwister(25000); - RandomSource ioRand = new MathNet.Numerics.Random.MersenneTwister(25000); - //RandomSource rand = new MathNet.Numerics.Random.CryptoRandomSource(); - - // go - string catchyName = $"PopNot{targetPackage.Name}" + "λ" + λ + "Noise" + gNoiseTerm + "E" + epochs + "K" + K + "C" + (columns ? "T" : "F") + T; - outDir = System.IO.Path.Combine(outDir, catchyName); - Experiment.RunExperiment(rand, config, outDir, titlePrefix: customPrefix ?? (catchyName + " "), openTerminalsPlots: false, startingGenome: genome, disableIO: disableIO, mutantAnalysisPeriod: 0, bigSaveCount: 10, ioRand: ioRand, disableTransientIO: disableTransientIO); - } - - private static void Fear(FileStuff stuff) - { - //Random rnd = new Random(1000); - RandomSource rnd = new MathNet.Numerics.Random.MersenneTwister(25001); - - int N = 4; - double[] G = new double[N]; - double[] B = new double[N]; - for (int i = 0; i < N; i++) - { - G[i] = 1; - B[i] = 0; - } - - double λ = 0.02; - int generationsPerEnvironmentPerEpoch = 1000; - - double[] E = new double[] { 1.1, 1 }; - - double[][] rcs = new double[N][]; - - double[][] trRcs = new double[N][]; - double[][] trFitDiff = new double[N][]; - double[][] trFitDiffMean = new double[N][]; - double[][] trFitDiffNorm = new double[N][]; - - int samplePeriod = 100; - int tracePeriod = 10000; - - int epochs = 100000; - - for (int i = 0; i < N; i++) - { - rcs[i] = new double[epochs / samplePeriod + 1]; - - trRcs[i] = new double[generationsPerEnvironmentPerEpoch]; - trFitDiff[i] = new double[generationsPerEnvironmentPerEpoch]; - trFitDiffMean[i] = new double[generationsPerEnvironmentPerEpoch]; - trFitDiffNorm[i] = new double[generationsPerEnvironmentPerEpoch]; - } - - double squash(double x) => Math.Tanh(x * 0.5); - - double fitness(double[] _B, double e) - { - double weightedSum = 0; - for (int i = 0; i < N; i++) - { - weightedSum += G[i] * _B[i]; - } - - double squashed = squash(weightedSum); - - double b = 0; - for (int i = 0; i < N; i++) - { - b += 0.8 * G[i] + squashed; - } - - b /= 5; - b = 0.5 * (1.0 + b / N); - - double c = 0; - for (int i = 0; i < N; i++) - { - c += Math.Abs(_B[i] * N); - } - c /= (N * N); - - return b * e - c * λ; - } - - double f = 0; - for (int epoch = 0; epoch <= epochs; epoch++) - { - bool sampleThis = samplePeriod > 0 && (epoch % samplePeriod == 0); - bool traceThis = tracePeriod > 0 && (epoch % tracePeriod == 0); - - foreach (var e in E) - { - for (int generation = 0; generation < generationsPerEnvironmentPerEpoch; generation++) - { - rnd.NextDouble(); // burn1 - - double[] B2 = new double[N]; - for (int i = 0; i < N; i++) - B2[i] = Misc.Clamp(B[i] + rnd.NextSignedUniform(0.001), double.NegativeInfinity, double.PositiveInfinity); // clamping (or not) - - f = fitness(B, e); - double f2 = fitness(B2, e); - - if (f2 > f) - { - B = B2; - f = f2; - } - - if (traceThis) - { - // fit diff - double[] bNudged = B.ToArray(); - double baseFitness = fitness(bNudged, E.Last()); - - double[] fdiff = new double[N]; - for (int i = 0; i < N; i++) - { - double dC = 0.0001; - double old = bNudged[i]; - - trRcs[i][generation] = B[i]; - - bNudged[i] -= dC; - trFitDiff[i][generation] = fdiff[i] = (fitness(bNudged, e) - baseFitness) / -dC; - bNudged[i] = old; - } - - fdiff.MeanCentreInplace(); - - for (int i = 0; i < N; i++) - { - trFitDiffMean[i][generation] = fdiff[i]; - } - - fdiff.NormaliseInplace(); - - for (int i = 0; i < N; i++) - { - trFitDiffNorm[i][generation] = fdiff[i]; - } - // - } - } - - if (traceThis) - { - PlotLines(stuff.Title("TrRcs" + epoch + e), stuff.File("TrRcs" + epoch + e + ".pdf"), trRcs, 0, samplePeriod, xTitle: "Epoch", yTitle: "Weight"); - PlotLines(stuff.Title("TrFitDiff" + epoch + e), stuff.File("TrFitDiff" + epoch + e + ".pdf"), trFitDiff, 0, samplePeriod, xTitle: "Epoch", yTitle: "dFitness/dColumn"); - PlotLines(stuff.Title("TrFitDiffMean" + epoch + e), stuff.File("TrFitDiffMean" + epoch + e + ".pdf"), trFitDiffMean, 0, samplePeriod, xTitle: "Epoch", yTitle: "dFitness/dColumn (l1 mean centered)"); - PlotLines(stuff.Title("TrFitDiffNorm" + epoch + e), stuff.File("TrFitDiffNorm" + epoch + e + ".pdf"), trFitDiffNorm, 0, samplePeriod, xTitle: "Epoch", yTitle: "dFitness/dColumn (l1 mc'd & normed)"); - } - } - - if (sampleThis) - { - int k = epoch / samplePeriod; - - for (int i = 0; i < N; i++) - { - rcs[i][k] = B[i]; - } - - double p = 100 * (double)epoch / epochs; - Console.WriteLine(epoch + " " + p.ToString("00.0") + "% " + f); - } - - if (epoch % 10000 == 0) - { - PlotLines(stuff.Title("nrcs" + epoch), stuff.File("nrcs" + epoch + ".pdf"), rcs, 0, samplePeriod, xTitle: "Epoch", yTitle: "Weight"); - } - } - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/OneModule.cs b/M4MCode/M4M_MkI/M4M.Old/OneModule.cs deleted file mode 100644 index da8fdf00918b2cf9c738d7ca4868cf90d9305c8b..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/OneModule.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Linear = MathNet.Numerics.LinearAlgebra; - -namespace M4M -{ - public class OneModule - { - public static Linear.Vector<double> PrepareG(int k, double ys, double yr) - { - return Linear.CreateVector.Dense(k + 1, i => i == 0 ? ys : yr); - } - - public static Linear.Matrix<double> PrepareB(int k, double s, double r, double q) - { - return Linear.CreateMatrix.Dense(k + 1, k + 1, (i, j) => j == 0 ? i == 0 ? s : r : q); - } - - public static VectorTarget PrepareE(int k, double es, double er, IVectorTargetJudger customJudger = null) - { - var vec = Linear.CreateVector.Dense(k + 1, i => i == 0 ? es : er); - return new VectorTarget(vec, $"E{k} {es} {er}", customJudger); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/OxyPlotting.cs b/M4MCode/M4M_MkI/M4M.Old/OxyPlotting.cs deleted file mode 100644 index 2917870b681edc6a86dc2559c8aa6e964f260b6a..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/OxyPlotting.cs +++ /dev/null @@ -1,354 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static Plotting.Sillyness; -using static Plotting.Misc; - -namespace Plotting -{ - public static partial class Misc - { - public static Tuple<TL, TR> Label<TL, TR>(TL l, TR r) - { - return new Tuple<TL, TR>(l, r); - } - } - - public static class OxyPlotting - { - public static OxyPlotModelShim Space(string title) - { - return new OxyPlotModelShim(title); - } - - public static OxyPlotModelShim Plot(string title, Func<double, double> func, double x0, double x1, int count) - { - var opms = Space(title); - - opms.LinearX(); - opms.LinearY(); - - opms.PlotLine(title, func, x0, x1, count); - - // no legend - - opms.Sillyify(); - - return opms; - } - - public static OxyPlotModelShim Plot(string title, double[] x, double[] y) - { - var opms = Space(title); - - opms.LinearX(); - opms.LinearY(); - - opms.PlotLine(title, x.Zip(y, (a, b) => Label(a, b))); - - // no legend - - opms.Sillyify(); - - return opms; - } - } - - public class OxyPlotModelShim - { - public OxyPlot.PlotModel PlotModel { get; } - - public string Title - { - get - { - return PlotModel.Title; - } - set - { - PlotModel.Title = value; - } - } - - public OxyPlotModelShim(string title) - { - PlotModel = new OxyPlot.PlotModel() { Title = title }; - } - - public void LinearX(string label = "X", double paddingLow = 0, double paddingHigh = 0, string key = "x", bool flip = false) - { - var axis = new OxyPlot.Axes.LinearAxis() { Key = key, Title = label, Position = OxyPlot.Axes.AxisPosition.Bottom, IsPanEnabled = false, IsZoomEnabled = false }; - - if (flip) - { - axis.StartPosition = 1; - axis.EndPosition = 0; - } - - PlotModel.Axes.Add(axis); - } - - public void LinearY(string label = "Y", double paddingLow = 0, double paddingHigh = 0, string key = "y", bool flip = false) - { - var axis = new OxyPlot.Axes.LinearAxis() { Key = key, Title = label, Position = OxyPlot.Axes.AxisPosition.Left, IsPanEnabled = false, IsZoomEnabled = false }; - - if (flip) - { - axis.StartPosition = 1; - axis.EndPosition = 0; - } - - PlotModel.Axes.Add(axis); - } - - public void LogarithmicX(string label = "X", double paddingLow = 0, double paddingHigh = 0, string key = "x", bool flip = false) - { - var axis = new OxyPlot.Axes.LogarithmicAxis() { Key = key, Title = label, Position = OxyPlot.Axes.AxisPosition.Bottom, IsPanEnabled = false, IsZoomEnabled = false }; - - if (flip) - { - axis.StartPosition = 1; - axis.EndPosition = 0; - } - - PlotModel.Axes.Add(axis); - } - - public void LogarithmicY(string label = "Y", double paddingLow = 0, double paddingHigh = 0, string key = "y", bool flip = false) - { - var axis = new OxyPlot.Axes.LogarithmicAxis() { Key = key, Title = label, Position = OxyPlot.Axes.AxisPosition.Left, IsPanEnabled = false, IsZoomEnabled = false }; - - if (flip) - { - axis.StartPosition = 1; - axis.EndPosition = 0; - } - - PlotModel.Axes.Add(axis); - } - - public void Colours(string label = "Colours", string key = "c") - { - var axis = new OxyPlot.Axes.LinearColorAxis() { Key = key, Title = label, Position = OxyPlot.Axes.AxisPosition.Right, IsPanEnabled = false, IsZoomEnabled = false }; - PlotModel.Axes.Add(axis); - } - - public void ColoursGrey(string key = "c", double? min = null, double? max = null) - { - var axis = new OxyPlot.Axes.LinearColorAxis { Key = key, Title = "", Position = OxyPlot.Axes.AxisPosition.Right, IsPanEnabled = false, IsZoomEnabled = false, HighColor = OxyPlot.OxyColors.White, LowColor = OxyPlot.OxyColors.Black, Palette = OxyPlot.OxyPalettes.Gray(100) }; - - if (max.HasValue) - axis.Maximum = max.Value; - if (min.HasValue) - axis.Minimum = min.Value; - - PlotModel.Axes.Add(axis); - } - - public void ShowLegend() - { - PlotModel.IsLegendVisible = true; - } - - public void HideLegend() - { - PlotModel.IsLegendVisible = false; - } - - public void Legend(string title, bool show = true) - { - PlotModel.IsLegendVisible = show; - PlotModel.LegendTitle = title; - } - - public void ExportPdf(string filename, double width, bool smAlso = true) - { - OldOxyPlotting.ExportToPdf(PlotModel, filename, width, false, false, null); - - if (smAlso) - ExportPdf(filename + "_sm", width * 0.9, false); - } - - private void ForceCompute() - { - PlotModel.ResetAllAxes(); - PlotModel.InvalidatePlot(true); - - int failCount = 0; - again: - try - { - ExportPdf("nil.pdf", 1); // HACK HACK: only way I can find to get it to compute everything... - } - catch - { - if (failCount == 0) - Console.WriteLine("nil err"); - - failCount++; - - if (failCount < 10) - { - System.Threading.Thread.Sleep(10 * failCount); - goto again; - } - else - { - Console.WriteLine("nil failed, plot may be invalid (" + PlotModel.Title + ")"); - return; - } - } - } - - public void Sillyify(int sillyTickCount, string key) - { - ForceCompute(); // hack hack - - var a = PlotModel.Axes.First(x => x.Key == key); - Sillyify(sillyTickCount, a); - - PlotModel.InvalidatePlot(true); - } - - public static void Sillyify(int sillyTickCount, OxyPlot.Axes.Axis a) - { - double min = a.DataMinimum; - double max = a.DataMaximum; - - Sillyify(sillyTickCount, a, min, max); - } - - public static void Sillyify(int sillyTickCount, OxyPlot.Axes.Axis a, double min, double max) - { - double tickStep = SillyCeil10((max - min) / sillyTickCount); - max = SillyCeil(max, tickStep / 2.0); // TODO: replace these with something based on the tickStep - min = SillyFloor(min, tickStep / 2.0); - - a.Maximum = max; - a.Minimum = min; - a.MajorStep = tickStep; - a.MajorGridlineStyle = OxyPlot.LineStyle.Solid; - } - - public void Sillyify(int sillyTickCount = 10) - { - ForceCompute(); // hack hack - - if (PlotModel.Series.Count > 0) - { - foreach (var a in PlotModel.Axes) - { - Sillyify(sillyTickCount, a); - } - } - - PlotModel.InvalidatePlot(true); - } - - public OxyPlot.Series.LineSeries PlotLine(string title, Func<double, double> func, double x0, double x1, int count) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.FunctionSeries(func, x0, x1, count, title) { LineStyle = OxyPlot.LineStyle.Solid, MarkerType = OxyPlot.MarkerType.None, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.LineSeries PlotScatter(string title, Func<double, double> func, double x0, double x1, int count) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.FunctionSeries(func, x0, x1, count, title) { LineStyle = OxyPlot.LineStyle.None, MarkerType = OxyPlot.MarkerType.Diamond, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.LineSeries PlotSteps(string title, IEnumerable<Tuple<double, double>> steps) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries() { Title = title, LineStyle = OxyPlot.LineStyle.Solid, MarkerType = OxyPlot.MarkerType.None, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - double prev = 0; - bool first = true; - foreach (System.Tuple<double, double> d in steps) - { - if (first) - { - first = false; - } - else - { - series.Points.Add(new OxyPlot.DataPoint(d.Item1, prev)); - } - - series.Points.Add(new OxyPlot.DataPoint(d.Item1, d.Item2)); - prev = d.Item2; - } - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.LineSeries PlotLine(string title, IEnumerable<Tuple<double, double>> points) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries() { Title = title, LineStyle = OxyPlot.LineStyle.Solid, MarkerType = OxyPlot.MarkerType.None, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - foreach (System.Tuple<double, double> d in points) - { - series.Points.Add(d == null ? OxyPlot.DataPoint.Undefined : new OxyPlot.DataPoint(d.Item1, d.Item2)); - } - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.LineSeries PlotLine(string title, IEnumerable<double> points, double x0 = 0, double dx = 1) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries() { Title = title, LineStyle = OxyPlot.LineStyle.Solid, MarkerType = OxyPlot.MarkerType.None, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - double x = x0; - foreach (double d in points) - { - series.Points.Add(new OxyPlot.DataPoint(x, d)); - x += dx; - } - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.LineSeries PlotScatter(string title, IEnumerable<Tuple<double, double>> points) - { - OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries() { Title = title, LineStyle = OxyPlot.LineStyle.None, MarkerType = OxyPlot.MarkerType.Diamond, XAxisKey = "x", YAxisKey = "y", RenderInLegend = true }; - - foreach (System.Tuple<double, double> d in points) - { - series.Points.Add(d == null ? OxyPlot.DataPoint.Undefined : new OxyPlot.DataPoint(d.Item1, d.Item2)); - } - - var s = new OxyPlot.Series.ScatterSeries(); - - PlotModel.Series.Add(series); - return series; - } - - public OxyPlot.Series.HeatMapSeries PlotHeatmap(string title, double[,] darr, double x0, double x1, double y0, double y1) - { - OxyPlot.Series.HeatMapSeries series = new OxyPlot.Series.HeatMapSeries() { Title = title, RenderMethod = OxyPlot.Series.HeatMapRenderMethod.Rectangles, X0 = x0, X1 = x1, Y0 = y0, Y1 = y1, XAxisKey = "x", YAxisKey = "y", ColorAxisKey = "c" }; - series.Data = darr; - - PlotModel.Series.Add(series); - return series; - } - - private IEnumerable<OxyPlot.Series.Series> EnumerateDataPointSeries(OxyPlot.Axes.Axis axis) - { - foreach (var s in PlotModel.Series) - { - if (s is OxyPlot.Series.DataPointSeries dps) - { - yield return s; - } - } - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Properties/AssemblyInfo.cs b/M4MCode/M4M_MkI/M4M.Old/Properties/AssemblyInfo.cs deleted file mode 100644 index 53563a8a0813c62b7c1a21fe388a13b15d0af618..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("M4M.Old")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Microsoft")] -[assembly: AssemblyProduct("M4M.Old")] -[assembly: AssemblyCopyright("Copyright © Microsoft 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("8b27e40d-9db6-4179-a2bd-e9de907509b0")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/M4MCode/M4M_MkI/M4M.Old/Reporting/Basics.cs b/M4MCode/M4M_MkI/M4M.Old/Reporting/Basics.cs deleted file mode 100644 index 8f23fa379a18aa4ab816dda82d9c4a378bbdd294..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Reporting/Basics.cs +++ /dev/null @@ -1,149 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace M4M.Reporting -{ - public class Section : ParentDocElem, IDocElem - { - public string Title { get; } - - public Section(string title) - { - Title = title; - } - - public void WriteOut(DocWriteInfo dwi, TextWriter writer) - { - writer.WriteLine(); - writer.WriteLine(); - - writer.Write(new string('#', dwi.SectionDepth + 2)); - writer.Write(' '); - writer.WriteLine(Title); - - var subDwi = dwi.SubSection(); - foreach (var subElem in SubElements) - subElem.WriteOut(subDwi, writer); - } - - public void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer) - { - string cmd = ""; - for (int i = 0; i < dwi.DocWriteInfo.SectionDepth; i++) - cmd += "sub"; - cmd += "section"; - - writer.Write(cmd, Title); - - var subDwi = dwi.SubSection(); - foreach (var subElem in SubElements) - subElem.WriteOutTex(subDwi, writer); - } - } - - public class RawParagraph : IDocElem - { - public string Text { get; } - - public RawParagraph(string text) - { - Text = text; - } - - public void WriteOut(DocWriteInfo dwi, TextWriter writer) - { - writer.WriteLine(); - writer.WriteLine(Text); - } - - public void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer) - { - writer.WriteEmptyLine(); - writer.WriteRawLine(Text); - } - - public void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo) - { - } - } - - public class RawInlineText : IDocElem - { - public string Text { get; } - - public RawInlineText(string text) - { - Text = text; - } - - public void WriteOut(DocWriteInfo dwi, TextWriter writer) - { - writer.Write(Text); - } - - public void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer) - { - writer.WriteRaw(Text); - } - - public void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo) - { - } - - public static implicit operator RawInlineText(string text) - { - return new RawInlineText(text); - } - } - - public class ItemList : IDocElem - { - public List<IDocElem> Items { get; } = new List<IDocElem>(); - - public ItemList() - { - } - - public void Add(IDocElem item) - { - Items.Add(item); - } - - public void WriteOut(DocWriteInfo dwi, TextWriter writer) - { - writer.WriteLine(); - - foreach (var item in Items) - { - writer.Write(" - "); - item.WriteOut(dwi, writer); - - writer.WriteLine(); - } - } - - public void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer) - { - using (var enumeration = writer.Begin("itemize")) - { - foreach (var item in Items) - { - writer.Write("item"); - item.WriteOutTex(dwi, writer); - - writer.WriteEmptyLine(); - } - } - } - - public void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo) - { - foreach (var item in Items) - item.AccumulateDocumentInfo(globalDocWriteInfo); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Reporting/Figure.cs b/M4MCode/M4M_MkI/M4M.Old/Reporting/Figure.cs deleted file mode 100644 index 1cd314528ccc21e3d905f3522f62e992ab27f8fd..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Reporting/Figure.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace M4M.Reporting -{ - public class FigureGraphic - { - public string Path { get; } - public string Width { get; } - - public FigureGraphic(string path, string width) - { - Path = path; - Width = width; - } - } - - public class Figure : IDocElem - { - public string Caption { get; set; } - public string Label { get; set; } - public List<FigureGraphic> Graphics { get; } = new List<FigureGraphic>(); - - public Figure() - { - } - - public void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo) - { - } - - public void WriteOut(DocWriteInfo dwi, TextWriter writer) - { - writer.WriteLine(); - writer.WriteLine("Figure: " + Caption); - foreach (var graphic in Graphics) - writer.WriteLine(" Graphic: " + graphic.Path); - } - - public void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer) - { - using (var figure = writer.Begin("figure", null, new[] { "htbp" })) - { - writer.Write("centering"); - - foreach (var graphic in Graphics) - writer.Write("includegraphics", new[] { "width=" + graphic.Width }, graphic.Path, null); - - string captext = Caption + (Label != null ? $"\\label{{{Label}}}" : ""); - writer.Write("caption", captext); - } - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Reporting/Report.cs b/M4MCode/M4M_MkI/M4M.Old/Reporting/Report.cs deleted file mode 100644 index 4ee894e21cd9c1b795b7d2cdfe7ef148cca6b746..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Reporting/Report.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace M4M.Reporting -{ - public class Packages - { - private List<PackageInfo> PackageInfos = new List<PackageInfo>(); - - public void Add(PackageInfo packageInfo) - { - PackageInfos.Add(packageInfo); - } - - public void WriteOutTex(TexWriter writer) - { - foreach (var package in PackageInfos) - { - writer.Write("usepackage", package.PreParameters, package.Package, package.PostParameters); - } - } - - public static readonly Packages DefaultPackages; - - static Packages() - { - DefaultPackages = new Packages(); - DefaultPackages.Add(new PackageInfo("url")); - DefaultPackages.Add(new PackageInfo("ulem")); - //DefaultPackages.Add(new PackageInfo("geometry", new[] { "margin=0.5in" })); - DefaultPackages.Add(new PackageInfo("geometry", new[] { "a4paper" })); - DefaultPackages.Add(new PackageInfo("graphicx")); - DefaultPackages.Add(new PackageInfo("epstopdf")); - DefaultPackages.Add(new PackageInfo("amsmath")); - DefaultPackages.Add(new PackageInfo("amssymb")); - DefaultPackages.Add(new PackageInfo("color")); - DefaultPackages.Add(new PackageInfo("listings")); - DefaultPackages.Add(new PackageInfo("placeins")); - DefaultPackages.Add(new PackageInfo("fontenc", new[] { "T1" })); - } - } - - public class Report : ParentDocElem - { - public string Title { get; } - public string Author { get; } - public string NominalDate { get; } - - public Packages Packages { get; } = Packages.DefaultPackages; - public List<string> GraphicsExtensions = new List<string> { ".png", ".jpg", ".pdf" }; - - public Report(string title, string author, string nominalDate) - { - Title = title; - Author = author; - NominalDate = nominalDate; - } - - public void WriteOut(TextWriter writer) - { - // accumulate gdwi - var globalDocWriteInfo = new GlobalDocWriteInfo(); - foreach (var subElem in SubElements) - subElem.AccumulateDocumentInfo(globalDocWriteInfo); - - // header - writer.WriteLine("# " + Title); - writer.WriteLine(); - writer.WriteLine(Author); - writer.WriteLine(); - writer.WriteLine(NominalDate); - - // content - foreach (var subElem in SubElements) - subElem.WriteOut(new DocWriteInfo(globalDocWriteInfo), writer); - } - - public void WriteOut(string fileName) - { - using (var writer = new StreamWriter(fileName)) - { - WriteOut(writer); - } - } - - public void WriteOutTex(TextWriter writer) - { - var tw = new TexWriter(writer); - - // accumulate gdwi - var globalDocWriteInfo = new GlobalDocWriteInfo(); - foreach (var subElem in SubElements) - subElem.AccumulateDocumentInfo(globalDocWriteInfo); - - // preamble - tw.Write("documentclass", "article"); - - tw.WriteEmptyLine(); - tw.WriteComment("packages"); - Packages.WriteOutTex(tw); - - tw.WriteEmptyLine(); - tw.WriteComment("document"); - using (tw.Begin("document")) - { - // header - tw.Write("title", Title); - tw.Write("author", Author); - tw.Write("date", NominalDate); - tw.Write("maketitle"); - - tw.WriteEmptyLine(); - - // content - foreach (var subElem in SubElements) - subElem.WriteOutTex(new TexDocWriteInfo(globalDocWriteInfo), tw); - - tw.WriteEmptyLine(); - } - } - - public void WriteOutTex(string fileName) - { - using (var writer = new StreamWriter(fileName)) - { - WriteOutTex(writer); - } - } - - public void RenderTex(string fileName, bool openPdf = false, string latexProcessor = "pdflatex") - { - WriteOutTex(fileName); - - string dir = Path.GetDirectoryName(fileName); - string path = Path.GetFullPath(dir); - - System.Diagnostics.ProcessStartInfo latexPsi = new System.Diagnostics.ProcessStartInfo(); - latexPsi.WorkingDirectory = path; - latexPsi.UseShellExecute = false; - latexPsi.FileName = latexProcessor; - latexPsi.Arguments = "\"" + fileName + "\""; - - var pdfLatex = System.Diagnostics.Process.Start(latexPsi); - - pdfLatex.WaitForExit(); - - if (openPdf) - System.Diagnostics.Process.Start("\"" + fileName.Replace(".tex", ".pdf") + "\""); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Reporting/Reporting.cs b/M4MCode/M4M_MkI/M4M.Old/Reporting/Reporting.cs deleted file mode 100644 index db61131116f1725b960c9787172f38e9bd175356..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Reporting/Reporting.cs +++ /dev/null @@ -1,248 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace M4M.Reporting -{ - public class Empty - { - public static readonly string[] String = new string[0]; - } - - public class TexOpenClose : IDisposable - { - public string EndString { get; } - private readonly TexWriter TexWriter; - private IEnumerable<string> PostParams; - - public TexOpenClose(TexWriter texWriter, string command, IEnumerable<string> preParams, IEnumerable<string> postParams) - { - TexWriter = texWriter; - PostParams = postParams; - - TexWriter.WriteOpen(command, preParams); - } - - public void Dispose() - { - TexWriter.WriteClose(PostParams); - } - } - - public class TexBeginEnd : IDisposable - { - public string Command { get; } - private readonly TexWriter TexWriter; - - public TexBeginEnd(TexWriter texWriter, string command, IEnumerable<string> preParams, IEnumerable<string> postParams) - { - TexWriter = texWriter; - Command = command; - - TexWriter.WriteBegin(Command, preParams, postParams); - } - - public void Dispose() - { - TexWriter.WriteEnd(Command); - } - } - - public class TexWriter - { - private TextWriter Writer { get; } - - public void Texify(ref string str) - { - if (str.Contains("\n")) - { - str = str.Replace("\r", ""); - str = str.Replace("\n", "\\\\"); - } - } - - public TexWriter(TextWriter writer) - { - Writer = writer; - } - - public void WriteEmptyLine() - { - Writer.WriteLine(); - } - - public void WriteRaw(string misc) - { - Writer.Write(misc); - } - - public void WriteTexy(string misc) - { - Texify(ref misc); - Writer.Write(misc); - } - - public void WriteRawLine(string misc) - { - Writer.WriteLine(misc); - } - - public void WriteTexyLine(string misc) - { - Texify(ref misc); - Writer.WriteLine(misc); - } - - public void WriteComment(string comment) - { - Writer.Write("% "); - Writer.WriteLine(comment); - } - - public void Write(string command) - { - Writer.WriteLine($"\\{command}"); - } - - public void Write(string command, string content) - { - Texify(ref content); - Writer.WriteLine($"\\{command}{{{content}}}"); - } - public void Write(string command, IEnumerable<string> preParams, string content, IEnumerable<string> postParams) - { - Texify(ref content); - preParams = preParams ?? Empty.String; - postParams = postParams ?? Empty.String; - Writer.WriteLine($"\\{command}{string.Join("", preParams.Select(p => $"[{p}]"))}{{{content}}}{string.Join("", postParams.Select(p => $"[{p}]"))}"); - } - - public TexOpenClose Open(string command, IEnumerable<string> preParams, IEnumerable<string> postParams) - { - return new TexOpenClose(this, command, preParams, postParams); - } - - public void WriteOpen(string command, IEnumerable<string> preParams) - { - preParams = preParams ?? Empty.String; - Writer.WriteLine($"\\{command}{string.Join("", preParams.Select(p => $"[{p}]"))}{{"); - } - - public void WriteClose(IEnumerable<string> postParams = null) - { - postParams = postParams ?? Empty.String; - Writer.WriteLine($"}}{string.Join("", postParams.Select(p => $"[{p}]"))}"); - } - - public TexBeginEnd Begin(string command, IEnumerable<string> preParams = null, IEnumerable<string> postParams = null) - { - return new TexBeginEnd(this, command, preParams, postParams); - } - - public void WriteBegin(string command, IEnumerable<string> preParams, IEnumerable<string> postParams) - { - preParams = preParams ?? Empty.String; - postParams = postParams ?? Empty.String; - Writer.WriteLine($"\\begin{string.Join("", preParams.Select(p => $"[{p}]"))}{{{command}}}{string.Join("", postParams.Select(p => $"[{p}]"))}"); - } - - public void WriteEnd(string command) - { - Writer.WriteLine($"\\end{{{command}}}"); - } - } - - public class GlobalDocWriteInfo - { - public Dictionary<string, object> GlobalUserInfo { get; } - - public GlobalDocWriteInfo() - { - } - } - - public class DocWriteInfo - { - public GlobalDocWriteInfo GlobalDocWriteInfo { get; } - - public int SectionDepth { get; } - - public Dictionary<string, object> ScopedUserInfo { get; } - - private DocWriteInfo(GlobalDocWriteInfo globalDocWriteInfo, int sectionDepth, Dictionary<string, object> scopedUserInfo) - { - SectionDepth = sectionDepth; - ScopedUserInfo = scopedUserInfo; - } - - public DocWriteInfo(GlobalDocWriteInfo globalDocWriteInfo) : this(globalDocWriteInfo, 0, new Dictionary<string, object>()) - { - // nix - } - - public DocWriteInfo SubSection() - { - return new DocWriteInfo(GlobalDocWriteInfo, SectionDepth + 1, new Dictionary<string, object>(ScopedUserInfo)); - } - } - - public class TexDocWriteInfo - { - public DocWriteInfo DocWriteInfo { get; } - - private TexDocWriteInfo(DocWriteInfo docWriteInfo) - { - DocWriteInfo = docWriteInfo; - } - - public TexDocWriteInfo(GlobalDocWriteInfo globalDocWriteInfo) : this(new DocWriteInfo(globalDocWriteInfo)) - { - // nix - } - - public TexDocWriteInfo SubSection() - { - return new TexDocWriteInfo(DocWriteInfo.SubSection()); - } - } - - public interface IDocElem - { - void WriteOut(DocWriteInfo dwi, TextWriter writer); - void WriteOutTex(TexDocWriteInfo dwi, TexWriter writer); - void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo); - } - - public abstract class ParentDocElem - { - protected List<IDocElem> SubElements { get; private set; } = new List<IDocElem>(); - - public void Add(IDocElem subelem) - { - SubElements.Add(subelem); - } - - public virtual void AccumulateDocumentInfo(GlobalDocWriteInfo globalDocWriteInfo) - { - foreach (var subElem in SubElements) - subElem.AccumulateDocumentInfo(globalDocWriteInfo); - } - } - - public class PackageInfo - { - public IReadOnlyList<string> PreParameters { get; } - public string Package { get; } - public IReadOnlyList<string> PostParameters { get; } - - public PackageInfo(string package, IEnumerable<string> preParameters = null, IEnumerable<string> postPrameters = null) - { - Package = package; - PreParameters = preParameters?.ToArray() ?? Empty.String; - PostParameters = postPrameters?.ToArray() ?? Empty.String; - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Sillyness.cs b/M4MCode/M4M_MkI/M4M.Old/Sillyness.cs deleted file mode 100644 index 9d116914c278ad3673f27338fde0eb65089cd75a..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Sillyness.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plotting -{ - public class Sillyness - { - public static double SignedSillyFloor10(double d) - { - if (d == 0) - return 0; - else if (d < 0) - return -SillyCeil10(-d); - else if (d > 0) - return SillyFloor10(d); - else - return double.NaN; - } - - public static double SignedSillyCeil10(double d) - { - if (d == 0) - return 0; - else if (d < 0) - return -SillyFloor10(-d); - else if (d > 0) - return SillyCeil10(d); - else - return double.NaN; - } - - public static double SillyFloor10(double d) - { - double l10 = Math.Log10(d); - l10 = Math.Floor(l10); - double e = Math.Pow(10, l10); - - if (e * 5 <= d) - return e * 5; - if (e * 2 <= d) - return e * 2; - return e; - } - - public static double SillyCeil10(double d) - { - double l10 = Math.Log10(d); - l10 = Math.Ceiling(l10); - double e = Math.Pow(10, l10); - - if (e / 5 >= d) - return e / 5; - if (e / 2 >= d) - return e / 2; - return e; - } - - public static double SillyFloor(double d, double step) - { - return step * Math.Floor(d / step); - } - - public static double SillyCeil(double d, double step) - { - return step * Math.Ceiling(d / step); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.Old/Topology.cs b/M4MCode/M4M_MkI/M4M.Old/Topology.cs deleted file mode 100644 index 12130a4f0c3cc1f99b2fef5be852a81bb0968bb8..0000000000000000000000000000000000000000 --- a/M4MCode/M4M_MkI/M4M.Old/Topology.cs +++ /dev/null @@ -1,213 +0,0 @@ -using OxyPlot; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using MathNet.Numerics.LinearAlgebra; -using MathNet.Numerics.Random; -using static M4M.Misc; -using static M4M.MiscPlotting; - -namespace M4M -{ - public interface ITopology - { - double[][] Develop(double[] coefs, int steps, Func<double, double> squash); - } - - public delegate double[] DifferentialFunction(double[] coeficients, double[] state, Func<double, double> squash); - - public class TopologyStuff - { - public static double Judge(ITopology topology, double[] p, int T, Func<double, double> squash, double lambda) - { - int N = p.Length; - // TODO: /5 term is nasty, sort it out (need proper rules) - double b = 0.5 * (1 + topology.Develop(p, T, squash).Last().Sum() / (5 * N)); - - // TODO: explicit L1 is nasty, for it out (need proper rules) - double c = p.Sum(r => Math.Abs(r)) / (N * N); - - return b - c * lambda; - } - - public static void SolveTopology(string name, ITopology topology, double min, double max, double dd, int T, Func<double, double> squash, int res = 50) - { - var plotModel = Plotting.OldOxyPlotting.LinearAxes(name, "Total Cost", "Weights"); - - int n = 4; - - OxyPlot.Series.LineSeries[] ls = new OxyPlot.Series.LineSeries[n]; - for (int i = 0; i < n; i++) - ls[i] = new OxyPlot.Series.LineSeries() { Title = ""+i }; - - for (double x = min; x <= max; x += dd) - { - double[] p = Misc.ArgMax(Combinatorics.EnumerateOrderedParameterisations(n, x, res), d => Judge(topology, d, T, squash, 0.0)); - - for (int i = 0; i < n; i++) - ls[i].Points.Add(new DataPoint(x, p[i])); - } - - for (int i = 0; i < n; i++) - plotModel.Series.Add(ls[i]); - plotModel.InvalidatePlot(true); - - Plotting.OldOxyPlotting.ExportToPdf(plotModel, name + ".pdf"); - } - } - - public class GeneralTopology : ITopology - { - public static readonly GeneralTopology Flat = new GeneralTopology((coefs, state, squash) => - { - return Misc.Create<double>(state.Length, i => - { - return -0.2 * state[i] + squash(state.Sum() * coefs[i] / 4.0); - }); - }); - - public static readonly GeneralTopology Identity = new GeneralTopology((coefs, state, squash) => - { - return Misc.Create<double>(state.Length, i => - { - return -0.2 * state[i] + squash(state[i] * coefs[i]); - }); - }); - - public static readonly GeneralTopology Column = new GeneralTopology((coefs, state, squash) => - { - return Misc.Create<double>(state.Length, i => - { - return -0.2 * state[i] + squash(state[0] * coefs[i]); - }); - }); - - public GeneralTopology(DifferentialFunction differentialFunction) - { - DifferentialFunction = differentialFunction; - } - - public DifferentialFunction DifferentialFunction { get; } - - public double[][] Develop(double[] coefs, int steps, Func<double, double> squash) - { - double[][] trajs = new double[steps + 1][]; - - double[] state = Misc.Create<double>(coefs.Length, i => 1.0); - trajs[0] = state; - - for (int i = 1; i <= steps; i++) - { - var diff = DifferentialFunction(coefs, state, squash); - - state = state.Zip(diff, (y, dy) => y + dy).ToArray(); - - trajs[i] = state; - } - - return trajs; - } - } - - /// <summary> - /// Reduces the genome to a single vector, which has a topological development and judgement - /// </summary> - public class TopologicalGenome - { - public TopologicalGenome(double[] regulationCoefs, ITopology topology) - { - RegulationCoefs = regulationCoefs; - Topology = topology; - } - - public static TopologicalGenome Empty(int size, ITopology topology) - { - return new TopologicalGenome(new double[size], topology); - } - - public double[] RegulationCoefs { get; } - public ITopology Topology { get; } - - public int Size => RegulationCoefs.Length; - - public double Judge(int T, Func<double, double> squash, double lambda) - { - return TopologyStuff.Judge(Topology, RegulationCoefs, T, squash, lambda); - } - - public TopologicalGenome Mutate(RandomSource rand, double mutationMagnitude) - { - double[] nrcs = new double[Size]; - - for (int i = 0; i < Size; i++) - { - // draw delta - double μ2 = rand.NextSignedUniform(mutationMagnitude); - - // apply (unclamped) - nrcs[i] = μ2 + RegulationCoefs[i]; - } - - return new TopologicalGenome(nrcs, Topology); - } - - public static void Spin(ref TopologicalGenome genome, ref double fitness, RandomSource rand, int T, Func<double, double> squash, int generations, double mutationMagnitude, double lambda) - { - TopologicalGenome g = genome; - double f = g.Judge(T, squash, lambda); - - for (int i = 0; i < generations; i++) - { - // generate and judge - TopologicalGenome g2 = g.Mutate(rand, mutationMagnitude); - double f2 = g2.Judge(T, squash, lambda); - - // select - if (f2 > f) - { - g = g2; - f = f2; - } - } - - genome = g; - fitness = f; - } - - public static void RunExperiment(string outDir, string filePrefix, string titlePrefix, RandomSource rand, TopologicalGenome initialGenome, int epochs, int generationsPerEpoch, int T, Func<double, double> squash, double mutationMagnitude, double lambda) - { - outDir += outDir + "_" + Misc.NowTime; - System.IO.Directory.CreateDirectory(outDir); - - string file(string name) - { - return System.IO.Path.Combine(outDir, filePrefix + name); - } - - TopologicalGenome genome = initialGenome; - double f = genome.Judge(T, squash, lambda); - - int N = genome.Size; - double[][] regulationCoefs = CreateEmpty<double>(N, epochs + 1); - - for (int epoch = 0; epoch <= epochs; epoch++) - { - if (epoch > 0) - { - Spin(ref genome, ref f, rand, T, squash, generationsPerEpoch, mutationMagnitude, lambda); - } - - // record - for (int i = 0; i < N; i++) - { - regulationCoefs[i][epoch] = genome.RegulationCoefs[i]; - } - } - - // plot - PlotLines(titlePrefix + "Regulation Coefs", file("rcs.pdf"), regulationCoefs, 0, 1, xTitle: "Epoch", yTitle: "Weight"); - } - } -} diff --git a/M4MCode/M4M_MkI/M4M.sln b/M4MCode/M4M_MkI/M4M.sln index 1c8feb86e78a6257a692bc5b458d01cf9c118404..1125730fb7c73c40d4b98a2ff0d423f4ada0dcfa 100644 --- a/M4MCode/M4M_MkI/M4M.sln +++ b/M4MCode/M4M_MkI/M4M.sln @@ -7,15 +7,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M4M", "M4M\M4M.csproj", "{B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.Model", "M4M.Model\M4M.Model.csproj", "{6A470149-D199-4B60-9F76-0F01FBD587E0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M4M.Old", "M4M.Old\M4M.Old.csproj", "{8B27E40D-9DB6-4179-A2BD-E9DE907509B0}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.New", "M4M.New\M4M.New.csproj", "{1E7D2BDD-30E8-48F8-9636-AF93F451EC38}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.CoreRunner", "M4M.CoreRunner\M4M.CoreRunner.csproj", "{9F765166-0377-446C-8A23-3D5E08DC51D5}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.Tests", "M4M.Tests\M4M.Tests.csproj", "{BBC537E5-82BC-4101-855D-B9732CC4257D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M4M.Benchmarks", "M4M.Benchmarks\M4M.Benchmarks.csproj", "{1755059D-E854-45CF-BD2B-86878B7ABBA0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "M4M.Benchmarks", "M4M.Benchmarks\M4M.Benchmarks.csproj", "{1755059D-E854-45CF-BD2B-86878B7ABBA0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -71,26 +69,6 @@ Global {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|Any CPU.Build.0 = Release|Any CPU {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|x64.ActiveCfg = Release|Any CPU {6A470149-D199-4B60-9F76-0F01FBD587E0}.Release4|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|x64.ActiveCfg = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Debug|x64.Build.0 = Debug|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release2|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release3|x64.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|Any CPU.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|Any CPU.Build.0 = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|x64.ActiveCfg = Release|Any CPU - {8B27E40D-9DB6-4179-A2BD-E9DE907509B0}.Release4|x64.Build.0 = Release|Any CPU {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E7D2BDD-30E8-48F8-9636-AF93F451EC38}.Debug|x64.ActiveCfg = Debug|Any CPU