/// <summary> /// Initializes a new instance of <see cref="ObjectPoolAssistant{T, TSpecs}"/> thats wraps a create function and evaluate function. /// </summary> /// <param name="create">The create function of a T item.</param> /// <param name="evaluate">The evaluate function of a T item.</param> /// <param name="clearCount">The number of elements counted for <see cref="IsClear"/> returns <see langword="false"/>.</param> public ObjectPoolAssistant(CreateFunction create, EvaluateFunction evaluate, uint clearCount = 32) { _create = create ?? throw new ArgumentNullException(nameof(create)); _evaluate = evaluate; _clearCount = clearCount; _count = _clearCount; }
public ScriptQualifiedName(AstNodeArgs args) : base(args) { IsVariable = false; NextFirst = false; IsVar = false; if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0) { _identifier = ExtractId(((TokenAst)ChildNodes[0]).Text); IsVariable = true; _evaluation = EvaluateIdentifier; _assignment = AssignIdentifier; } else if (ChildNodes[0] is TokenAst && ChildNodes[1].ChildNodes.Count != 0) { _identifier = ExtractId(((TokenAst)ChildNodes[0]).Text); //NOTE: There might be two cases: // 1) a()[]...() // 2) a<>.(NamePart) _modifiers = new List <ScriptAst>(); foreach (ScriptAst node in ChildNodes[1].ChildNodes) { _modifiers.Add(node); } var generic = _modifiers.FirstOrDefault() as ScriptGenericsPostfix; if (generic != null && _modifiers.Count == 1) { //Case 2 _evaluation = EvaluateGenericType; _assignment = null; } else { //Case 1 _evaluation = EvaluateFunctionCall; _assignment = AssignArray; } } else { _namePart = ChildNodes[0] as ScriptQualifiedName; _identifier = ExtractId(((TokenAst)ChildNodes[2]).Text); NextFirst = true; if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count != 0) { _modifiers = new List <ScriptAst>(); foreach (ScriptAst node in ChildNodes[3].ChildNodes) { _modifiers.Add(node); } } _evaluation = EvaluateNamePart; _assignment = AssignNamePart; } }
public ScriptQualifiedName(AstNodeArgs args) : base(args) { if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0) { Identifier = ((Token)ChildNodes[0]).Text; evaluation = EvaluateIdentifier; assignment = AssignIdentifier; } else if (ChildNodes[0] is Token && ChildNodes[1].ChildNodes.Count != 0) { Identifier = (ChildNodes[0] as Token).Text; //NOTE: There might be two cases: // 1) a()[]...() // 2) a<>.(NamePart) Modifiers = new List <ScriptAst>(); foreach (ScriptAst node in ChildNodes[1].ChildNodes) { Modifiers.Add(node); } ScriptGenericsPostfix generic = Modifiers.FirstOrDefault() as ScriptGenericsPostfix; if (generic != null && Modifiers.Count == 1) { //Case 2 evaluation = EvaluateGenericType; assignment = null; } else { //Case 1 evaluation = EvaluateFunctionCall; assignment = AssignArray; } } else { NamePart = ChildNodes[0] as ScriptQualifiedName; Identifier = ((Token)ChildNodes[2]).Text; if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count != 0) { Modifiers = new List <ScriptAst>(); foreach (ScriptAst node in ChildNodes[3].ChildNodes) { Modifiers.Add(node); } } evaluation = EvaluateNamePart; assignment = AssignNamePart; } }
public ScriptQualifiedName(AstNodeArgs args) : base(args) { if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0) { Identifier = ((Token)ChildNodes[0]).Text; evaluation = EvaluateIdentifier; assignment = AssignIdentifier; } else if (ChildNodes[0] is Token && ChildNodes[1].ChildNodes.Count != 0) { Identifier = (ChildNodes[0] as Token).Text; //NOTE: There might be two cases: // 1) a()[]...() // 2) a<>.(NamePart) Modifiers = new List<ScriptAst>(); foreach (ScriptAst node in ChildNodes[1].ChildNodes) Modifiers.Add(node); ScriptGenericsPostfix generic = Modifiers.FirstOrDefault() as ScriptGenericsPostfix; if (generic != null && Modifiers.Count == 1) { //Case 2 evaluation = EvaluateGenericType; assignment = null; } else { //Case 1 evaluation = EvaluateFunctionCall; assignment = AssignArray; } } else { NamePart = ChildNodes[0] as ScriptQualifiedName; Identifier = ((Token)ChildNodes[2]).Text; if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count != 0) { Modifiers = new List<ScriptAst>(); foreach (ScriptAst node in ChildNodes[3].ChildNodes) { Modifiers.Add(node); } } evaluation = EvaluateNamePart; assignment = AssignNamePart; } }
/// <summary> /// Constructs a NEAT client with the given pedigree, number of organisms, evaluation function, and every constant. /// </summary> /// <param name="pedigree">The pedigree used to track all genes in this NEAT client.</param> /// <param name="numOrganisms">The total number of organisms this NEAT client will train.</param> /// <param name="evaluateFunction">The function to evaluate organisms' neural networks and give them a fitness score.</param> /// <param name="compatibility_distance">The initial compatibility distance.</param> /// <param name="CD_function">The function to adjust the compatibility distance by. Good options are the <see cref="NEAT.NEATClient.NERO_CD_Function(int)"/> and the /// <see cref="NEAT.NEATClient.Constant_CD_Function(int)"/></param> /// <param name="surviving_percentage">The percentage of organisms that will survive on <see cref="NEAT.NEATClient.Kill"/>.</param> public NEATClient(Pedigree pedigree, int numOrganisms, EvaluateFunction evaluateFunction, double compatibility_distance, CompatibilityDistanceFunction CD_function, double surviving_percentage) { #region Internal Setters Pedigree = pedigree; NumOrganisms = numOrganisms; Organisms = new List <Organism>(numOrganisms); Species = new HashSet <Species>(); this.evaluateFunction = evaluateFunction; CompatibilityDistance = compatibility_distance; this.CD_function = CD_function; SurvivingPercentage = surviving_percentage; #endregion Internal Setters #region Initial Conditions //Create organisms: for (int i = 0; i < NumOrganisms; ++i) { Organisms.Add(new Organism(Pedigree.CreateGenome())); } //Create Species: Species first_species = new Species(); Species.Add(first_species); first_species.AddOrganism(Organisms[0]); //Prepares first species to house every organism. Speciate(); #endregion Initial Conditions }
public static int ComputerVSComputer(EvaluateFunction fun1, EvaluateFunction fun2) { // 经过指定步数则为和棋 int step = Settings.DRAW_STEPS; GameState gs = new GameState(); ChessPiece winner = ChessPiece.EMPTY; while (step-- != 0) { winner = gs.WhoIsWin(); if (winner == ChessPiece.EMPTY) { if (step == Settings.DRAW_STEPS - 1) { gs.PlaceChessInCenter(); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } else if (step % 2 == 1) { PlaceChessAI(gs, fun1); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } else if (step % 2 == 0) { PlaceChessAI(gs, fun2); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } } else if (winner == ChessPiece.CROSS) { return(Settings.WIN_FITNESS); } else if (winner == ChessPiece.NOUGHT) { return(Settings.LOSE_FITNESS); } } return(Settings.DRAW_FITNESS); }
//-------------------------------------------------------------------------------------------------------------------------------- // Main Function to use static public bool EvaluateExpression(string expression, EvaluateFunction evalFunction) { //Remove white spaces and double && || string cleanExpr = ""; for (int i = 0; i < expression.Length; i++) { switch (expression[i]) { case ' ': break; case '&': cleanExpr += expression[i]; i++; break; case '|': cleanExpr += expression[i]; i++; break; default: cleanExpr += expression[i]; break; } } List <Token> tokens = new List <Token>(); StringReader reader = new StringReader(cleanExpr); Token t = null; do { t = new Token(reader); tokens.Add(t); } while(t.type != Token.TokenType.EXPR_END); List <Token> polishNotation = Token.TransformToPolishNotation(tokens); var enumerator = polishNotation.GetEnumerator(); enumerator.MoveNext(); Expression root = MakeExpression(ref enumerator, evalFunction); return(root.Evaluate()); }
/// <summary> /// 使用AI放置棋子 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="ef">评价函数</param> public static void PlaceChessAI(GameState gs, EvaluateFunction ef) { IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates(); int x = 0, y = 0; double value = Settings.LOSE_POINT; double temp_value = 0; while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, Settings.SEARCH_DEPTH - 1, Settings.LOSE_POINT, Settings.WIN_POINT, gs.turn, ef); if (temp_value > value) { x = ie_gs.Current.X; y = ie_gs.Current.Y; value = temp_value; } } } //Debug.WriteLine("{0} {1} {2}", x, y, temp_value); gs.PlaceChess(x, y, gs.NextPlayer()); }
private void OnEvaluateFunction(string name, FunctionArgs args) => EvaluateFunction?.Invoke(name, args);
/// <summary> /// Constructs a NEAT client with the given pedigree, number of organisms, and evaluation function. Uses 4 for the initial compatibility distance, /// <see cref="NEAT.NEATClient.NERO_CD_Function(int)"/> for the CompatibilityDistanceFunction, and 0.8 for the survival percentage. /// </summary> /// <param name="pedigree">The pedigree used to track all genes in this NEAT client.</param> /// <param name="numOrganisms">The total number of organisms this NEAT client will train.</param> /// <param name="evaluateFunction">The function to evaluate organisms' neural networks and give them a fitness score.</param> public NEATClient(Pedigree pedigree, int numOrganisms, EvaluateFunction evaluateFunction) : this(pedigree, numOrganisms, evaluateFunction, 4, NERO_CD_Function, 0.8) { }
/// <summary> /// MIN_MAX博弈的核心 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="depth">搜素深度</param> /// <param name="min">允许的最小值</param> /// <param name="max">允许的最大值</param> /// <param name="cp">评判的是哪一方</param> /// <param name="ef">可调整的评价函数</param> /// <returns></returns> public static double MinMax(GameState gs, int depth, double min, double max, ChessPiece cp, EvaluateFunction ef) { ChessPiece winner = gs.WhoIsWin(); if (winner == cp) { return Settings.WIN_POINT; } else if (winner == GameState.AnotherPlayer(cp)) { return Settings.LOSE_POINT; } if (depth == 0) { return ef(gs); } double value, temp_value; // 当前为极大节点 if (gs.NextPlayer() == cp) { // 检查其所有子节点 value = min; IEnumerator<GameState> ie_gs = gs.GetNextSiblingStates(); while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, depth - 1, value, max, cp, ef); // alpha pruning if (temp_value > value) { value = temp_value; } if (value > max) { return max; } } } return value; } // 当前为极小节点 else { // 检查其所有子节点 value = max; IEnumerator<GameState> ie_gs = gs.GetNextSiblingStates(); while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { // beta pruning temp_value = MinMax(ie_gs.Current, depth - 1, min, value, cp, ef); if (temp_value < value) { value = temp_value; } if (value < min) { return min; } } } return value; } }
public static int ComputerVSComputer(EvaluateFunction fun1, EvaluateFunction fun2) { // 经过指定步数则为和棋 int step = Settings.DRAW_STEPS; GameState gs = new GameState(); ChessPiece winner = ChessPiece.EMPTY; while (step-- != 0) { winner = gs.WhoIsWin(); if (winner == ChessPiece.EMPTY) { if (step == Settings.DRAW_STEPS - 1) { gs.PlaceChessInCenter(); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } else if (step % 2 == 1) { PlaceChessAI(gs, fun1); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } else if (step % 2 == 0) { PlaceChessAI(gs, fun2); Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString()); } } else if (winner == ChessPiece.CROSS) return Settings.WIN_FITNESS; else if (winner == ChessPiece.NOUGHT) return Settings.LOSE_FITNESS; } return Settings.DRAW_FITNESS; }
/// <summary> /// 使用AI放置棋子 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="ef">评价函数</param> public static void PlaceChessAI(GameState gs, EvaluateFunction ef) { IEnumerator<GameState> ie_gs = gs.GetNextSiblingStates(); int x = 0, y = 0; double value = Settings.LOSE_POINT; double temp_value = 0; while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, Settings.SEARCH_DEPTH - 1, Settings.LOSE_POINT, Settings.WIN_POINT, gs.turn, ef); if (temp_value > value) { x = ie_gs.Current.X; y = ie_gs.Current.Y; value = temp_value; } } } //Debug.WriteLine("{0} {1} {2}", x, y, temp_value); gs.PlaceChess(x, y, gs.NextPlayer()); }
static public Expression MakeExpression(ref List <Token> .Enumerator polishNotationTokensEnumerator, EvaluateFunction _evalFunction) { if (polishNotationTokensEnumerator.Current.type == Token.TokenType.LITERAL) { Expression lit = new ExpressionLeaf(_evalFunction, polishNotationTokensEnumerator.Current.value); polishNotationTokensEnumerator.MoveNext(); return(lit); } else { if (polishNotationTokensEnumerator.Current.value == "NOT") { polishNotationTokensEnumerator.MoveNext(); Expression operand = MakeExpression(ref polishNotationTokensEnumerator, _evalFunction); return(new ExpressionNot(operand)); } else if (polishNotationTokensEnumerator.Current.value == "AND") { polishNotationTokensEnumerator.MoveNext(); Expression left = MakeExpression(ref polishNotationTokensEnumerator, _evalFunction); Expression right = MakeExpression(ref polishNotationTokensEnumerator, _evalFunction); return(new ExpressionAnd(left, right)); } else if (polishNotationTokensEnumerator.Current.value == "OR") { polishNotationTokensEnumerator.MoveNext(); Expression left = MakeExpression(ref polishNotationTokensEnumerator, _evalFunction); Expression right = MakeExpression(ref polishNotationTokensEnumerator, _evalFunction); return(new ExpressionOr(left, right)); } } return(null); }
public ExpressionLeaf(EvaluateFunction _evalFunction, string _content) { evalFunction = _evalFunction; content = _content; }
public ScriptQualifiedName(AstNodeArgs args) : base(args) { IsVariable = false; NextFirst = false; IsVar = false; if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0) { _identifier = ExtractId(((TokenAst)ChildNodes[0]).Text); IsVariable = true; _evaluation = EvaluateIdentifier; _assignment = AssignIdentifier; } else if (ChildNodes[0] is TokenAst && ChildNodes[1].ChildNodes.Count != 0) { _identifier = ExtractId(((TokenAst)ChildNodes[0]).Text); //NOTE: There might be two cases: // 1) a()[]...() // 2) a<>.(NamePart) _modifiers = new List<ScriptAst>(); foreach (ScriptAst node in ChildNodes[1].ChildNodes) _modifiers.Add(node); var generic = _modifiers.FirstOrDefault() as ScriptGenericsPostfix; if (generic != null && _modifiers.Count == 1) { //Case 2 _evaluation = EvaluateGenericType; _assignment = null; } else { //Case 1 _evaluation = EvaluateFunctionCall; _assignment = AssignArray; } } else { _namePart = ChildNodes[0] as ScriptQualifiedName; _identifier = ExtractId(((TokenAst)ChildNodes[2]).Text); NextFirst = true; if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count != 0) { _modifiers = new List<ScriptAst>(); foreach (ScriptAst node in ChildNodes[3].ChildNodes) { _modifiers.Add(node); } } _evaluation = EvaluateNamePart; _assignment = AssignNamePart; } }
private void OnEvaluateFunction(string name, FunctionArgs args) { EvaluateFunction?.Invoke(flow, name, args); }
/// <summary> /// MIN_MAX博弈的核心 /// </summary> /// <param name="gs">游戏状态</param> /// <param name="depth">搜素深度</param> /// <param name="min">允许的最小值</param> /// <param name="max">允许的最大值</param> /// <param name="cp">评判的是哪一方</param> /// <param name="ef">可调整的评价函数</param> /// <returns></returns> public static double MinMax(GameState gs, int depth, double min, double max, ChessPiece cp, EvaluateFunction ef) { ChessPiece winner = gs.WhoIsWin(); if (winner == cp) { return(Settings.WIN_POINT); } else if (winner == GameState.AnotherPlayer(cp)) { return(Settings.LOSE_POINT); } if (depth == 0) { return(ef(gs)); } double value, temp_value; // 当前为极大节点 if (gs.NextPlayer() == cp) { // 检查其所有子节点 value = min; IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates(); while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { temp_value = MinMax(ie_gs.Current, depth - 1, value, max, cp, ef); // alpha pruning if (temp_value > value) { value = temp_value; } if (value > max) { return(max); } } } return(value); } // 当前为极小节点 else { // 检查其所有子节点 value = max; IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates(); while (ie_gs.MoveNext()) { if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y)) { // beta pruning temp_value = MinMax(ie_gs.Current, depth - 1, min, value, cp, ef); if (temp_value < value) { value = temp_value; } if (value < min) { return(min); } } } return(value); } }
public static void Evaluate(EvaluateFunction argument) { argument(); }
public static void Evaluate(EvaluateFunction argument) { IsNotNull(argument); argument(); }
public void EvaluateFunction(Channels channels, EvaluateFunction evaluateFunction, double[] values, int length) { IntPtr exception = IntPtr.Zero; #if ANYCPU if (NativeLibrary.Is64Bit) #endif #if WIN64 || ANYCPU NativeMethods.X64.MagickImage_EvaluateFunction(Instance, (UIntPtr)channels, (UIntPtr)evaluateFunction, values, (UIntPtr)length, out exception); #endif #if ANYCPU else #endif #if !WIN64 || ANYCPU NativeMethods.X86.MagickImage_EvaluateFunction(Instance, (UIntPtr)channels, (UIntPtr)evaluateFunction, values, (UIntPtr)length, out exception); #endif CheckException(exception); }