private void WriteCSharpContinueCondition(ICSharpWriter writer, out string continueExpressionText) { ICSharpExpressionContext ExpressionContext = new CSharpExpressionContext(); ContinueCondition.WriteCSharp(writer, ExpressionContext, -1); if (ExpressionContext.CompleteDestinationNameList.Count > 1) { continueExpressionText = string.Empty; foreach (string DestinationName in ExpressionContext.CompleteDestinationNameList) { if (continueExpressionText.Length > 0) { continueExpressionText += " && "; } continueExpressionText += DestinationName; } } else { Debug.Assert(ExpressionContext.ReturnValue != null); continueExpressionText = ExpressionContext.ReturnValue; } }
public TIndividual Run(XFitnessFunction <TChromosome> fitnessFunction) { int generation = 0; var reproductionGroup = new List <TIndividual>(); var currentPopulationsChromosomes = PopulationInitializer.Initialize(); IReadOnlyList <TIndividual> currentPopulation = currentPopulationsChromosomes.Select( chromosome => IndividualFactory.CreateIndividual(chromosome, fitnessFunction) ).ToList(); var bestSolution = currentPopulation[0]; foreach (var individual in currentPopulation) { if (individual.Fitness > bestSolution.Fitness) { bestSolution = individual; } } while (ContinueCondition.ShouldContinue(currentPopulation, generation)) { var pairs = Breeding.Select(currentPopulation); reproductionGroup.Clear(); foreach (var Compound in pairs.Select(pair => Crossover.Crossover(pair))) { reproductionGroup.AddRange( Compound .Select(chromosome => Mutation.Mutate(chromosome)) .Select(mutant => IndividualFactory.CreateIndividual(mutant, fitnessFunction))); } foreach (var individual in reproductionGroup) { if (individual.Fitness > bestSolution.Fitness) { bestSolution = individual; } } currentPopulation = Strategy.NextGeneration( currentPopulation, reproductionGroup ); generation++; } return(bestSolution); }
/// <summary> /// Sets the <see cref="ICSharpOutputNode.WriteDown"/> flag. /// </summary> public override void SetWriteDown() { if (WriteDown) { return; } WriteDown = true; ContinueCondition.SetWriteDown(); foreach (ICSharpContinuation Continuation in ContinuationList) { Continuation.SetWriteDown(); } if (ElseInstructions != null) { ElseInstructions.SetWriteDown(); } }
public override bool SemanticAnalysis() { IsSemanticCorrect = true; if (BeginningActivity != null) { foreach (var Act in BeginningActivity) { try { IsSemanticCorrect &= Act.SemanticAnalysis(); } catch (SemanticException e) { Console.WriteLine(e.Message); IsSemanticCorrect = false; } } } if (IsPredCondition) { if (ContinueCondition != null) { try { IsSemanticCorrect &= ContinueCondition.SemanticAnalysis(); } catch (SemanticException e) { Console.WriteLine(e.Message); IsSemanticCorrect = false; } } } if (IterationActivity != null) { try { IsSemanticCorrect &= IterationActivity.SemanticAnalysis(); } catch (SemanticException e) { Console.WriteLine(e.Message); IsSemanticCorrect = false; } } foreach (var oper in ChildrenOperators) { try { IsSemanticCorrect &= oper.SemanticAnalysis(); } catch (SemanticException e) { Console.WriteLine(e.Message); IsSemanticCorrect = false; } } if (ContinueCondition != null) //ещё раз проверить условие, так как оно проверятся на каждой итерации { try { IsSemanticCorrect &= ContinueCondition.SemanticAnalysis(); } catch (SemanticException e) { Console.WriteLine(e.Message); IsSemanticCorrect = false; } } CheckVariables(); return(IsSemanticCorrect); }
public override void GenerateIntermediateCode() { int num_cycle = ++Counters.cycles; if (BeginningActivity != null) { foreach (var oper in BeginningActivity) { oper.GenerateIntermediateCode(); } } //ставим метку - сюда будем возвращаться после каждой итерации IntermediateCodeList.push(new PutLabel("again_cycle_" + num_cycle.ToString())); if (IsPredCondition) { ContinueCondition.GenerateIntermediateCode(); //то, что получилось, сравниваем с нулём if (ContinueCondition.MainVariable.Type == "int") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("int", "0", 0).MainVariable)); } else if (ContinueCondition.MainVariable.Type == "bool") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("bool", "false", 0).MainVariable)); } else { Console.WriteLine("Что-то с типами в цикле"); } //если ложь, то на выход IntermediateCodeList.push(new GoToLabel("exit_cycle_" + num_cycle.ToString(), "je")); } //генерим код для тела цикла foreach (var oper in ChildrenOperators) { oper.GenerateIntermediateCode(); } //если есть последующее действие - генерим код для него if (IterationActivity != null) { IterationActivity.GenerateIntermediateCode(); } //если цикл с постусловием, то генерить здесть if (!IsPredCondition) { ContinueCondition.GenerateIntermediateCode(); //то, что получилось, сравниваем с нулём if (ContinueCondition.MainVariable.Type == "int") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("int", "0", 0).MainVariable)); } else if (ContinueCondition.MainVariable.Type == "bool") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("bool", "false", 0).MainVariable)); } else { Console.WriteLine("Что-то с типами в цикле"); } //если ложь, то на выход IntermediateCodeList.push(new GoToLabel("exit_cycle_" + num_cycle.ToString(), "je")); } //переход на следующую итерацию IntermediateCodeList.push(new GoToLabel("again_cycle_" + num_cycle.ToString(), "jmp")); //последняя метка - на выход IntermediateCodeList.push(new PutLabel("exit_cycle_" + num_cycle.ToString())); }