예제 #1
0
 private void OnAnswerFound(EF_Equation equation, EventArgs e)
 {
     if (AnswerFound != null)
     {
         AnswerFound(this, equation, e);
     }
 }
예제 #2
0
        public void MutateEquation(ref EF_Equation equation, dynamic maxMutation, dynamic minMutation)
        {
            //Maybe limit mutations here
            int mutations = RandomGenerator.Instance.Random.Next(1, equation.Operators.Count + equation.Variables.Count + 2);

            equation.Mutate(maxMutation, minMutation, mutations);
        }
예제 #3
0
        private bool foundAnswer(EF_Equation equation)
        {
            if (Math.Abs(Answer - EquationCalculator.Instance.ComputeEquation(equation)) <= acceptableCost)
            {
                return(true);
            }

            return(false);
        }
예제 #4
0
        private void initializePopulation()
        {
            for (int i = 0; i < populationSize; i++)
            {
                EF_Equation equation = EquationMaker.Instance.MakeEquation(inputs, type, maxValue, minValue);

                Equations.Add(equation);

                //Console.Out.WriteLine(equation.PrettyName);
            }

            this.PopulationCount = 1;
        }
예제 #5
0
        private void AnswerFound(object sender, EF_Equation equation, EventArgs e)
        {
            lockFunctions = false;

            button_Step.Enabled   = true;
            button_StepBy.Enabled = true;
            button_Stop.Enabled   = true;
            enableButtons();

            displayPopulationSettings();
            label_PopCounter.Text = "Answer found on " + solutionFinder.PopulationCount;
            Console.Out.WriteLine("Value = " + EquationCalculator.Instance.ComputeEquation(equation));

            textBox_Solution.Text = equation.PrettyName;
            newSolution           = true;
        }
예제 #6
0
        private int compareEquations(EF_Equation equationOne, EF_Equation equationTwo)
        {
            //smaller cost is best
            if (equationOne == null)
            {
                if (equationTwo == null) //they are equal
                {
                    return(0);
                }
                else //pointOne is null and pointTwo is not null. PointTwo is greater
                {
                    return(1);
                }
            }
            else
            {
                //If x is not null
                if (equationTwo == null) //y is null, x is greater
                {
                    return(-1);
                }
                else
                {
                    dynamic cost_one = cost(equationOne);
                    dynamic cost_two = cost(equationTwo);

                    if (cost_one > cost_two) // lower cost is better
                    {
                        return(1);           //pointTwo is better
                    }
                    else if (cost_two > cost_one)
                    {
                        return(-1); //pointOne is better
                    }
                    else // they are the same
                    {
                        return(0);
                    }
                }
            }
        }
예제 #7
0
        private dynamic cost(EF_Equation equation)
        {
            dynamic val  = EquationCalculator.Instance.ComputeEquation(equation);
            dynamic cost = 0;

            if (Math.Abs(val) > Math.Abs(Answer))
            {
                cost = (1 - val / Answer);
            }
            else
            {
                cost = (1 - Answer / val);
            }

            if (Math.Abs(val) < 1.0)
            {
                return(Math.Abs(cost / val));
            }
            else
            {
                return(Math.Abs(cost * val));
            }
        }
예제 #8
0
        private void performStep()
        {
            if (Equations.Count <= 0)
            {
                return;
            }

            Equations.Sort(compareEquations);
            if (foundAnswer(Equations[0]))
            {
                //answer found
                OnAnswerFound(Equations[0], null);
                return;
            }
            else
            {
                for (int i = Equations.Count / 2, parents = 0; i < Equations.Count; i += 2, parents += 2)
                {
                    //Console.Out.WriteLine("i = " + i + " parents = " + parents);

                    Tuple <EF_Equation, EF_Equation> newEquations = EquationMaker.Instance.MakeChildren(Equations[parents], Equations[parents + 1]);
                    Equations[i]     = newEquations.Item1;
                    Equations[i + 1] = newEquations.Item2;
                }

                Equations.Sort(compareEquations);


                //increment population count
                PopulationCount++;

                if (foundAnswer(Equations[0]))
                {
                    //answer found
                    OnAnswerFound(Equations[0], null);
                    return;
                }
                do
                {
                    int     index           = RandomGenerator.Instance.Random.Next(1, Equations.Count);
                    dynamic maxMutationRate = Math.Abs(RandomGenerator.Instance.Random.NextDouble() * mutationRate);
                    dynamic minMutationRate = maxMutationRate * -1;

                    EF_Equation mutateMe = Equations[index];
                    EquationMaker.Instance.MutateEquation(ref mutateMe, maxMutationRate, minMutationRate);
                    Equations[index] = mutateMe;
                } while (mutationRate-- > 0);

                Equations.Sort(compareEquations);
                //for (int i = 0; i < Equations.Count; i++)
                //    Console.Out.WriteLine("Cost = " + cost(Equations[i]) + " Value = " + EquationCalculator.Instance.ComputeEquation(Equations[i]));
                if (foundAnswer(Equations[0]))
                {
                    //answer found
                    OnAnswerFound(Equations[0], null);
                    return;
                }

                //Console.Out.WriteLine(" Best Computed Value = " + EquationCalculator.Instance.ComputeEquation(Equations[0]));
                //Console.Out.WriteLine("Best cost = " + cost(Equations[0]));
            }
        }
예제 #9
0
 private void initializePopulationWithSeed(EF_Equation seed)
 {
 }
예제 #10
0
 public SolutionFinder(Dictionary <char, dynamic> inputs, dynamic output, dynamic acceptableCost, dynamic mutationRate, int populationSize, EquationType type, dynamic maxValue, dynamic minValue, EF_Equation seed)
 {
     fillOut(inputs, output, acceptableCost, mutationRate, populationSize, type, maxValue, minValue);
     this.seed = seed;
     initializePopulationWithSeed(this.seed);
 }
예제 #11
0
        public dynamic ComputeEquation(EF_Equation equation)
        {
            //DataTable computeTable = new DataTable();
            //return  Convert.ToDouble(computeTable.Compute(equation.ToString(), null).ToString());

            if (equation.Variables.Count == 1)
            {
                return(equation.Variables[0].ComputedValue);
            }

            dynamic            val          = 0;
            List <dynamic>     variableList = new List <dynamic>();
            List <EF_Operator> operatorList = new List <EF_Operator>();

            for (int i = 0; i < equation.Variables.Count; i++)
            {
                variableList.Add(equation.Variables[i].ComputedValue);
            }
            for (int i = 0, x = 0; i < equation.Operators.Count; i++, x = x + 2)
            {
                switch (equation.Operators[i].Operator)
                {
                case EF_Operand.Add: operatorList.Add(equation.Operators[i]); break;

                case EF_Operand.Subtract: operatorList.Add(equation.Operators[i]); break;

                case EF_Operand.Multiply:
                    dynamic val_temp = 0;
                    val_temp        = computeVariables(variableList[x], variableList[x + 1], equation.Operators[i]);
                    variableList[x] = val_temp;
                    variableList.RemoveAt(x + 1);
                    x--;
                    break;

                case EF_Operand.Divide:
                    dynamic val_temp2 = 0;
                    val_temp2       = computeVariables(variableList[x], variableList[x + 1], equation.Operators[i]);
                    variableList[x] = val_temp2;
                    variableList.RemoveAt(x + 1);
                    x--;
                    break;

                default: break;
                }
            }

            for (int i = 0, x = 0; i < operatorList.Count; i++, x = x + 2)
            {
                switch (operatorList[i].Operator)
                {
                case EF_Operand.Add:
                    dynamic val_temp = 0;
                    val_temp        = computeVariables(variableList[x], variableList[x + 1], operatorList[i]);
                    variableList[x] = val_temp;
                    variableList.RemoveAt(x + 1);
                    x--;
                    break;

                case EF_Operand.Subtract:
                    dynamic val_temp2 = 0;
                    val_temp2       = computeVariables(variableList[x], variableList[x + 1], operatorList[i]);
                    variableList[x] = val_temp2;
                    variableList.RemoveAt(x + 1);
                    x--;
                    break;

                default: break;
                }
            }

            return(variableList[0]);
        }
예제 #12
0
        public Tuple <EF_Equation, EF_Equation> MakeChildren(EF_Equation parent1, EF_Equation parent2)
        {
            VariableType varType;

            if (parent1.EquationType == EquationType.Int)
            {
                varType = VariableType.Int;
            }
            else
            {
                varType = VariableType.Double;
            }

            EquationType parent1Copy_eq = new EquationType();
            EquationType parent2Copy_eq = new EquationType();

            if (parent1.EquationType.Equals(EquationType.Double))
            {
                parent1Copy_eq = EquationType.Double;
                parent2Copy_eq = EquationType.Double;
            }
            else
            {
                parent1Copy_eq = EquationType.Int;
                parent2Copy_eq = EquationType.Int;
            }

            List <EF_Variable> parent1Copy_variables = new List <EF_Variable>();
            List <EF_Variable> parent2Copy_variables = new List <EF_Variable>();

            foreach (EF_Variable variable in parent1.Variables)
            {
                char    symbol = variable.Symbol;
                dynamic value  = variable.Value;

                dynamic coeff = variable.CoEfficient;

                dynamic     power       = variable.Power;
                EF_Variable newVariable = new EF_Variable(symbol, value, coeff, power, varType);

                parent1Copy_variables.Add(newVariable);
            }

            foreach (EF_Variable variable in parent2.Variables)
            {
                char        symbol      = variable.Symbol;
                dynamic     value       = variable.Value;
                dynamic     coeff       = variable.CoEfficient;
                dynamic     power       = variable.Power;
                EF_Variable newVariable = new EF_Variable(symbol, value, coeff, power, varType);

                parent2Copy_variables.Add(newVariable);
            }

            List <EF_Operator> parent1Copy_operators = new List <EF_Operator>();
            List <EF_Operator> parent2Copy_operators = new List <EF_Operator>();

            foreach (EF_Operator oper in parent1.Operators)
            {
                EF_Operand op = new EF_Operand();
                if (oper.Operator == EF_Operand.Add)
                {
                    op = EF_Operand.Add;
                }
                else if (oper.Operator == EF_Operand.Subtract)
                {
                    op = EF_Operand.Subtract;
                }
                else if (oper.Operator == EF_Operand.Multiply)
                {
                    op = EF_Operand.Multiply;
                }
                else
                {
                    op = EF_Operand.Divide;
                }

                EF_Operator opera = new EF_Operator(op);
                parent1Copy_operators.Add(oper);
            }

            foreach (EF_Operator oper in parent2.Operators)
            {
                EF_Operand op = new EF_Operand();
                if (oper.Operator == EF_Operand.Add)
                {
                    op = EF_Operand.Add;
                }
                else if (oper.Operator == EF_Operand.Subtract)
                {
                    op = EF_Operand.Subtract;
                }
                else if (oper.Operator == EF_Operand.Multiply)
                {
                    op = EF_Operand.Multiply;
                }
                else
                {
                    op = EF_Operand.Divide;
                }

                EF_Operator opera = new EF_Operator(op);
                parent2Copy_operators.Add(oper);
            }

            EF_Equation child1 = new EF_Equation(parent1Copy_eq, parent2Copy_variables, parent1Copy_operators);
            EF_Equation child2 = new EF_Equation(parent2Copy_eq, parent1Copy_variables, parent2Copy_operators);

            return(Tuple.Create(child1, child2));
        }