public LocalityTest(GrammaticalEvolution ge1, GrammaticalEvolution ge2, FitnessCalculator calculator, ExecutionParameters parameters) { GE1 = ge1; GE2 = ge2; Calculator = calculator; Parameters = parameters; }
public GeneticAlgorithm(int elitismSize, GrammaticalEvolution ge, FitnessCalculator fitnessCalculator, Selection selection, Crossover crossover, Mutation mutation, Population population, ExecutionParameters parameters) { ElitismSize = elitismSize; GE = ge; _FitnessCalculator = fitnessCalculator; Selection = selection; Crossover = crossover; Mutation = mutation; Population = population; Parameters = parameters; }
public object Execute(ExecutionParameters parameters, string code) { CSharpCodeProvider c = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.AddRange(parameters.AssemblyPaths); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; CompilerResults cr = c.CompileAssemblyFromSource(cp, code); if (cr.Errors.Count > 0) { throw new Executor.CompilationErrorException(cr.Errors, code); } Assembly a = cr.CompiledAssembly; object codeObject = a.CreateInstance(parameters.MainObjectType); Type t = codeObject.GetType(); MethodInfo mi = t.GetMethod(parameters.MethodName); object s; var task = Task.Run(() => s = mi.Invoke(codeObject, parameters.MethodParameters)); bool successfulCompilation; try { successfulCompilation = task.Wait(TimeSpan.FromMilliseconds(Executor.MaxExecutionTimeOut)); } catch (Exception e) { throw new Executor.ExecutionExceptionException(e, code); } if (successfulCompilation) { return(task.Result); } else { if (task.Status == TaskStatus.Faulted) { throw task.Exception; } else { throw new Executor.ExecutionExceptionException(new Executor.ExecutionTimeOutException(Executor.MaxExecutionTimeOut), code); } } }
public static object Execute(ExecutionParameters parameters, string code) { string newCode = parameters.BluePrintCode.Replace("PECLECODE", code); executions++; if (executions > maxExecutions) { environment.Dispose(); environment = new IsolatedEnvironment(); executions = 0; } return(environment.Value.Execute(parameters, newCode)); }
private static ProductionRule CheckIfAlwaysTrue(OneMaxGO go) { string code = go.ConvertToCode((NonTerminal)"condition"); bool canExecute = !code.Contains("a") && !code.Contains("b") && !code.Contains("c") && !code.Contains("d") && !code.Contains("e") && !code.Contains("f"); ExecutionParameters parameters = new ExecutionParameters(genericCodeWrapper.Replace("TYPE", "bool"), "CodeExecutor.Executor", "Execute"); if (canExecute) { bool result = (bool)Executor.Execute(parameters, "return !" + code + ";"); go.ConvertToCode(new ErrorCheck(g => result)); return(new ProductionRule(new Terminal(code))); //If the value is always false, there can't be an infine loop (although a loop that is never executed is useless) } else//If the code contains variables, by definition its value isn't constant { return(new ProductionRule(new Terminal(code))); } }