コード例 #1
0
 public void GenerateNewAndEvaluate(IEquationFinderArgs equationArgs)
 {
     _result      = null;
     EquationArgs = equationArgs;
     Equation     = HelperClass.GenerateRandomEquation(EquationArgs);
     Solve();
 }
コード例 #2
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            _result      = null;
            _isSolution  = false;
            EquationArgs = args;

            IncreasingOperations = new List <TupleOperation>();
            DecreasingOperations = new List <TupleOperation>();

            foreach (char op in EquationArgs.OperatorPool)
            {
                TupleOperation tupleOperation = new TupleOperation(op);

                switch (tupleOperation.Operand)
                {
                case OperationType.Add:
                case OperationType.Multiply:
                case OperationType.Exponentiation:
                    IncreasingOperations.Add(tupleOperation);
                    break;

                case OperationType.Subtract:
                case OperationType.Divide:
                    DecreasingOperations.Add(tupleOperation);
                    break;
                }
            }

            BuildEquation();
        }
コード例 #3
0
ファイル: HelperClass.cs プロジェクト: T5C2U7B/EquationFinder
        public static string GenerateRandomEquation(IEquationFinderArgs EquationArgs)
        {
            List <string> operators = new List <string>(EquationArgs.NumberOfOperations);
            List <string> terms     = new List <string>(EquationArgs.NumberOfOperations);

            int termCount = EquationArgs.TermPool.Count;
            int opCount   = EquationArgs.OperatorPool.Length;

            int counter = EquationArgs.NumberOfOperations - 1;

            while (counter-- > 0)
            {
                operators.Add(EquationArgs.OperatorPool.ElementAt(EquationArgs.Rand.Next(0, opCount)).ToString());
            }

            counter = EquationArgs.NumberOfOperations;
            while (counter-- > 0)
            {
                terms.Add(EquationArgs.TermPool.ElementAt(EquationArgs.Rand.Next(0, termCount)).ToString());
            }

            counter = 0;

            StringBuilder stringBuilder = new StringBuilder(terms[counter++]);

            foreach (string op in operators)
            {
                stringBuilder.AppendFormat(" {0} {1}", op, terms[counter++]);
            }

            return(stringBuilder.ToString());
        }
コード例 #4
0
 public void GenerateNewAndEvaluate(IEquationFinderArgs args)
 {
     _result      = null;
     EquationArgs = args;
     Equation     = GenerateRandomEquation();
     Solve();
 }
コード例 #5
0
 public ExpressionBuilder(IEquationFinderArgs equationArgs)
 {
     if (equationArgs == null)
     {
         throw new ArgumentNullException("equationArgs");
     }
     EquationArgs = new EquationFinderArgs(equationArgs.TargetValue, ResultPredicate.IsEqualTo, equationArgs.NumberOfOperations, equationArgs.TermPool, equationArgs.OperatorPool);
 }
コード例 #6
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (_equationArgs == null)
            {
                _equationArgs = args;
            }
            _builder = new ExpressionBuilder<decimal>(args);

            IsSolution = (Result == _equationArgs.TargetValue);
        }
コード例 #7
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            _result      = null;
            EquationArgs = args;
            int counter = EquationArgs.NumberOfOperations;

            Expression expression = Expression.Empty();
            Func <Expression, Expression, Expression> operation = NextOperation();
            Expression lhs = AlgebraicBuilder.Constant(GenerateTerm);
            Expression rhs = AlgebraicBuilder.Constant(GenerateTerm);

            while (_lastOperation == '/' && _lastTerm == 0)
            {
                operation = NextOperation();
                rhs       = AlgebraicBuilder.Constant(GenerateTerm);
            }
            expression = operation(lhs, rhs);
            counter   -= 2;

            while (counter-- > 0)
            {
                if (EquationArgs.Rand.Next(0, 2) == 1)
                {
                    operation = NextOperation();
                    rhs       = AlgebraicBuilder.Constant(GenerateTerm);

                    while (_lastOperation == '/' && _lastTerm == 0)
                    {
                        operation = NextOperation();
                        rhs       = AlgebraicBuilder.Constant(GenerateTerm);
                    }
                    expression = operation(expression, rhs);
                }
                else
                {
                    operation = NextOperation();
                    lhs       = AlgebraicBuilder.Constant(GenerateTerm);

                    while (_lastOperation == '/')
                    {
                        operation = NextOperation();
                    }
                    expression = operation(lhs, expression);
                }
            }
            Equation = expression;
            Solve();
            BigInteger result = Result;
            bool       solved = IsSolution;
        }
コード例 #8
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (_equationArgs == null)
            {
                _equationArgs = args;
            }
            _builder = new ExpressionBuilder <decimal>(args);


            IsSolution = (Result == _equationArgs.TargetValue);
        }
コード例 #9
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (_equationArgs == null)
            {
                _equationArgs = args;
            }

            _builder = new ExpressionBuilder <BigInteger>(args);


            if (_equationArgs.TargetValuePredicate == ResultPredicate.IsDivisibleBy)
            {
                IsSolution = ((Result != 0) && (Result % _equationArgs.TargetValue == 0));
            }
            else if (_equationArgs.TargetValuePredicate == ResultPredicate.IsEqualTo)
            {
                IsSolution = (Result == _equationArgs.TargetValue);
            }
        }
コード例 #10
0
        public ThreadSpawnerArgs(FoundEquationDelegate foundSolutionCallbackFunction, int timeToLive, int numberOfThreads, int numberOfRounds, IEquationFinderArgs finderArgs)
            : this()
        {
            if (finderArgs == null)
            {
                throw new ArgumentNullException("FinderArgs cannot be null.", "finderArgs");
            }
            if (timeToLive <= 0)
            {
                throw new ArgumentException("TimeToLive must be greater than zero.", "timeToLive");
            }
            if (numberOfThreads < 1)
            {
                throw new ArgumentException("NumberOfThreads must be at least one.", "numberOfThreads");
            }
            if (numberOfRounds < 1)
            {
                throw new ArgumentException("NumberOfRounds must be at least one.", "numberOfRounds");
            }

            if (foundSolutionCallbackFunction == null)
            {
                FoundResultCallback = null;
            }
            else
            {
                FoundResultCallback = foundSolutionCallbackFunction;
            }

            // Thread settings
            TimeToLive      = timeToLive;
            NumberOfThreads = numberOfThreads;
            NumberOfRounds  = numberOfRounds;

            // Equation settings
            EquationFinderArgs = finderArgs;
        }
コード例 #11
0
 public AlgebraicString(IEquationFinderArgs equationArgs)
 {
     GenerateNewAndEvaluate(equationArgs);
 }
コード例 #12
0
        public ThreadSpawnerArgs(FoundEquationDelegate foundSolutionCallbackFunction, int timeToLive, int numberOfThreads, int numberOfRounds, IEquationFinderArgs finderArgs)
            : this()
        {
            if (finderArgs == null)
            {
                throw new ArgumentNullException("FinderArgs cannot be null.", "finderArgs");
            }
            if (timeToLive <= 0)
            {
                throw new ArgumentException("TimeToLive must be greater than zero.", "timeToLive");
            }
            if (numberOfThreads < 1)
            {
                throw new ArgumentException("NumberOfThreads must be at least one.", "numberOfThreads");
            }
            if (numberOfRounds < 1)
            {
                throw new ArgumentException("NumberOfRounds must be at least one.", "numberOfRounds");
            }

            if (foundSolutionCallbackFunction == null)
            {
                FoundResultCallback = null;
            }
            else
            {
                FoundResultCallback = foundSolutionCallbackFunction;
            }

            // Thread settings
            TimeToLive = timeToLive;
            NumberOfThreads = numberOfThreads;
            NumberOfRounds = numberOfRounds;

            // Equation settings
            EquationFinderArgs = finderArgs;
        }
コード例 #13
0
 public void GenerateNewAndEvaluate(IEquationFinderArgs equationArgs)
 {
     _result = null;
     EquationArgs = equationArgs;
     Equation = HelperClass.GenerateRandomEquation(EquationArgs);
     Solve();
 }
コード例 #14
0
 public AlgebraicString(IEquationFinderArgs equationArgs)
 {
     GenerateNewAndEvaluate(equationArgs);
 }
コード例 #15
0
 public ThreadSpawnerArgs(List<string> previouslyFoundSolutons, FoundEquationDelegate displayOutputFunction, int timeToLive, int numberOfThreads, int numberOfRounds, IEquationFinderArgs finderArgs)
     : this(displayOutputFunction, timeToLive, numberOfThreads, numberOfRounds, finderArgs)
 {
     if (previouslyFoundSolutons != null && previouslyFoundSolutons.Count > 0)
     {
         foreach (string prevSolution in previouslyFoundSolutons)
         {
             if (!string.IsNullOrWhiteSpace(prevSolution))
             {
                 FoundSolutions.Add(prevSolution);
             }
         }
     }
 }
コード例 #16
0
 public AlgebraicExpression(IEquationFinderArgs args)
 {
     GenerateNewAndEvaluate(args);
 }
コード例 #17
0
 public AlgebraicExpression(IEquationFinderArgs args)
 {
     GenerateNewAndEvaluate(args);
 }
コード例 #18
0
 public ThreadSpawnerArgs(List <string> previouslyFoundSolutons, FoundEquationDelegate displayOutputFunction, int timeToLive, int numberOfThreads, int numberOfRounds, IEquationFinderArgs finderArgs)
     : this(displayOutputFunction, timeToLive, numberOfThreads, numberOfRounds, finderArgs)
 {
     if (previouslyFoundSolutons != null && previouslyFoundSolutons.Count > 0)
     {
         foreach (string prevSolution in previouslyFoundSolutons)
         {
             if (!string.IsNullOrWhiteSpace(prevSolution))
             {
                 FoundSolutions.Add(prevSolution);
             }
         }
     }
 }
コード例 #19
0
 public void GenerateNewAndEvaluate(IEquationFinderArgs args)
 {
     _result = null;
     EquationArgs = args;
     Equation = GenerateRandomEquation();
     Solve();
 }