public void GenerateNewAndEvaluate(IEquationFinderArgs equationArgs) { _result = null; EquationArgs = equationArgs; Equation = HelperClass.GenerateRandomEquation(EquationArgs); Solve(); }
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(); }
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()); }
public void GenerateNewAndEvaluate(IEquationFinderArgs args) { _result = null; EquationArgs = args; Equation = GenerateRandomEquation(); Solve(); }
public ExpressionBuilder(IEquationFinderArgs equationArgs) { if (equationArgs == null) { throw new ArgumentNullException("equationArgs"); } EquationArgs = new EquationFinderArgs(equationArgs.TargetValue, ResultPredicate.IsEqualTo, equationArgs.NumberOfOperations, equationArgs.TermPool, equationArgs.OperatorPool); }
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); }
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; }
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); }
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); } }
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; }
public AlgebraicString(IEquationFinderArgs equationArgs) { GenerateNewAndEvaluate(equationArgs); }
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); } } } }
public AlgebraicExpression(IEquationFinderArgs args) { GenerateNewAndEvaluate(args); }
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); } } } }