Beispiel #1
0
        public static void ProcessNewAnswer(Node newAnswer, ConcurrentBag <string> existingEquationStrings, ref bool foundNonExistantAnswer, ConcurrentBag <PotentialAnswer> potentialAnswers, List <Operator> operators, List <Goal> goals)
        {
            string newEquationString1 = Node.Join(newAnswer);

            if (existingEquationStrings.Contains(newEquationString1) == false)
            {
                existingEquationStrings.Add(newEquationString1);
                foundNonExistantAnswer = true;
                PotentialAnswer newPotential = new PotentialAnswer();
                //newPotential.Equation = newAnswer;
                newPotential.EquationString = newEquationString1;
                newPotential.Equation       = Node.Parse(newPotential.EquationString, operators);
                if (newEquationString1.Contains("∞") == false && newEquationString1.Contains("NaN") == false)
                {
                    newPotential.Fitness  = Goal.CalculateGoalFitness(newAnswer, goals);
                    newPotential.Isolated = IsIsolated(newPotential.Equation);
                    potentialAnswers.Add(newPotential);
                }
            }
        }
Beispiel #2
0
        public static void ProcessUntransformedAnswer(PotentialAnswer potentialAnswer, ConcurrentBag <string> existingEquationStrings, ref bool foundNonExistantAnswer, ConcurrentBag <PotentialAnswer> potentialAnswers, List <Operator> operators, List <Goal> goals, List <Transform> inTransforms, ref bool stopSolving, bool useSubstitutions)
        {
            List <Node> newAnswers = TransformBranchFunctions.TransformBranchesWithTransforms(potentialAnswer.Equation, inTransforms, operators);

            potentialAnswer.Transformed = true;
            foreach (Node newAnswer1 in newAnswers)
            {
                Node newAnswer2 = EvaluateBranches.Evaluate(newAnswer1);
                ProcessNewAnswer(newAnswer1, existingEquationStrings, ref foundNonExistantAnswer, potentialAnswers, operators, goals);
                ProcessNewAnswer(newAnswer2, existingEquationStrings, ref foundNonExistantAnswer, potentialAnswers, operators, goals);
                if (stopSolving)
                {
                    break;
                }
            }
            if (useSubstitutions)
            {
                Substitute(potentialAnswer.Equation, potentialAnswers, existingEquationStrings, ref foundNonExistantAnswer, operators, goals, ref stopSolving);
            }
        }
Beispiel #3
0
        public Node Solve(List <Node> system, List <Transform> inTransforms, int maxRepetitions, int populationSize, List <Goal> goals, List <Operator> operators)
        {
            ConcurrentBag <PotentialAnswer> potentialAnswersBag    = new ConcurrentBag <PotentialAnswer>();
            List <PotentialAnswer>          potentialAnswersSorted = new List <PotentialAnswer>();

            stopSolving = false;
            List <Node> system2 = system.ToList();

            for (int i1 = 0; i1 < system.Count; i1++)
            {
                for (int i2 = i1 + 1; i2 < system.Count; i2++)
                {
                    system2.AddRange(Substitute(system[i1], system[i2], operators));
                }
            }
            foreach (Node solveMe in system2)
            {
                PotentialAnswer solveMeAnswer  = new PotentialAnswer();
                Node            reducedSolveMe = EvaluateBranches.Evaluate(solveMe);
                if (reducedSolveMe.DescendantsAndSelf().Count < solveMe.DescendantsAndSelf().Count)
                {
                    solveMeAnswer.Equation       = reducedSolveMe;
                    solveMeAnswer.EquationString = Node.Join(reducedSolveMe);
                    //solveMeAnswer.Equation = Node.Parse(solveMeAnswer.EquationString, operators);
                }
                else
                {
                    solveMeAnswer.Equation       = solveMe;
                    solveMeAnswer.EquationString = Node.Join(solveMe);
                    //solveMeAnswer.Equation = Node.Parse(solveMeAnswer.EquationString, operators);
                }
                solveMeAnswer.Fitness  = Goal.CalculateGoalFitness(solveMeAnswer.Equation, goals);
                solveMeAnswer.Isolated = IsIsolated(solveMeAnswer.Equation);
                potentialAnswersBag.Add(solveMeAnswer);

                //PotentialAnswer reparsed = new PotentialAnswer();
                //reparsed.Equation = Node.Parse(solveMeAnswer.EquationString, operators);
                //reparsed.EquationString = solveMeAnswer.EquationString;
                //solveMeAnswer.Fitness = Goal.CalculateGoalFitness(reparsed.Equation, goals);
                //solveMeAnswer.Isolated = IsIsolated(reparsed.Equation);
                //potentialAnswers.Add(reparsed);
            }
            potentialAnswersSorted = potentialAnswersBag.OrderByDescending(x => x.Fitness).ToList();
            bool                   foundNonExistantAnswer  = false;
            List <double>          bestFitnesses           = new List <double>();
            int                    totalBestFitnesses      = 5;
            ConcurrentBag <string> existingEquationStrings = new ConcurrentBag <string>();

            string[] existingEqs = potentialAnswersBag.Select(x => x.EquationString).ToArray();
            foreach (string eq in existingEqs)
            {
                existingEquationStrings.Add(eq);
            }

            for (int i = 0; i < maxRepetitions; i++)
            {
                List <PotentialAnswer> unTransformed = potentialAnswersSorted.Where(x => x.Transformed == false).ToList();
                foundNonExistantAnswer = false;
                //existingEquationStrings = potentialAnswers.Select(x => x.EquationString).ToList();
                if (UseParallel)
                {
                    cancellationTokenSource = new CancellationTokenSource();
                    ParallelOptions po = new ParallelOptions();
                    po.CancellationToken      = cancellationTokenSource.Token;
                    po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
                    try
                    {
                        Parallel.ForEach(unTransformed, po, x => ProcessUntransformedAnswer(x, existingEquationStrings, ref foundNonExistantAnswer, potentialAnswersBag, operators, goals, inTransforms, ref stopSolving, UseSubstitutions));
                    }
                    catch (OperationCanceledException e)
                    {
                        //Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        cancellationTokenSource.Dispose();
                    }
                    if (stopSolving)
                    {
                        return(potentialAnswersBag.OrderByDescending(x => x.Fitness).First().Equation);
                    }
                }
                else
                {
                    foreach (PotentialAnswer potentialAnswer in unTransformed)
                    {
                        ProcessUntransformedAnswer(potentialAnswer, existingEquationStrings, ref foundNonExistantAnswer, potentialAnswersBag, operators, goals, inTransforms, ref stopSolving, UseSubstitutions);
                        if (stopSolving)
                        {
                            return(potentialAnswersBag.OrderByDescending(x => x.Fitness).First().Equation);
                        }
                    }
                }
                if (foundNonExistantAnswer == false)
                {
                    break;
                }
                potentialAnswersSorted = potentialAnswersBag.GroupBy(o => o.EquationString).Select(g => g.First()).OrderByDescending(x => x.Fitness).Take(populationSize).ToList();
                bestFitnesses.Add(potentialAnswersSorted[0].Fitness);
                if (bestFitnesses.Count > totalBestFitnesses)
                {
                    bestFitnesses.RemoveAt(0);
                    double firstFitness = bestFitnesses.First();
                    if (bestFitnesses.All(x => x == firstFitness))
                    {
                        break;
                    }
                }
                if (FinishedRepetition != null)
                {
                    FinishedRepetition(potentialAnswersSorted);
                }
            }
            return(potentialAnswersSorted.First().Equation);
        }