AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Stockfish engine12/24/2023 ![]() So many Knights, and yet, it’s still a mate in 3 puzzle. Peanlty += 6 - len(board.pieces(chess.KNIGHT, chess.BLACK)) * 0.1 Penalty = 1 - len(board.pieces(chess.KNIGHT, chess.WHITE)) * 0.01 What if instead, we decide that we want as many Knights as possible, preferably enemy Knights? We’ll change our function to add this as a penalty: # Removed Every other move allows black to equalize or even gain a small advantage.īut, how much control do we really have? Our current function tries to include as few pieces as possible. I included the top four engine moves on the right, but as you can see there’s only one move white can play which will win the game. You’ll get a different result every time you run this, but here’s one example: An invalid position is heavily penalized, as are cases where the game is already over.A mate in 4 puzzle is better than a mate in 7 puzzle.Similar to above, we want chess positions that are close to our target to have lower values than chess positions that are further away. This will allow us to check more properties of the board-like if it’s a valid position, if it’s currently a stalemate, etc. We can use the python library python-chess to construct a chess board object. And the integers will be between 0 and 12, representing an empty square or a piece. What if we generate 64 integers - one for each chess position. Our function is pretty arbitrary though-who’s to say that it needs to represent some mathematical function. In the second graph, we just constantly got the same value until we gave up. In the first graph, we see that the algorithm quickly converged on the right answer, getting feedback that it was on the right path. You can see this in the graphs that are produced: Over time, our objective function f(X) decreases Over time, our objective function f(X) stays the same In our second function, our algorithm basically gets no feedback until we happen to get. In our first function, solutions close to have lower values than solutions further from. If you think about how our black box n() might work, you can probably figure out why this happens. These two functions have the same best solution, but the second one fails occasionally (using the default parameters) whereas the first one doesn’t. It’s also worth contrasting that function, with this one: def f(X): Return abs(X - 3) + abs(X - 1) + abs(X - 10)Īnd that works pretty easily. Since f(X) just sums up the 3 integers, the best solution is if they are all 0Īdding three numbers together is cool and all, but let’s try something slightly more complicated.Our goal is to minimize the function f(X).Those integers are all between 0 and 10, inclusive.We have a function f(X) that takes in an array of 3 integers.Model=ga(function=f,dimension=3,variable_type='int',variable_boundaries=varbound) Let’s start with an example from the docs: import numpy as npįrom geneticalgorithm import geneticalgorithm as ga We won’t go into the details here, and instead will focus on what interfaces the library exposes. So, at a high level, we take some solution to a problem and randomly modify it to get new possible solutions. The main thing to note, from that article, is in order to find a solution using the GA, random changes applied to the current solutions to generate new ones There are a number of good articles that explain what genetic algorithms are (like this one). ![]() The first puzzle was generated with the constraint “Include as many Knights as possible,” which explains why both sides have way too many Knights.īut before we get into all that, we’ll start by understanding the library. ![]() This means that White can win the game in 3 moves, but only if they find one specific move. If you aren’t familiar with chess, these are both called “mate in 3” puzzles. ![]() In this post, we’ll use it to generate chess puzzles that look like this: The python library geneticalgorithm is beautifully open ended-exposing a simple but powerful interface that we can use for all sorts of weird stuff. One of my favorite things to do is to find interesting libraries and test out unusual use cases with them. ![]()
0 Comments
Read More
Leave a Reply. |