Featured post

Textbook: Writing for Statistics and Data Science

If you are looking for my textbook Writing for Statistics and Data Science here it is for free in the Open Educational Resource Commons. Wri...

Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Sunday, 29 September 2019

Tips to successfully web scrape with a macro


Recently, I updated the cricket simulator that I made in grad school, which entailed gathering four years of new T20I, IPL, and ODI cricket data. That's about 1000 matches, and the website ESPNcricinfo has been dramatically updated since I first scraped it. For that matter, so have the tools in R for scraping.

However, for one part, the play-by-play commentaries, nothing was working, and I ended up relying on recording and repeating mouse-and-keyboard macros. It's crude, but the loading-as-scrolling mechanic was just too hard to deal with programmatically, even with otherwise very powerful Rselenium.

Using macros to scrape pages is a trial-and-error process, but with the following principles, you can drastically reduce the number of trials it takes to get it right.

Sunday, 28 January 2018

AlphaZero, Stockfish, and flexibility regarding chess variants.

Recently there was a high-profile set of matches between reigning champion chess AI stockfish and a newcomer called AlphaZero. AlphaZero was created with the same deep learning System that created AlphaGo, an AI that beat the world's best at the game Go. In the 50 matches that AlphaZero played as white, it won 24 of them and drew on the other 26. In the 50 games that it played as black it won three of them and drew on the other 47.

This advantage towards the white player may seem startling, however it's not out of line with other matches between artificial intelligence programs at the world-class level, nor is it out of line between matches between world-class human players. Stockfish, which evaluates positions in a chess game in terms of pawns of advantage starts the game with an advantage towards white of 0.1 pawns. AlphaZero, on the other hand, has no idea how many pawns of advantage white has, because it looks at the game holistically which is a radically different method of analysis compared to other modern AIs.

Part of the reason I bring up issues of artificial intelligence is to look at how well the these various systems will carry over to different chess variants rather than just the orthrodox version of the game.

Let's start with Stockfish: Stockfish is a system very much like Deep Blue and many of the other ones that came between it and Stockfish. The difference being that Stockfish is open source, meaning anyone can examine the code and edit it. This and many of the artificial intelligence programs that came before it run on a minimax principle, meaning that they try to choose the move on the assumption that their opponent will choose the best counter move in response to it, thus they try to pick the move which has the worst best counter-solution. (They try to minimize their opponent's maximum move quality.

To simplify, consider this abstract game. You have two options: Option A allows your opponent to score 5 points. Option B allows your opponent to choose between a move that scores 6 points, and a move that scores 2 points. Assuming that your opponent will choose their best move, your best choice is to select Option A, because it limits their score to 5. The fact that Option B provides the possibility for your opponent to score only 2 points is irrelevant. This is the minimax principle.

Most of what a traditional chess AI does when selecting a move is to evaluate a particular position is worth in terms of some abstract score, such as 'number of pawns'. The value of pieces is straightfoward: a pawn is worth approximately 1, a bishop or knight is worth about 3, and a queen is worth roughly 9. However the position of these pieces also matters. Having a piece in the middle or able to reach the middle at any point is worth a premium. A 'passed pawn', or one which has no opposing pawn directly ahead of it, is worth more than it would otherwise be, because of its greater potential to be promoted. The alpha-beta algorithm (not related to AlphaZero) contains set of parameters which decide how much each board piece is worth on each square. Different machine learning methods such as neural networks can be used to determine what these parameters should be.

For variants of chess that are very close to the original game such as Chess 960 (a.k.a Fischer Random Chess) or Really Bad Chess, which both feature 8 by 8 grids, 16 pieces per side, and only the orthodox six pieces, an AI using the alpha-beta algorithm should be able to play such games with few if any complications.

These AIs work even after pieces have been removed from the game so variants that use fewer pieces don't produce any difficulties either. In practice, variants with different board sizes are different arrangements such as Martin Gardener's mini chess or Romanchenko Chess (shown in the figure, source: Jocly) work well too as long as the value of any squares beyond the board are hard-coded to zero. This also means in practice that an alpha-beta algorithm can produce a viable chess AI on a board that is not a perfect square or rectangle. However it can increase the computational load the non-viable squares are considered, as they are in Jocly's implementation of alpha-beta on Romanchenko Chess.



Some systems, including Deep Blue, take advantage of chess literature, specifically for the orthodox game also take advantage of openings and their reputations methods for winning particular and games such as when you have a rook and a bishop against an opponent who just has a rook. However, after the opening and before the end game it's pretty much alpha-beta all the way. [1]

Variants that included new pieces such as fairy chess, or non-linear board movement such as Smess, the Ninny's Chess, can also be supported by AI programs that uses the alpha-beta algorithm. However these programs will need additional manual training to be able to evaluate the value of different pieces and space.

AlphaZero works on an entirely different principle; it does not assume that its opponent is the best possible opponent, one which will make the best possible counter move. Instead, AlphaZero evaluates a candidate position by simulating games of weighted random moves starting from the position to be evaluated. The evaluation is simply the proportion those random-move games that win* from AlphaZero's side. It evaluates the position this way for each move that it could make, and simply chooses the move that results in the best win proportion.

In these simulation games that AlphaZero uses, the weighting of the moves is based on moves that are likely to lead to a win based on games that AlphaZero played against itself. For example, AlphaZero may assign more weight towards a move that takes a piece over one that doesn't. It may also assign greater weight towards moves that give it control of the centre of the board. But these weight assignments would not be the result of any human supervision.

Similarly, AlphaZero has no concept of chess theory such as openings or their refutations, and it doesn't have a book of endgames to rely upon. AlphaZero was trained simply by giving the system the rules of chess, and letting it play many games against different versions of itself. It's reasonable to assume from here that AlphaZero would be able to handle many chess variants without any additional modifications other than informing it of the new rules. Furthermore a very similar training system could be given to nearly any chess variant to produce an AI program that could play that particular game.

* More exactly, the evaluation is (Proportion of Wins) + 1/2*(Proportion of Ties)

[1] Beyond Deep Blue: Chess in the Stratosphere, Monty Newborn

Sunday, 28 August 2016

Why Chess? Part 1: Smaller Boards

Chess fascinates me; it's a very pure board game, and it has existed in some recognizable form for centuries. There's a couple of general questions I have about chess that I return to occasionally, but haven't formally approached until now.

1) Why is the game that we consider chess the orthodox game and not some variant or alternative?

2) How robust is existing artificial intelligence to changes in chess?


Chess, at least in the western world, fills a niche in the game market. It's a competitive game between two players with no random chance and no physical requirement. The rules of the game take an hour to learn, a day to understand, a lifetime to master. In game design terms, chess has a great deal of depth, but limited complexity.

It's not the only such game. The game of Go, for example, has fewer rules, comparable depth, and an effective and convenient method to handicap.

Also, what about variants of chess? Would the game be less interesting or deep if the knight moved in a 3-and-1 jump instead of a 2-and-1 jump? What if the board dimensions were different? Chess has changed before, however slowly. Is it just an accident of history that this is the particular game we got, or is there something optimal about it?

I've been playing games of different variations of chess starting with those available on the Android app Chess Variants. Several of the variants avialable in this app are games that use the same kinds of pieces as orthodox chess but fewer of them, and on a smaller, simpler board. One clear pattern has emerged after 50 plays across 3 variants: I'm horrible. I am embarrassingly bad at chess compared to a computer opponent.

Mini chess (original).

The first variant I tried was Martin Gardner's 5x5 variant, made in 1969. I played as white (first) against an AI opponent (to be discussed in part 2) about 20 times. The result was 0 wins, 2 draws, and a heap of losses. The app allows you to play as black, or even as both for local play, but I'm stubborn.

The layout, shown in Figure 1, suggests a major advantage to black to a naive player like myself. There are 7 possible moves white can make at the beginning of the game. All of these moves let a piece be captured right away. What else could explain my tremendous defeat?

Figure 1 – Martin Garner's original Minichess

I looked up the game to see if my suspicions on white's disadvantage were correct. They were not. It turns out the Gardner's 1969 version of 5x5 chess is a weakly solved problem. Each player can play perfectly and always draw or win. If both are playing perfectly, the game ends in a draw. Furthermore, the perfect play algorithm (also called the 'oracle'), is a mere ~10 pages of if-then instructions.

The "weakly" in weakly solved refers to the fast that a perfect strategy exists and is known from the starting position of the game, but not necessarily every position. So if you play sub-optimally to start a game, the 'oracle' doesn't necessarily cover your situation.

A paper by Mhalla, M., & Prost, F. (2013) outlines this perfect strategy and talks about the winning rates of each side in a sample of historical correspondence games. The white player won slightly more of these games than black did.

Mini chess (updated).

This is a 1989 update to Gardner's 5x5 chess. It is identical except that the knight and bishop have been swapped on the black side.

I played 5 games on this version after playing the 1969 version. Games lasted longer and were closer, but with a small sample and an experience confound, I'm not confident in explaining why.

Micro Chess

This seems to be as minimal as chess gets without being a chess puzzle instead of a game. The layout, shown in Figure 2, only has one pawn per side. The pawn can be moved two squares in its first move. However, the pawn cannot be promoted to a queen. This seems reasonable as no player starts with a queen.

Figure 2 – Micro Chess

There is a one move checkmate for black:
White bishop to C2
Black knight to C3
White is in checkmate.

I was unable to win any match at this (again as white) either, but I did reach a draw quite often. My suspicion is that the knight is the most powerful piece, and that this change in the relative strength of pieces could cause an AI opponent to make poor trades if it was using piece valuations from classic chess. No such luck.


The Alpha-Beta Algorithm

The AI used in this app employs the alpha-beta tree algorithm, which is good for quick computation, like on a phone, because it eliminates large sets of possible actions quickly. It's also non-deterministic; it will choose different actions in identical situations if there is no single obviously best solution.

The alpha-beta algorithm is not specific to chess. It could be used for any discrete choice system, like Shogi or Go. However, it relies on a heuristic scoring system. To use the algorithm, an AI needs a way to evaluate how good or bad a consequence of that action is. Last time, I talked about throwing off the AI by assuming it would under-value a knight. That is, I was assuming that the value assigned to the consequence "lose your knight" would be copied over from classic chess to micro chess, and that this value could be exploited.

The Alpha-Beta algorithm also has some tuning parameters that determine the quality of the decisions made by it. By changing the number or depth (turns removed from the current situation) of the possibilities that the algorithm will evaluate. These parameters can be manipulated indirectly in the Chess Variants app by a difficulty setting with four options: easy, medium, hard, and fast.

The last setting, fast, was the one I was using. In this case, the algorithm will evaluate solutions until 1 second of processor time has been used. This means the algorithm becomes harder to beat when it is provided more processing power, so I can blame my equipment for my losses. The AI may have been playing a close-to-perfect game on the simplified boards, because the space of possible states was much smaller. I also noticed that in orthodox chess matches, the AI got better as the game progressed and pieces were removed from the board. I could frequently take the opponent's queen without a sacrifice only to be crushed in the end-game.

For existing pieces, the value of retaining each piece, and having them in various positions relative to an opposing king is well established by humans. That, I suspect, is why Jocli, the platform used to make all these chess variants online, only has variants that use pieces that exist in orthodox chess and which alter the board - because the heuristics for these (combinations of) pieces are well-established.

Next, I intend to look into Chess 960, which is a variant played on an 8x8 board in which the starting positions of non-pawn pieces are randomized. There are 960 possible starting arrangements that fit the restrictions (King is between rooks, bishops are on opposite colours, black mirrors white, etc. ), hence the name.

An AI may not be able to adapt a few opening strategies from orthodox chess to chess 960, but it fares well by treating any starting as a game already in progress (even if that progress was impossible). If you introduced a common chess puzzle piece, such as the grasshopper or princess, would an entirely new set of heuristics be needed?



Relevant links:

Mhalla, M., & Prost, F. (2013). Gardner’s minichess variant is solved. ICGA Journal, 36(4), 215-221.

https://jocly.com The platform used for the Chess Variants app. Also playable in a web browser.

https://en.wikipedia.org/wiki/Minichess including Gardner's Minichess, and Microchess.

https://en.wikipedia.org/wiki/Fairy_chess_piece list of unorthodox chess pieces used in puzzles and variants.



Still to explore:
Onitama
The Duke
Nightmare Chess
Alice Chess
The Encyclopedia of Chess Variants, by David Pritchard
The Classified Encyclopedia of Chess Variants, by John Derek Beasley

Friday, 8 May 2015

The end of jobs

A friend recently asked me if I foresee any chance of "jobs", or in his words "the economic trade of labour in return for payment" becoming so unsustainable that we as society would abandon it. My response is below.

-----------------

No.

The term "chance" implies that I'm not certain about the unsustainability.

There will NEVER be enough meaningful jobs for everyone. Unemployment is only in a 'healthy' range around 6-7% right now because of a system stretched to its utter limit to create jobs, often at the cost of getting meaningful work done.

First, self employment is counted as jobs in this statistic, as is part time work. So the proportion of people that trade their labour for payment likely a lot smaller than official surface figures.

There are also a large portion of jobs that simply shouldn't be.

- Literally pointless jobs like full-service gas pumps. Really gas stations could be fully automated and could behave like large vending machines.

- Parasitic jobs such as car salespeople, day traders and arbitrageurs. I separate these from pointless jobs because they do perform a service, but only because of legacy systems that mandate that these services are necessary.

- Fields where the output of the field is only loosely related to the number of people working in the field, such as marketing. From the perspective of companies, if half of all ads disappears, the only real effect would be for each remaining ad to be twice as effective. Likewise, the benefit to the consumer, knowledge of a product, would be just as large with perhaps only 10% of ads retained. In that sense, 90% of the work in advertising, from ad creation to posting billboards, is parasitic.

Then there are the jobs that won't be for much longer.

- Physical jobs that are due for automation, such as semi-truck driving.

- Small manufacturing jobs that can be simply replaced by on-demand 3D printing.

- Technical jobs that routinely get automated, like how much search engines have supplanted librarians.

- Many service jobs are a fixed portion of the population, such as teaching, haircutting, and child care. However, the population of countries in the developed world are either flat, declining, or dependant upon immigration to maintain the population increase that modern economics relies upon so dearly.

- Many resource-based jobs are at risk to better energy efficiency, better labour efficiency, and automated landfill harvesting and reclaimation. Even argiculture is being turned upside down by cultured meat. With it, there goes shipping.

Finally, the work that NEEDS to be done such as environmental restoration, medical services, and the development of space technology, simply doesn't work well under an exchange-for-payment system because economically 'rational' people and corporations either won't or can't pay for it.

I would refer you to the 20 minute video "Humans Need Not Apply" for a compelling argument about how this is inevitable. My best resources for universal basic income and on post-scarcity are the novels Accelerando and Red Mars touch on these topics.