protected FunctionPairBase(IGEPGeneticCode geneticCode, IFunctionIdentifier primaryFunction, 
            IFunctionIdentifier parameterlessFunction)
        {
            this.PrimaryFunction = primaryFunction;
            this.ParameterlessFunction = parameterlessFunction;

            if (this.PrimaryFunction == null || this.ParameterlessFunction == null)
                throw new GeneticCodeException("Both the Primary Function and the Parameterless Function must be provided.");

            if (!geneticCode.FunctionSet[this.ParameterlessFunction].IsParameterless)
                throw new GeneticCodeException("The given Parameterless Function must actually be parameterless.");
        }
        /// <summary>
        /// Gets the IDebuggableAminoAcid as a string.
        /// This is the function, its evaluated arguments and its resulting variable name.
        /// </summary>
        /// <param name="geneticCode">The IGeneticCode to which the IDebuggableAminoAcid adheres.</param>
        /// <param name="printIVariablesAsValues">If true, all argument IVariable names will be replaced
        /// by their corresponding value. If false, the argument IVariable names will be printed.</param>
        /// <returns></returns>
        public string GetDebuggableAminoAcidString(IGEPGeneticCode geneticCode, bool printIVariablesAsValues)
        {
            var function = geneticCode.FunctionSet[this.AminoAcid.Function];

            var stepString = "var " + this.ResultingVariable.Name + " = " + function.Name + "(";

            for (var i = 0; i < this.AminoAcid.Count; i++)
            {
                var parameter = function[i];

                if(parameter.ParameterType == ParameterType.AminoAcid)
                {
                    stepString += "AminoAcid: " + geneticCode.FunctionSet[this.AminoAcid[i].Value.AminoAcid.Function].Name;
                }
                else if (parameter.ParameterType == ParameterType.EvaluatedAminoAcid)
                {
                    if (printIVariablesAsValues)
                        stepString += this.AminoAcid[i].Value.EvaluatedAminoAcid.Value;
                    else
                        stepString += this.Arguments[i].ResultingVariable.Name;
                }
                else if (parameter.ParameterType == ParameterType.ConvertedCodon)
                {
                    stepString += this.AminoAcid[i].Value.ConvertedCodon.Value;
                }
                else
                {
                    throw new ValueNotYetSupported(parameter.ParameterType, typeof (ParameterType));
                }

                if (i + 1 < this.AminoAcid.Count)
                    stepString += ", ";
            }
            stepString += ");";

            stepString += " == " + this.Result.Value;
            return stepString;
        }
Exemplo n.º 3
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geneticCode">The IGEPGeneticCode to which all IGEPOrganisms in this IGEPUniverse adhere.</param>
 public GEPUniverse(IGEPGeneticCode geneticCode) 
     : base(geneticCode)
 {
 }
Exemplo n.º 4
0
        public Form1()
        {
            InitializeComponent();

            /*
            var sqlConnectionDetails = new SQLConnectionDetails(DatabaseType.MSSQL, System.Configuration.ConfigurationManager.ConnectionStrings["Production"].ConnectionString);
            SM.Initialize(sqlConnectionDetails.Server, recordAllSQLTransactions: false);
            DM.Initialize(new SqlConnection(sqlConnectionDetails.ConnectionString).Database, sqlConnectionDetails, new SessionManager());

             */
            var environmentState = new EnvironmentState(populationSize: 500, eliteCount: 3)
                                       {
                                           ChanceOfInitialization = .1,
                                           ChanceOfRecombination = 0,
                                           ChanceOfMutation = .9,
                                       };

            var environment = new GEPEnvironment(identifier: new NameIdentifier("Forest"),
                                                 state: environmentState,
                                                 metricsEvaluator:
                                                     new MetricsEvaluator(MetricsFunction, (one, two) => 1,
                                                                          (one, two) => 1, (one, two) => 1,
                                                                          (one, two) => 1),
                                                 endCriteriaEvaluator:
                                                     new EndAfterGenerationsEvaluator(generationAtWhichToEnd: 1000),
                                                     /*new EndAfterValueMetEvaluator(40000), */
                                                organismValueComparer: new CompareByValueLowerWins(),
                                                     callableGenesProvider: new CallableGenesProviderFirstLevelChildren());


            environmentState.InitializationOperators.Add(new GEPRandomOneGeneInitializationOperator(100));
            
            environmentState.SelectionOperators.Add(new RandomSelection(5));
            environmentState.SelectionOperators.Add(new TournamentSelection(25));
            environmentState.SelectionOperators.Add(new RouletteSelection(70));

            environmentState.MutationOperators.Add(new GEPMainGeneOneNucleotide(30));
            environmentState.MutationOperators.Add(new GEPMainGeneIncrementNucleotide(25));
            environmentState.MutationOperators.Add(new GEPMainGeneDecrementNucleotide(25));
            environmentState.MutationOperators.Add(new GEPMainGeneRandomizeNucleotides(20, minimumNucleotidesToMutate: 2, maximumNucleotidesToMutate: 4));




            var geneticCode = new GEPGeneticCodeUsingDatabase(codonLength: 6, maxParameterfulFunctionsInDNA: 20);

            geneticCode.AddNucleotide("A");
            geneticCode.AddNucleotide("B");
            geneticCode.AddNucleotide("C");
            geneticCode.AddNucleotide("D");
            geneticCode.AddNucleotide("E");
            geneticCode.AddNucleotide("F");
            geneticCode.AddNucleotide("G");
            geneticCode.AddNucleotide("H");

            geneticCode.AddCodonConverter(new ConvertCodonToInt3());
            geneticCode.AddCodonConverter(new ConvertCodonToInt5());
            geneticCode.AddCodonConverter(new ConvertCodonToInt16());
            geneticCode.AddCodonConverter(new ConvertCodonToFloat16());
            geneticCode.AddCodonConverter(new ConvertCodonToDecimal());


            var executeMethods = new List<MethodInfo>();

            var nestedTypes = typeof (GeneticFaraday.GEP.Functions.Functions).GetNestedTypes();
            foreach(var type in nestedTypes)
                executeMethods.AddRange(type.GetMethods());

            /*
            var functionRowTemplate = new TheFunctionRow();
            var functionRows = Qry.SelectAllFrom(functionRowTemplate)
                .LeftOuterJoinSelectingAllOn(functionRowTemplate.ForeignKeyRow_Parameter_ParentFunctionRowColumn)
                .LeftOuterJoinSelectingAllOn(functionRowTemplate.ForeignKeyRow_FunctionRegistry_TheFunctionRowColumn)
                .GoAndExtractMultiple<TheFunctionRow>();

            for (var i = 0; i < functionRows.Count; i++)
            {
                var functionRow = functionRows[i];
                var applicableRows = new List<TheFunctionRow>();
                while (i + 1 < functionRows.Count && functionRows[i + 1].PrimaryKey == functionRow.PrimaryKey)
                {
                    applicableRows.Add(functionRows[i + 1]);
                    i++;
                }

                var uniqueParameters = applicableRows.Where(r => !r.ForeignKeyRow_Parameter_ParentFunctionRow.IsPrimaryKeyNullOrEmpty)
                    .Select(r => r.ForeignKeyRow_Parameter_ParentFunctionRow)
                    .Distinct(r => r.PrimaryKey);

                var uniqueRegistryIdentifiers = applicableRows.Where(r => !r.ForeignKeyRow_FunctionRegistry_TheFunctionRow.IsPrimaryKeyNullOrEmpty)
                    .Select(r => r.ForeignKeyRow_FunctionRegistry_TheFunctionRow)
                    .Distinct(r => r.PrimaryKey);

                geneticCode.AddFunction(functionRow.ToFunction(uniqueParameters, uniqueRegistryIdentifiers, executeMethods));
            }
            */

            var universe = new GEPUniverse(geneticCode: geneticCode);

            universe.Add(environment);
            environment.GenerationEnd += environment_GenerationEnd;
            universe.AnyEnvironmentEnds += universe_AnyEnvironmentEnds;







            /*

            geneticCode.AddFunctions(Functions.Arithmetic.GetSumFunctions(environment: environment, weightInGeneticCode: 3));

            geneticCode.AddFunctions(Functions.IRegistryRead.GetReadFunctions(environment: environment, weightInGeneticCode: 4));
            geneticCode.AddFunctions(Functions.IRegistryWrite.GetWriteFunctions(environment: environment, weightInGeneticCode: 2));
            
            
            geneticCode.AddFunctions(Functions.Arithmetic.GetDifferenceFunctions(environment: environment, weightInGeneticCode: .5));
            geneticCode.AddFunctions(Functions.Arithmetic.GetMultiplyFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetDivideFunctions(environment: environment, weightInGeneticCode: .25));
            
            geneticCode.AddFunctions(Functions.Arithmetic.GetSquareFunctions(environment: environment, weightInGeneticCode: 3));
            geneticCode.AddFunctions(Functions.Arithmetic.GetPowerFunctions(environment: environment, weightInGeneticCode: 3));

            
            geneticCode.AddFunctions(Functions.Arithmetic.GetSquareRootFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetNegateFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetForceNegateFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetAbsoluteFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetRoundToNearestWholeNumberFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetSuccessorFunctions(environment: environment, weightInGeneticCode: .25));
            geneticCode.AddFunctions(Functions.Arithmetic.GetPredecessorFunctions(environment: environment, weightInGeneticCode: .25));
            
            geneticCode.AddFunctions(Functions.Constants.GetConstantsFunctions(weightInGeneticCode: 3));

            geneticCode.AutoAssignCodonsToAminoAcids(percentageStopCodons: .01, redundancyRating: 100);
             */
            this.GeneticCode = geneticCode;




            var distinctFunctionsByFunctionName = this.GeneticCode.FunctionSet.Values.Distinct(pair => pair.Name);
            foreach (var function in distinctFunctionsByFunctionName)
                function_names_combo_box.Items.Add(function.Name);
            function_names_combo_box.SelectedIndexChanged += function_names_combo_box_SelectedIndexChanged;

            var distinctFunctionsByFunctionBaseName = this.GeneticCode.FunctionSet.Values.Distinct(pair => pair.BaseName);
            foreach (var function in distinctFunctionsByFunctionBaseName)
                function_base_name_combo_box.Items.Add(function.BaseName);

            var distinctFunctionIdentifiers = this.GeneticCode.FunctionSet.Values.Select(p => p.Identifier);
            foreach (var function in distinctFunctionIdentifiers)
                function_identifiers_combo_box.Items.Add(function);


            this.Universe = universe;
            universe.Run();
        }
Exemplo n.º 5
0
 public FunctionPair(IGEPGeneticCode geneticCode, IFunctionIdentifier primaryFunction, 
     IFunctionIdentifier parameterlessFunction) 
     : base(geneticCode, primaryFunction, parameterlessFunction)
 {
 }
Exemplo n.º 6
0
 protected GEPCodonBase(IGEPGeneticCode geneticCode, ICodonIdentifier codonIdentifier, 
     CodonType codonType, IEnumerable<INucleotide> nucleotides, IFunctionPair functions) 
     : base(geneticCode, codonIdentifier, codonType, nucleotides)
 {
     this.Functions = functions;
 }