// Evaluation function public static double EvaluateWithWeights(State state, WeightVector weights) { if (state.IsGameOver()) return GetLowerBound(weights) - 10; else { double corner = 0, emptycells = 0, highestvalue = 0, monotonicity = 0, points = 0, smoothness = 0, snake = 0, trappedpenalty = 0; // Only do the heuristic calculation if the weight is not 0 (avoid unnescessary work) if(((WeightVectorAll)weights).Corner != 0) corner = Corner(state); if(((WeightVectorAll)weights).Empty_cells != 0) emptycells = EmptyCells(state); if(((WeightVectorAll)weights).Highest_tile != 0) highestvalue = HighestValue(state); if(((WeightVectorAll)weights).Monotonicity != 0) monotonicity = Monotonicity(state); if(((WeightVectorAll)weights).Points != 0) points = Points(state); if(((WeightVectorAll)weights).Smoothness != 0) smoothness = Smoothness(state); if(((WeightVectorAll)weights).Snake != 0) snake = WeightSnake(state); if(((WeightVectorAll)weights).Trapped_penalty != 0) trappedpenalty = TrappedPenalty(state); // evaluation function is a linear combination of heuristic values and their weights double eval = ((WeightVectorAll)weights).Corner * corner + ((WeightVectorAll)weights).Empty_cells * emptycells + ((WeightVectorAll)weights).Highest_tile * highestvalue + ((WeightVectorAll)weights).Monotonicity * monotonicity + ((WeightVectorAll)weights).Points * points + ((WeightVectorAll)weights).Smoothness * smoothness + ((WeightVectorAll)weights).Snake * snake - ((WeightVectorAll)weights).Trapped_penalty * trappedpenalty; if (state.IsWin()) { return eval + 10; } else return eval; } }
// Simple evaluation function only using Snake heuristic public static double Evaluate(State state) { if (state.IsGameOver()) { return -1000; } else { double eval = WeightSnake(state); if (state.IsWin()) return eval + 1000; else { return eval; } } }