(Re-)Learning C Via Computer Chess

In recent months I haven’t had much time to do a lot of programming, what with the demands of my work. One thing I’d been meaning to do, whether it factors into my research directly or not, was to re-acquaint myself with the C programming language. I used it way back in the day, but then as time went on I fell in love with Python, which despite being ridiculously slow in comparison, is extremely fun to use. But the fact remains that it’s very useful to be able to write compact, speedy code from time to time, either for writing simulations for work or for passion projects.

So, I decided to find myself just such a passion project to rediscover the joy of programming in C, and given that I’ve been playing and studying a hell of a lot of chess and shogi in my spare time of late, I decided to learn how to program a fast and relatively powerful chess engine in C. A traditional chess engine uses brute force to search a very large number of possible moves on its turn, evaluating each one in turn until it chooses what it thinks is the best move for the situation. Given how much computing power is available these days, even a half-decent smartphone can now play chess at a level greater than any human, including Grandmaster-level professionals.

In order to do this I followed a great series of videos on YouTube called ‘Programming a Chess Engine in C’, which is 95 videos long (!), but covers a ton of stuff, helping you build a fully-functional chess engine in C which uses the standard techniques in chess programming — alpha-beta search with null-move pruning and some other optimisations. The engine is capable of playing a game of chess via text commands with the user, or by communicating with graphical chess software using the UCI or WinBoard/CECP protocols to let you play a game with mouse control and lovely graphics for the pieces.

After watching all that and feeling my way around C again, I’ve now produced a chess engine of my own, which I’ve named SpaceDog, in honour of my dog who is from space.  At the moment it’s basically the same as the VICE engine which comes from the videos above, but has a few small additions in the evaluation function to make it a little stronger (hopefully), as well as a few quality-of-life improvements here and there.  It works great, and plays a mean game of chess already — which perhaps isn’t surprising since it searches and evaluates about 3.5 million chess positions per second!  In comparison a master-level human player might evaluate perhaps 3 or 4 positions per second.

Here’s a screenshot of SpaceDog playing in text mode:

Screen Shot 2018-10-14 at 03.23.34As you can see, it prints out a nice little text-based board for you (white pieces are capital letters, black pieces are lowercase).  Moves are entered in long algebraic notation — so to move white’s queen at the bottom of the board to the square above white’s king, you’d enter d1e2.  SpaceDog also prints out its search results and position evaluations on each move, so here you can see at the bottom that it searched nine moves ahead (depth:9) and spent 2.9 seconds evaluating 11.9 million moves before choosing the move e7e4 (taking my pawn with its queen) based on what it thinks of the resulting position and its future prospects.

Every searched position is evaluated quite simply, with a score calculated on the basis of material balance, the position of the pieces, and things like whether there are isolated pawns and other key features.  Right now I’m adding some additional evaluation terms that better capture how the relative value of certain pieces, and their ideal placement on the board, changes as you proceed from the opening to the endgame.  Hopefully this will make SpaceDog a bit more shrewd at finding checkmate!

The engine can also use opening books — these are files generated by processing millions of opening moves from many hundreds of thousands of professional chess games, choosing a repertoire of openings based on what moves proved to be most successful.  This means SpaceDog essentially has a huge file of opening moves already catalogued in the book, with an enormous selection of replies and counter-replies for all the best possible responses from the opponent.  These moves then don’t need to be searched, meaning that SpaceDog saves tons of time for searching much deeper in difficult middlegame and endgame positions.

At this point SpaceDog probably plays well enough to beat anyone I know, but would likely still lose to players above Master level.  That would probably change at fast time controls — i.e., quick game setups like blitz (5 or 10 minute time limit for each player) or bullet (1 minute each!).  At these time controls, humans simply can’t make much use out of our superior long-term strategic planning abilities, so even SpaceDog’s rudimentary but tactically sound play should be tough to beat when us human meat-bags are sweating over the clock and feeling the pressure.

Anyway, it’s been a lot of fun so I plan to keep it going!  Next steps are to continue to enhance the evaluation function to better account for things like keeping the king safe and setting up outposts for bishops and knights.  I’ll also work on some more technical enhancements like multi-PV search (searching multiple lines of play on multiple CPU cores simultaneously) and adding support for endgame tablebases to allow SpaceDog to achieve perfect endgame play.

Most importantly though, I want to add a mode so SpaceDog can play Crazyhouse and Chessgi, variants of chess in which captured pieces become yours and can be dropped back onto the board as part of your army.  This is a feature taken directly from shogi which is a game I also love, so I’m looking forward to implementing these.  Eventually I may try to build on that foundation and add a shogi mode as well.

‘What’s the point of all this?’ you’re probably asking at this point — after all, SpaceDog will never be as good as current strongest engine Stockfish, and plenty of other engines play Crazyhouse and lots of other variants besides (such as this version of the mighty Stockfish).  There are even innovative neural-network-based engines coming out now like LCZero that are challenging for the throne of toughest computer opponent.  But nevertheless writing SpaceDog has been satisfying and fun, and it’s given me another way to learn more about chess and enjoy the game from a different angle.  I’d also forgotten how satisfying coding in C can be — the final SpaceDog program takes up only 74KB (!), yet it effortlessly plays chess better than I can.

Anyway, I thought I’d post this up just on the off chance anyone else might get something out of learning a bit about chess programming.  I highly recommend the tutorial videos I linked above from Bluefever Software — they’re really easy to follow and provide excellent explanations of the key concepts you’ll need to know to write a chess engine.

Someday I’ll post up the code for SpaceDog too, once I add a few more additional features in!

Advertisements
Tagged , , ,

The Game of Go

I’ve been off work for a couple of weeks now, and rather than going on holiday I’ve mainly been trying to rest up after several months of really intense work.  As part of my attempted recuperation I’ve been playing a lot of the board game Go, a game I have a tremendous fondness for but go through long periods of slacking off when my brain is busy with work stuff.  I’ve been enjoying the opportunity to play more and thought that some of my friends and colleagues might enjoy the game too, so I decided to put together an intro to the game, alongside copious links to resources and ways to play online.  I hope this might spur some of you out there to give it a try.

Go is a board game that’s been around for a very long time, and is generally believed to be the oldest board game still being played today; Go was invented about 3000 years ago in ancient China.  The game is still immensely popular in Japan, China (where it is known as weiqi), and Korea (baduk), where professional play is well-established, and it boasts a  growing following in America and Europe as well.

I’ll start by giving you a very brief summary of the rules of the game, then link you to some resources that will help you get started.

Playing Go

Go is known as a game of great complexity and subtlety, but the actual rules are simple.  There are two players, White and Black, and each has 180 stones of the appropriate colour.  Black always goes first, which confers a slight advantage, so in compensation White receives a bonus (komi) of 6.5 points at the end of the game (under Japanese rules, it’s 7.5 under Chinese rules).

The players start with an empty board of 19×19 squares, like so:

empty go board

Notice that the board has nine marked star points which help players to orient themselves in this large playing space.

The players take it in turns to place a stone on an unoccupied point on the board.  Once placed, a stone never moves, although it can be removed when captured.  Stones are captured if all of the empty points surrounding them (their liberties) are occupied by stones of the opposing colour.  This GIF shows an interesting example, where Black keeps capturing White stones only to eventually have his entire group captured himself by cutting off his own stones’ liberties:

go capturing gif

Over time as stones are placed and some groups are captured or fortified, the players will sketch out and secure territory where their opponent cannot play without being captured.  At the end of the game, both players total up the amount of territory surrounded by their stones, add in the number of stones they’ve captured from their opponent, and compare the totals (plus the 6.5 points komi for White) — the player with the highest total of territory and prisoners wins the game.

Below is an animated gif showing an example of play: a complete record of the famous ‘Game of the Century’ between Go Seigen (Black) and Honinbo Shusai (White).  In this match players could adjourn to think at any time, so this game dragged on for three months!  White won by two points in the end and the game is celebrated for some brilliant moves and fierce fighting over territory.

seigen shusai gotc

You can download a huge archive of 48,000 historic Go games in SGF format here, or if you want to view records of professional games in a handy web applet you can check out GoKifu.com.

The Style of Play

From the simple rules of Go, very complex patterns of play emerge.  Unlike other prominent competitive board games like chess, in Go the board starts empty, and the number of available points to play a piece is very large (361 points on a Go board vs 64 squares on a chess board).  The sheer number of possible moves is so huge that players must rely on intuition, a solid grasp of fundamental principles of play, and keen awareness of their opponent’s movements to play well.

This reliance on intuition makes Go a surprisingly emotional game — pick up any book by a serious professional and you’ll see them speak with great passion about how various moves and games made them feel.  This in turn leads to some seriously intense contests — watch professional matches and you’ll see the players’ faces often contort with pain as they confront a powerful move by their opponent.

As a result of the complexity of the game, you’ll see Go players start their play in the corners and edges of the board — this is because it’s easier to make territory on the sides and corners, since you can use the edges as boundaries for your territory.  As the sides and corners are decided, players begin reaching toward the centre of the board, attempting to connect their strongest groups of stones together and grasp larger chunks of territory.

The beginning of a Go game is often overwhelming for new players, who struggle to figure out where to start placing stones on this massive empty board.  Generally the Black player will start play on the upper-right star point, which is considered polite to your opponent — it’s close to their end of the board and gives them a clear idea of where to start play.  This helps somewhat, but the opening is still the toughest part of the game for newcomers.

As a result, most experienced players recommend that new players start on a smaller board, either 9×9 or 13×13.  These are less intimidating to start with and get you into territory fights right away, giving you an immediate introduction to the moment-to-moment tactics of Go.  Just bear in mind that you should try to advance to the full 19×19 board as soon as you can, as it’s a very different game at that scale.  On the full board strategy becomes as important as tactics, and stones in distant corners have powerful implications elsewhere as patterns of territory shift and evolve over large swathes of the board.

Learning the Game

Learning Go is a long process, as the game requires an understanding of a lot of complex interactions between stones.  There’s also a bit of a language barrier, as many Go terms derive from Japanese and are difficult to translate, so most players just use the Japanese words.  As a result you’ll need to understand things like what it means when stones are in atari (stones that will be captured on the next move unless you do something to save them), or in seki (opposing groups of stones that can’t capture one another without endangering themselves).

Luckily there are some great introductory books I can recommend that will give you copious examples of these terms and many helpful and illustrative Go problems (tsumego) to test your skills.

For complete newbies I highly recommend Go: A Complete Introduction to the Game by Cho Chikun.  Cho Chikun is one of the greatest players alive today, being the only player in history to hold all four main Japanese titles at the same time.  His book is surprisingly short but very dense with helpful illustrations of key go concepts, and it rewards deep study and playing out the examples on a real or computerised board to experience each pattern for yourself.

From here you can check out the Elementary Go Series from Kiseido Publishing.  This series covers key concepts of the game in detail in each volume.  Many players recommend these books for players seeking to advance to intermediate levels of play.

When you’ve fully digested all this, you can try Lessons in the Fundamentals of Go by Toshiro Kageyama.  This book is thicker than the others and quite dense with content — expect getting through this to take weeks, not days.  But the concepts he explores are critical to good high-level play, and if you master them you’ll be well on your way to a thorough understanding of the game.

For online resources, I recommend studying the games of great masters, which you can easily find via the archives linked above and at places like GoKifu.com.  The LifeIn19x19 forum is a great place to get tips from other Western players, with many members happy to give guidance on your development as a player.  Finding good software is also key to learning the game, as this will be how you view and study the games of great players, or analyse your own games to examine your own strengths and weaknesses.

 

Go Software

This section will be relatively short, because in my view there’s really just one piece of software you really need to study Go, and that’s Sabaki.  Sabaki is free, open-source, and works perfectly on Windows, Mac and Linux.  It also nicely captures the austere aesthetics of the game, showing you a lovely 19×19 wooden board sat on a tatami mat and simple black and wide stones.  You can customise the look with themes as well, if you like.

A subtle touch that I really appreciate is that each stone is placed with a slight random touch, rather than precisely on the relevant point on the board, giving the whole thing a slightly more organic look as the game evolves.

Looks aside, Sabaki is a great piece of Go software, allowing you to view SGF files (the move-by-move records of Go games, as in the archives linked above), edit your own, and play against any AI Go engine that supports the GTP protocol (basically all of them).  In particular I recommend using Tencent’s PhoenixGo engine, which is very strong and comes with instructions on how to link it with Sabaki on Windows and Mac.

Here’s a screenshot, stolen from Sabaki’s GitHub page:

Sabaki screenshot

If you’re a more advanced user and want sophisticated analysis of your games from a strong AI, you can use Lizzie, which uses the Leela Zero engine to analyse your moves and show you what the engine recommends in each situation.  Download the latest version from the Releases page at the link, then follow the instructions in the Readme file to get started.  The results can be helpful, but bear in mind Leela Zero can only show us what she thinks, not explain why she thinks it, so still it’s better in my opinion to get input from human players and learn Go fundamentals rather than just ape the moves the computer engines recommend.

Here’s an example of Lizzie’s output, showing the user its recommended moves (the cyan-coloured one is Leela’s top recommendation):

lizzie screenshot

 

Go Equipment

Go equipment, much like the game itself, is steeped in centuries of history and tradition.  Japanese aesthetics in particular have had a profound influence on Go equipment.  Today most high-level players or keen amateurs in search of fine equipment will go to Japan to get it.

go set1

The finest Go boards are made from the Japanese Kaya tree, and makers prefer that the tree is between 700-1000 years old when the wood is harvested.  This combined with the fact that Kaya trees are protected in Japan means that true Kaya boards can be ludicrously expensive; free-standing boards with legs can rise well into the tens of thousands of pounds.  Normal people go for so-called ‘Shin Kaya’ boards, usually made of Alaskan spruce, which looks close enough to the right colour and feel for most people and can be had for £100 or so for a tabletop board of 3cm thickness.

go stones 2

The finest Go stones, meanwhile, come from a place called Hyuga in Miyazaki Prefecture in Japan.  The black stones are made of nachiguro slate stone, while the white stones are meticulously carved from clamshells, resulting in a beautiful shining surface striped with subtle lines.  The finer the stone, the thicker it is and the denser and thinner the lines are.  As with the Kaya boards, genuine Hyuga stones are incredibly rare and have price tags far beyond most mortals.  However, these days you can get clamshell and slate stones made from imported Mexican clams that are nearly as perfect as the real thing, and a good set of decent thickness will set you back about £200-300 rather than tens of thousands.

go bowls1

Finally you have the Go bowls, which hold each player’s stones.  Traditionally you place the bowls next to the board with the lid upside-down in front of it, and place any captured pieces in the upturned lid.  This is polite to your opponent, who needs to know how many pieces you’ve captured as that affects the game’s final score.  Go bowls again can cost ridiculous amounts for traditional Japanese wooden articles, but beautiful and functional bowls hand-carved from cherry and other woods needn’t set you back more than £150 or so.

The overall effect of a fine Go set should be a beautiful, smooth board with a soft yellow tone that perfectly compliments the black and white stones.  When you place a stone on the board, it should make a satisfying ‘click’ against the wood — in particular on the free-standing boards, which have a chunk hollowed out of the interior to ensure it makes a pleasing sound.

In total, if you purchase a nice table Go set from a prized Japanese maker like Kuroki Goishi-ten, expect to pay about £300-400 for the lot (plus shipping/Customs fees of course).  This is assuming a 3cm or 6cm Shin Kaya board, ‘Blue Label’ Mexican clamshell stones, and mid-level bowls of sakura wood or similar.  Various distributors do sell some of their products in Europe — check Masters of Games in the UK, or Go-Spiele.de in Germany.  Both stores also offer budget sets with Korean glass stones that are more than fine for most people; a set with a nice Shin Kaya board, Korean glass stones and Go bowls made with European wood should cost no more than about £200.

One of my great regrets from my time in Japan is that despite my intense desire for one, I never purchased a fine Go set for myself.  They really are lovely.  As a consequence of not taking that leap, I have this masochistic ritual of checking Kuroki Goishi-ten’s sales every summer, finding myself unable to justify the ~£100 in shipping costs on top of the cost of the set, and end up torturing myself over it for weeks.  Someday I’ll take the plunge; in the meantime I do hope to get a Shin Kaya/glass stones set someday soon, and then eventually upgrade to proper clamshell stones.

Go vs Chess

For those of us in the West, the average person’s familiarity with abstract strategic board games often starts and ends with chess — even people who don’t play have probably heard of Garry Kasparov and Bobby Fischer.  Both chess and go are intellectually challenging and stimulating, but they differ in quite fundamental ways.  I’ll say up front that I enjoy both, and feel each one offers something the other doesn’t.

As described above, Go is simpler than chess as far as the rules go — stones are simply placed and never moved, and complex interactions between pieces arise from their configurations, not from the rules themselves.  Meanwhile, in chess each piece moves, different types of pieces move differently, and the goal of the game is to trap the opposing king, which again depends on intimate knowledge of the roles and movements of the different major and minor pieces.

For the chess player, Go can initially seem a bit incomprehensible.  Chess openings have been so thoroughly explored by humans and computers that many experienced players play the first 10-15 moves essentially from memory (depending on their choice of opening), while in Go this vast empty board makes the opening phase really perplexing.  Note that Go players also have opening patterns they study called joseki, but these aren’t overly necessary except at high levels of play, and in fact many pros discourage new players from studying them until they’re quite advanced in their play.

The smaller boards and armies of chess mean the game also turns much more on tactics than Go.  Chess does have lots of strategy to it of course, but on the whole it’s more likely that a single move can change the course of a chess game than a single move massively changes a Go game on that huge 19×19 board.  So, if you’re more interested in intricate tactics and moment-to-moment attacking play, chess may be more your game; whereas if you’re interested in sweeping strategic movements and more intuitionist play styles, Go may be for you.

Honestly though, I’d say just play both — each game is rewarding in its own way, and I suspect a strong tactical chess background will serve you well in Go, just as strong strategic instincts in Go should surely help your chess game.  I also recommend trying chess’ East Asian cousins Shogi, Xiangqi and Janggi.  Shogi (Japanese chess) allows captured pieces to re-enter the game on the capturing player’s side which creates an interesting dynamic feel.  Xiangqi (Chinese chess) has unique pieces and a larger board with territorial restrictions; Janggi (Korean chess) is very close to Xiangqi but with some rules differences that make it a very interesting variant.  In fact I may post about these games someday down the line, as all are very accessible now with free apps for online play, and both Shogi and Xiangqi have some excellent English-language resources available (less so Janggi).

Playing Go Online

Speaking of online play, Go is likewise more accessible than ever thanks to the efforts of an extremely dedicated global community of players.

There are a number of great free services that enable online play against opponents all over the world, 24/7/365.  A few of them specialise in real-time games, in which you and your opponent finish the game in one sitting, while others focus on correspondence games, where you take your time with each move and update the game on the server when you’ve chosen your move, and games take place at a leisurely pace over weeks or even months.

All of these services are free, by the way, though some offer subscriptions with extra benefits.

Real-Time Servers

IGS (The Internet Go Server) — By far the oldest of the bunch, the IGS has been around since 1992 (!).  It started in Japan and still has the largest Japanese player community; many professionals play here.  A nice software client, CGoban2, is available for all major operating systems, and there’s a good app as well for Android and iOS.

KGS — Popular with Westerners, KGS is well-known for being a chatty server where more experienced players will offer learning games for newbies and analyse your games to help you improve.  Has lost popularity somewhat in recent years and the Android app is apparently a bit unreliable, however.  I’ve heard some players recently recommending people shift to other servers, as they have difficulty finding good opponents here nowadays.

TygemBaduk — A Korean server but has an English-language website and client.  Quite popular and apparently a good place to face strong opposition, though the client only works on Windows and iPad so be aware of that.  The web client will work on any platform, though.

WBaduk — A very large Korean server, immensely popular.  Another great place to face strong opposition, but they’ve also got problems with accessibility — just a few days ago the English client disappeared off the Google Play Store (!).  Worth joining once you’re ready for strong opponents, but perhaps worth keeping on eye on things to see whether some changes to the service might be forthcoming.

Fox Weiqi — An absolutely massive Chinese server that’s becoming increasingly popular among Westerners due to having an English-language client and a polished Android app.  Do note however that you have to sideload the app onto your Android phone, presumably due to China not really being a fan of Google services.  Also the app is in Chinese only as far as I can tell.  I’m installing the app as I write this so we’ll see if I can manage to find what buttons to mash to play a game with someone!

Correspondence Servers

Online Go (OGS) — A fabulous place to play correspondence Go.  Uses a constantly-updated, modern web interface with responsive design — meaning it works perfectly and looks great regardless of whether you use it on desktop, laptop, phone or tablet.  At any given time there are tens of thousands of correspondence games going.  Real-time play is also supported, but most players do opt for correspondence games.

Dragon Go Server — A bit old-school design-wise, but has been around a long time.  I’ve never personally played here but my impression is the userbase is pretty loyal.  The web interface works fine for what it is, but definitely miles behind OGS.  For playing on Android you can use a free plugin for the Go app BW-Go.

 

Go Apps

AQGo — An Android app that lets you play against the super-strong neural-net go bot LeelaZero, which is a community attempt to replicate Google’s super-powerful AlphaZero go bot.  Needs to be sideloaded onto your phone.

Crazy Stone Deep Learning — A beautifully polished Go app that lets you play against a strong neural network opponent.  The Pro version is a bit pricey as apps go (£12.99), but it’s been worth it for me to have an ever-present strong opponent that will analyse my games in-depth.  Also available on Steam for a much higher price but also has a stronger rating (7-dan).

GridMaster Pro — A cheap, no-frills Go app that lets you play against a variety of Go engines, including Leela Zero.  A slight caveat here in that in my experience, sometimes Leela Zero will refuse to make a move until I forcibly shut and reopen the app, but your mileage may vary.  Leela and other engines can be downloaded directly through the app and installed instantly — check the app’s website for details.

TsumeGo Pro — An app for tsumego problems, Go puzzles that test your knowledge of key Go concepts.  Extremely useful for polishing your skills and furthering your understanding of the complexities of keeping groups of stones alive — or killing your opponents groups!  In-app purchases get you more problems to solve.

Pandanet IGS — The app for the Internet Go Server above.  Free and works really well in my experience.

 

Well, that’s quite enough for one day — I hope this info might prove useful to someone out there who’s curious about Go.  It’s a wonderful game and more accessible than ever, so if you’re interested in giving it a try, just pick your choice of app and get in there!

Tagged , , ,

Diversions: Fun with FRACTRAN

Recently I’ve been learning to use Mathematica, a piece of software I was curious about for a long time.  Luckily my university has a licence for all staff, so I snagged a key as soon as I learned this and have been mucking around with some serious stuff — namely it’s surprisingly good neural network features — and some less serious stuff to get my head around the Wolfram Language.

This weekend I’ve been messing around with FRACTRAN, a fascinating esoteric programming language and model of computation developed by John H Conway, who’s perhaps most famous amongst computer types for inventing the Game of Life cellular automaton.  FRACTRAN is a language in which programs consist entirely of lists of fractions, like so:

primeProg = {17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 
 1/17, 11/13, 13/11, 15/14, 15/2, 55/1};

Now that may seem nonsensical, but bear with me here.  FRACTRAN works with really simple rules; given a list of fractions and an initial input N:

  1. Find the first fraction F in the program listing which becomes an integer when multiplied by N, then replace N by N*F
  2. Repeat until N doesn’t produce any integers when multiplied by any fraction in the program, then halt.

Easy peasy!  So we can write a FRACTRAN interpreter in Mathematica quite easily.  This one outputs each stage of program execution in order to a list called outputFrac, to allow us to manipulate the results later if need be:

fracRunList[fracProg_, input_, steplimit_] := Module[{j, state},
 j = 0;
 state = input;
 outputFrac = {};
 While[j <= steplimit, newProg = state *fracProg;
 integerList = IntegerQ[#] & /@ newProg;
 intSpots = Position[integerList, True];
 AppendTo[outputFrac, state];
 If[Length[intSpots] == 0, Break[]];
 state = newProg[[intSpots[[1, 1]]]]; j++]];

So, when you call this function with fracRunList[{list of fractions}, N, timestep limit], it multiplies N through the list, checks that new list for integer values, appends that value to the list outputFrac, then starts again.  The function will halt either when it reaches the timestep limit you specified, or when no more integers result from multiplying N through the list.

When we run the program above — suspiciously called ‘primeProg’ — with an initial N=2 for 50 steps, we get this:

{2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 
132, 116, 308, 364, 68, 4, 30, 225, 12375, 10875, 28875, 25375, 
67375, 79625, 14875, 13650, 2550, 2340, 1980, 1740, 4620, 4060, 
10780, 12740, 2380, 2184, 408, 152, 92, 380, 230, 950, 575, 2375, 
9625, 11375, 2125, 1950, 1650, 1450, 3850, 4550, 850, 780, 660, 580, 
1540, 1820, 340, 312, 264, 232, 616, 728, 136, 8, 60}

That may look like nonsense, but note that scattered through that list of numbers we have 2, 4, and 8 — which are respectively 2^1, 2^2, and 2^3.  So what PrimeProg does is actually output all the prime numbers, in the form of prime exponents of 2!

We can see this easily if we run a simple filter on the list outputFrac after running the program for 50,000 steps:

findPrimes2[list_] :=
 Log[2, Select[list, IntegerQ[Log[2, #]] &]];

fracRunList[primeProg, 2, 50000]
findPrimes2[outputFrac]

Output: {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}

After 50,000 steps, our clever little list of fractions has produced the first 13 prime numbers!  Theoretically we can run this forever, and produce every prime number.  Although each one takes longer to come out than the last, unsurprisingly, so I don’t recommend it.

Another variation of the prime-finder program I found from the Esolang wiki is more efficient, using only 9 fractions to output prime exponents of 10.  We’ll test it out below, this time filtering the resulting output list for prime exponents of 10:

prime10Short = {3/11, 847/45, 143/6, 7/3, 10/91, 3/7, 36/325, 1/2, 
 36/5};

findPrimes10[list_] :=
 Log[10, Select[list, IntegerQ[Log[10, #]] &]];

fracRunList[prime10Short, 10, 50000]
findPrimes10[outputFrac]

Output: {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41}

This prime-finder has managed to dig up 15 primes in 50,000 steps, rather than 13 like the original.

The really remarkable thing about FRACTRAN, though, is that it’s actually Turing-complete — it can in principle calculate anything calculable by any other programming language.  A simple example of a multiplication programme shows off how this works:

multiFrac = {455/33, 11/13, 1/11, 3/7, 11/2, 1/3};

fracRunList[multiFrac, 72, 50]

Output: {72, 396, 5460, 4620, 63700, 53900, 4900, 2100, 900, 4950, 68250, 
57750, 796250, 673750, 61250, 26250, 11250, 61875, 853125, 721875, 
9953125, 8421875, 765625, 328125, 140625, 46875, 15625}

What’s happened here is that we gave our FRACTRAN program a single number that actually represents our two input numbers — in this case 3 and 2 — as a product of prime numbers raised to appropriate powers — 2^3 * 3^2 = 8 * 9 = 72.  FRACTRAN then outputs the result as the power of a different prime — 15,625 = 5^6 = 5^(2 * 3).  A fairly roundabout way to get multiplication done, but it works!

What this shows is that it’s possible to have FRACTRAN programs operate on multiple inputs, so long as those inputs are encoded as products of prime powers.  In fact, we can assign specific primes to be our data registers and use carefully-constructed fractions to operate on those registers, and even construct complicated programs with loops!  As this StackOverflow answer shows, it turns out FRACTRAN is exactly equivalent to a Minsky Register Machine, which have been proven to be Turing-equivalent — hence confirming that FRACTRAN is actually a Turing-complete language.

As a consequence some intrepid folk have built some impressive constructs in FRACTRAN.  One of my favourites is a FRACTRAN interpreter which is itself written in FRACTRAN!  Using just 48 fractions, this program takes as input an encoded FRACTRAN program and initial state, and correctly interprets the program and outputs the result.  Here’s everything you need to try it:

fracInFrac = {5/19, 1558654261983398483185/122130132904968017083, 
 185/1822837804551761449, 4996917562403854655/41, 272365/67, 43/5, 
 43/71, 125173/47, 145915005923554298917151/952809757913927, 
 950886101246622507133/41426511213649, 160585150715989139597/13, 
 8752951/23, 17/43, 17/29, 6409/47, 5/17, 31/53, 17042839/7, 
 1829/41, 59/73, 331639/23, 4307/41, 89/59, 3713/31, 79/83, 
 268837/23, 31/79, 8633/7, 101/97, 68579/11, 9797/13, 9797/47, 
 35/101, 9167/13, 103/107, 1774381/47, 109/103, 109/113, 578899/23, 
 11227/13, 127/109, 127/131, 16637/47, 16637/11, 1114679/61, 2/127, 
 5/2, 3/37};

(* Encode a FRACTRAN program as a base-11 number for input into 
fracInFrac interpreter *)
base2 = 11;
pad2[f_] :=
 Block[{n = IntegerDigits[Numerator[f], base2 - 1],
 d = IntegerDigits[Denominator[f], base2 - 1], len},
 len = Max[Length[n], Length[d]]; n = PadLeft[n, len]; 
 d = PadLeft[d, len]; Flatten[{0, Riffle[n, d], base2 - 1}]];
digits2[progList_] := Join[Flatten[pad2 /@ progList], {base2 - 1}];
encode2[progList_] := FromDigits[Reverse[digits2[progList]], base2];

(* FracInFrac input state encoder function *)

fracInput[fracProg_, init_] := 5*7^init*67^(encode2[fracProg]);

In order to encode our FRACTRAN program into a format the interpreter can understand, we first need to encode the program as a single number.  In this case the encode2 function reverses the list of fractions in the program, then encodes the digits as base-10 numbers within a base-11 number.  Then we need to combine that with our initial state into a single number that we can pass to the interpreter.  We do this using the fracInput function, which gives us a ridiculous huge number that consists of 5 * 7^(initial state) * 67^(encoded program).  In fact, the resulting numbers are far too huge to print here as examples even for the simple adding program (which is just {3/2}!), and Mathematica can’t even cope with encoding larger programs and simply spits out an error.  Changing the encoding to use a smaller prime for the program is possible, but I leave that as an exercise for the reader.

Another intrepid StackOverflow commenter produced a FRACTRAN interpreter for FRACTRAN in 84 fractions, which has a slightly less ginormous program encoding:

fracInFrac3 = {197*103/(2^11*101), 101/103, 103*127/(2*101), 101/103, 
 109/101, 2*23/(197*109), 109/23, 29/109, 197*41*47/(31*59), 
 11^10*53/(127*197), 197/53, 37/197, 7^10*43/(11^10*37), 37/43, 
 59/(37*47), 59/47, 41*61/59, 31*67/(41*61), 61/67, 7*67/(127*61), 
 61/67, 101/71, 73/(127^9*29), 79/(127^2*73), 83/(127*73), 
 89/(2*29), 163/29, 127^11*89/79, 337/83, 2*59/89, 71/61, 
 7*173/(127*163), 163/173, 337*167/163, 347/(31*337), 337/347, 
 151/337, 1/71, 19*179/(3*7*193), 193/179, 157/(7*193), 17*181/193, 
 7*211/(19*181), 181/211, 193/181, 157/193, 223/(7*157), 157/223, 
 281*283/239, 3*257*269/(7*241), 241/269, 263/241, 7*271/(257*263), 
 263/271, 281/263, 241/(17*281), 1/281, 307/(7*283), 283/307, 
 293/283, 71*131/107, 193/(131*151), 227/(19*157), 71*311/227, 
 233/(151*167*311), 151*311/229, 7*317/(19*229), 229/317, 
 239*331/217, 71*313/157, 239*251/(151*167*313), 239*251/(151*313), 
 149/(251*293), 107/(293*331), 137/199, 
 2^100*13^100*353/(5^100*137), 2*13*353/(5*137), 137/353, 349/137, 
 107/349, 5^100*359/(13^100*149), 5*359/(13*149), 149/359, 199/149};

To encode the program, we reverse the order of the fractions in the program, put ’10’ between each numerator and denominator, and encode the whole list as a base-11 number:

(* Encode a FRACTRAN program in reversed base-11 digits *)

baseConvert[frac_] := 
 {Numerator[frac], 10, Denominator[frac], 10};
baseDigits[fracList_] := 
 Most[Flatten[Reverse[Join[baseConvert /@ fracList]]]];
baseEncode[fracCode_] := FromDigits[baseDigits[fracCode], 11];

(* Sample encoding: Simple adding program {3/2} *)
encodedAdder = baseEncode[addProg]

Output: 475

That seems manageable, right?  But then we have to encode the initial state 72, as well, and this program uses the format (3^(state) * 5^(encoded program) * 199)…

(* Complete encoded initial state for FRACTRAN interpreter *)
(* 3^initial_state*5^encoded_program*199 *)

encodeFracProgState[fracList_, init_] := (3^init)*(5^baseEncode[fracList])*199;

(* Sample complete encoding: Adder, initial state 72 *)

encodeFracProgState[addProg, 72]

Output: 4595528627302514457847822534456305637274485006848124607416562426715142
2223431883566346800946031635723355137742896740401730524435178459281271
4997262424758884123382086634982376119146502282302917806400755768226744
1829230852502546067970809133366591936071662069537535458381098407899190
9230824160175285935800563682805991749525746897352823649995912091981153
9351940155029296875

…Yikes.  Well, because I love you guys so much, I tested this out.  I ran the encoded program through the 84-fraction interpreter, and piped the output to a text file which rapidly blew up to 4.8MB of numbers.  The correct answer pops out after 6,030 lines of gibberish and looks like this:

1331690264838856002293720794993649380710435784303038760424759391768076
4462808226237499620786133558911763356798565240461593352120399425752403
5496560518676882088942416619737846495343367625845408913052935028877605
1678370577471585522503962029887317190284834394621170248161337171206903
0172712146158755426661553931885800712601024170227364957005027071924897
2557777777294510307702117543602421742701115006472526481890849420208766
6735678794793784618377685546875

Which is actually the expanded form of:

3^243 * 5^475 * 149

And you’ll note that 3^243, which is also 3^(3^(3+2)), giving us the answer to our original request to add 3 and 2, buried up in the second layer of exponents.  Whew!

Anyway, as you can see it’s easy to get lost in FRACTRAN despite its apparent simplicity.  It’s really interesting to play with, though, and it’s an odd moment when you realise that these simple lists of fractions are actually capable of some remarkable things.  The weekend is nearly over now and I have to prepare for actual work, but perhaps next weekend I’ll return to this and construct a compiler for FRACTRAN, making it possible to write programs in a higher-level language and squish them into FRACTRAN form.

Or I might start fooling around with different nerd stuff instead, who knows?

 

 

 

 

 

 

 

 

 

 

 

Tagged , , , ,

Book now available via SpringerLink

Good news everyone!  Well, maybe not everyone, but at least people who love academic books about agent-based modelling might be happy about this news.

My book is now available, open access (free, in other words), via SpringerLink.  You can download the whole thing as a PDF or an ebook in EPUB format.  The website is mobile-friendly, too, so if you’re slightly mad and want to read this on your phone, you can certainly do so.

You can also download individual chapters, if you want, but I’d recommend *not* doing this; each chapter pretty much builds on the previous one, so you’ll get more out of it if you read the all the chapters in sequence.

Hardcover copies are not yet available, but I’m told they will be soon, and it seems like you can order print-on-demand softcovers via the Springer website now if you feel like it.

Tagged , , , ,

February update

Screen Shot 2018-02-02 at 09.47.08

I’ve just been sent a preview of the cover for my book, now due to be released in early March — so get your pre-orders in now!

Or don’t, it’s open-access and you can just download a PDF for free when it comes out.  I’ll post here again once it releases for real.

In other news:

  • Our team submitted a funding proposal for a cross-disciplinary network focused on the use of agent-based modelling for designing complex public health interventions
  • I contributed to another proposal, part of which will use ABM to study environmental and policy changes that might encourage more people to take up walking and cycling rather than driving
  • We’re working on a position paper for the public health crowd, to clear up some misconceptions and concerns about the use of ABM in health research
  • Another paper is in the works on a free simulation platform under development
  • Last but by no means least, John Bryden and I have a really exciting paper under review at the moment — watch this space!

I’m also excited about our ongoing work modelling social care provision in Scotland — we’ve just hit a major development milestone.  We’re planning to submit a paper on this first stage in March, and follow that up with further development of the model with help from social care experts here in Glasgow and in Stirling.  We’ll soon start producing  detailed documentation for the model — I’ll post some of those details here in the next month or two.

 

Tagged , , ,

Buddhism and Meditation

As some of you are aware, I’ve been suffering with chronic pain for more than three years now.  It’s been an exhausting, distressing and confidence-shattering experience in many ways, and medical science still struggles to find solutions to this problem, so most days the best I can hope for is that things simply stay stable.  I have to accept that it’s quite possible I will never have a day in my life again where I feel totally healthy and pain-free.

In my own case, pain management experts within the NHS have been extolling the virtues of complementary therapies, most particularly mindfulness practices and meditation (with a side-order of yoga).  In the medical context, mindfulness and meditation have proven very successful in their own right, divorced from their original Buddhist context and presented in a Westernised, clinical framework.  Mindfulness has been shown to increase psychological well-being, reduce symptoms of stress, and crucially, reduce pain.

I have a long relationship with Buddhist thought and practice, having discovered both during a period of mental health difficulty as a teenager.  For a number of years after that I maintained an interest in Buddhist meditation and philosophy, practicing meditation regularly and reading thousands of pages of sutras, commentaries and guides to Buddhist thought.  Then, bizarrely, I moved to a country with a rich Buddhist tradition (Japan) and largely fell out of Buddhist practice.

Now that I’ve been reminded of the benefits of these practices I left behind, I’ve jumped back into mindfulness and meditation recently.  For me, while mindfulness has benefits even outside the Buddhist context, its benefits are much more far-reaching when that context is maintained.  Then mindfulness goes far beyond a calming influence, and becomes a means to re-orient your understanding of self, consciousness, and the nature of mental and physical suffering.  It’s also really interesting to read and nerd out on this stuff.

So, this is all a very long way of saying I’ve been reading a lot of Buddhist stuff again and doing daily meditation.  Along the way I’ve been speaking to some people about it, and realised there are some major misconceptions out there about the nature of Buddhism and meditation.  So partly for those who are interested, and partly to put down in words my own understanding and remind myself of areas that require further study, I’ve decided to put together a little guide to the basics of Buddhist thought and hopefully provide you all some interesting stuff to read along the way.

Before I start all that, if you’re interested in practicing mindfulness and meditation, I can highly recommend the book Mindfulness in Plain English by Bhante Gunaratana.  In my opinion it’s the most readable, detailed, and well-organised guide to Buddhist mindfulness practice available.  If you read this book and follow the advice within it, you’ll have all the tools you need to start an effective and comprehensive mindfulness practice in your daily life.

Also, a disclaimer: all of this represents my own understanding of core Buddhist principles and practices.  Don’t take my word as being 100% accurate.  Some of it is heavily simplified, some of it will have my own misconceptions layered in there.  Take it as one guy’s summary and fill out the gaps with more authoritative sources!

———————————————————-

Common Questions about Buddhism and Meditation

Doesn’t Buddhism involve worshipping the Buddha?  I thought you were an atheist.

I am an atheist, and lucky for me, Buddhists do not believe in a creator god.  The Buddha is not a god, he was a human who spent years struggling to understand his place in the world, and eventually achieved enlightenment, and offered the knowledge he gained to the world.  He’s an object of respect and admiration, but not worship as some of us might offer to figures like Jesus Christ — and worship would be somewhat antithetical to the Buddha’s teachings, which encourage us to strive for enlightenment on our own terms, and only follow those teachings which match our own experiences and critical analysis.

Doesn’t Buddhism require belief in a soul, so that reincarnation can work?  Again that seems antithetical to your scientific mindset.

Not at all.  There are three core concepts, the three marks of existence, that define the Buddhist concept of the world: anicca (impermanence); dukkha (suffering); and anatta (not-self).  In short, everything in the universe is subject to decay and eventual destruction (impermanence), our existence is plagued with feelings of unsatisfactoriness and discontent (suffering), and our concept of a defined, eternal ‘self’ is an illusion (not-self).

To unpack the ‘not-self’ concept a bit, Buddhists believe that we are not defined, separate individuals with a unique essence, or soul.  I, for example, was once a baby — I was tiny, looked really different, had no beard, and my brain couldn’t even properly encode memories.  Yet I still say that baby is ‘me’, despite having a different physical and mental existence in every aspect.  I have an innate tendency to believe that this highly changeable and temporary existence is somehow united by some unique, ineffable essence that makes me, me.  For some of us that essence is an eternal, non-physical soul.

Buddhists deny this, and say that this concept of self is an illusion.  Our existence is actually an amalgam of the five skandhas, or five aggregates: form (matter), or rupa; sensations (feelings), or vidana; perceptions, or samjna; mental activity, or sankhara; and consciousness, or vijnana.  These five aggregates constitute our experience of physical and mental existence, and create the illusion of self to which we cling.  Part of the Buddhist path to liberation is to realise that our existence is a consequence of the constant interaction of these five changeable aggregates, and further, that these aggregates are without fundamental independent existence.

So, the idea of an eternal, unchangeable ‘soul’ is actually incompatible with Buddhist thought.  The sense of self we have is the direct result of constantly changing interactions with our surrounding reality.  There is no eternal soul, and further there is no separate ‘spirit realm’ in which it could exist.

It’s worth noting that the Buddhist concept of these aggregates is broken down even further into extensive detail, but I won’t go into this here.  It’s extremely interesting though so I may do that — again for self-study reasons as well — in a later post.

Wait, hang on a minute — how does that work, don’t Buddhists believe in reincarnation?  How can we reincarnate if we don’t have souls?

No, they don’t.  Buddhists believe in a cycle of life, death and rebirth, called samsara.  Rebirth is not the same thing as reincarnation.  Reincarnation is something we see in other religions, wherein our eternal soul transfers into a new body after death and experiences a continued existence in another physical form.

Buddhism, as explained above, doesn’t accept the idea of a soul.  Buddhists believe that when we die, we die — our experience ceases completely, nothing is transferred beyond death.  When I die, the being known as Eric ceases to exist, my consciousness and self-identity as Eric dissipates, and my body becomes worm food.

However, that’s not the end of the story.  This is where karma, or kamma to stick to the Pali versions of terms I’ve been using, comes into the frame.  Our actions in each existence cause positive or negative kamma, not as some sort of supernatural judge of good or ill will in our beliefs and actions, but as a physical cause-and-effect relationship — if I do a good/bad thing, good/bad results will inevitably develop later.

It is this kamma that continues beyond death.  The Buddhist belief, at its core, is that once we die, the consequence of our kamma is that another birth takes place, and our little bundle of karmic pluses and minuses determines what kind of birth that will be.  This cycle is inevitable, and eternal, unless we are able to break free of this cycle via liberating ourselves from clinging to this world and become enlightened.

This cycle can be hard to conceptualise, so it’s often described using an analogy.  Imagine my life as a burning candle, with the flame representing my consciousness.  Right as the candle is running out, I use that flame to light the next candle.  The next candle lights up right as the old one burns out.  So my consciousness directly causes another, subsequent consciousness to arise in the next life, but my original consciousness burns out — the new one is a different consciousness, existing in a different body (which may or may not be human).  Kamma is what lifts the old candle to the new and causes the new one to light up.

Now there’s obviously a hell of a lot more to kamma, death and rebirth, but that’s the gist of it.  Rebirth is probably the hardest thing for Western Buddhists to get to grips with, and many people (including myself) choose to conceptualise rebirth largely as a reframing of the physical facts of death — so upon death, the matter of my body will inevitably become part of the environment and provide materials and energy for future beings, so in that sense I am ‘reborn’ and contribute to the arising of some future sentient being(s), and this then keeps happening over and over.  That framing is totally fine for many people, and still works alongside the importance of Buddhist ethics, meditational practice and kamma, so the whole edifice hangs together well enough.

Some then later go on to accept the whole picture of life, death and rebirth; personally I’m more willing to buy that picture than anything hinging on eternal souls, infinite punishments in Hell after death for finite crimes in this world, or various other things.  Buddhist rebirth also still accepts death as a real cessation of existence; only kamma continues to the next life, not the same consciousness and there’s no essential essence that transfers over.  But still it’s a pretty major leap.  Most people I’ve encountered online or elsewhere who properly believe in rebirth as Westerners came to that conclusion after years of meditative contemplation, so who knows, I may also decide such a thing a decade from now.

As a point of clarification — yes, the Buddha does say upon reaching enlightenment that he can see all his previous lives.  However, this is not because of direct recollection of those experiences through an eternal soul or directly transferred consciousness, but because he at that point attained perfect understanding of kamma and thus his own karmic history.  So he was able to see all that as an unfolding of karmic cause and effect across unimaginable aeons of time.  That’s the idea as I understand it, anyway.

And for the hell of it, an additional answer to an unasked question — what do I mean by ‘aeons’?  Well, Buddhism talks about a truly immense units of time called a kalpa.  How long is a kalpa?  Imagine a huge cube of granite, measuring 16 miles on a side.  Now imagine every 100 years, a man comes along and gently brushes a silk handkerchief against that mountain of rock.  A kalpa is how long it would take for the mountain to be completely worn away by that bit of silk.  So when Buddhists talk about long cycles of death and rebirth, they’re talking about really really long cycles.

Okay fine, so reincarnation isn’t a thing, it’s rebirth.  How do you explain the Dalai Lama, isn’t he supposed to be a reincarnation?

Well, we’re getting to the limitations of my knowledge here, but technically the Dalai Lama is not a reincarnation but is an emanation, specifically the 14th emanation of Avalokitesvara (Chenrezig in Tibetan, Kannon in Japanese Buddhism), the boddhisattva of compassion.  There’s a lot to explain here, much of which I don’t fully understand, so I won’t attempt to do all that.  I’ll do a bit of explanation, but bear in mind some of the details may be oversimplified or a bit ‘off’.

The gist of it is that Avalokitesvara is an enlightened being that can ’emanate’ into different physical existences.  So each Dalai Lama is essentially a physical manifestation of an enlightened Buddha, who put a bit of themselves into the Dalai Lama to hang about on the Earth and teach us Buddhist things.  After a Dalai Lama dies, the next one appears somewhere else, again as a manifestation of the same enlightened being.

This is why each new Dalai Lama is tested to see if they recall certain objects, places and people from their last incarnation — the idea as I understand it from the Dalai Lama’s own statements is that as an emanation of a higher-level being, that higher-level mindstream (another complicated Tibetan Buddhist concept) retains knowledge of experiences from their last go-round.  So the Dalai Lama’s existence is still compatible with Buddhist concepts of rebirth and not-self — he is not literally the same soul reincarnating around the place, but a manifestation of a larger being that creates different individuals in each emanation.  That larger being retains knowledge of the karmic processes of cause and effect that link each emanation, allowing the Dalai Lamas to remember things from previous incarnations.

Does that really make sense?  For me it’s pretty hard to swallow, perhaps because I’m viewing it from outside its original Tibetan context, where incarnate Lamas (tulku) are a major thing and have been for centuries.  Personally, viewed either way it doesn’t affect my opinion of the Dalai Lama himself, who I’ve had the great fortune to see speak in person at length for several days back in 2004.  He’s quite clearly an exceptional human being, and when he speaks about compassion I very much trust what he has to say, whether he’s an emanation of Avalokitesvara or not; his behaviour, knowledge and practice speak for themselves, independent of any other considerations.

OK fine, so I get that there’s no God, no soul, no self, and suffering is everywhere and we’re constantly reborn into that suffering forever, but honestly that sounds awful.  If all that’s true, then what’s the point of anything?  If we’re all doomed to just suffer and die over and over again, why bother with any actions at all?

This is a very good question, and a really common one.  It’s very easy to misconstrue Buddhist thought as being fundamentally nihilist.  The self is not real, there is no God, death is for-real death for the most part, and suffering is all we get.

But the Buddha very explicitly, and repeatedly, denies this interpretation.  He frames this debate as the idea that reality is absolute and real against nihilism, in which nothing exists.  Buddha’s way is called the Middle Way because it embraces neither sensual indulgence nor strict asceticism, and likewise here it straddles two extremes.  While everything is always changing and dependent on external causes and conditions to exist at all, that does not mean nothing exists; instead, it means that things — including ourselves — do not have an inherent, independent existence.

Imagine, for example, the chair you’re sitting on right now.  That chair was not always a chair, but was once bits of wood, which were once part of a tree, which grew out of some seeds, and so on.  The Buddhist might say that the chair is thus not an absolute, independently existent thing, but is instead the result of various causes and conditions that lead to its current existence as a chair.  What we call a chair is a product of conceptual thought, not absolute reality, because actually chairs are all differing composites of various other things and the events that caused them to exist.

Crucially, however, that does not mean the chair or the bits of wood or whatever don’t exist at all; it just means that we should avoid clinging to the chair as an independently-existing thing and instead accept it as a fundamentally impermanent agglomeration that will eventually decay and cease to exist in its present form.  So, things still exist in Buddhist thought, but are empty — not of existence as a whole, but of independent, absolute existence.  In relative terms — everyday terms — that chair still exists, as do we, and the causes and consequences of our actions every day.

To take it even further, because we ourselves are not-selves and are composed of the five skandhas roaming about the place, we are also interdependent on everything around us.  So in that sense, not-self and emptiness concepts mean we are less separate from the world than we are in traditions that hold we have a non-physical, eternal soul.  My existence is dependent upon, and intermingled with, the causes and conditions that also make up everything else, so in that context it’s extremely important I be mindful of my actions as I am also not absolutely existent independently, but am part and parcel of the swirling mess that is samsara.

OK right — I kind of get it.  Or maybe not.  But if I take it as read that stuff still exists, and things I do actually matter, then what do I do to avoid suffering?  If suffering is inevitable, as it sounds like it is, then what can I possibly do to not be miserable?

Now we reach the core of Buddhist actions, rather than just philosophising.  In his very first discourse after reaching enlightenment, Buddha laid down the core of Buddhist practice: the Four Noble Truths, and the Noble Eightfold Path.  The Four Noble Truths are (roughly) as follows:

  1. The Truth of Suffering — there is suffering (dukkha)and it is everywhere.
  2. The Truth of the Origin of Suffering — suffering comes from attachment, or grasping/clinging to sense-pleasures, desire for existence, or desire for non-existence.
  3. The Truth of the Cessation of Suffering — suffering can cease when we give up these attachments.
  4. The Truth of the Way Leading to the Cessation of Suffering — the way to end suffering is to follow the Noble Eightfold Path.

So all is not lost — yes, suffering is everywhere, and now we know that fundamentally suffering arises due to our desire to cling to aspects of existence even when existence is ultimately impermanent and constantly changing.  But we can end our suffering through the Noble Eightfold Path, which the Buddha conveniently lays down shortly after this:

  1. Right View — basically, accepting the Four Noble Truths, and believing that there’s a way out of all this.
  2. Right Resolve — renouncing material attachments and devoting oneself to a more contemplative life.
  3. Right Speech — don’t lie, don’t speak ill of other people, and don’t say things that are not of benefit to others.
  4. Right Action — don’t kill people, no stealing, no sexual misconduct.
  5. Right Livelihood — make your living without harming other sentient beings or doing other bad things.
  6. Right Effort — exert your will to avoid unwholesome states of mind that spawn ill will, desires for sense-pleasures, etc.
  7. Right Mindfulness — cultivate awareness of existence as being impermanent, full of suffering, and devoid of self (anicca, dukkha, anatta).
  8. Right Concentration — develop a ‘one-pointedness of mind’, or the centring of consciousness on a single object, without loss of focus.

So the way to end suffering is to lead an ethical life, refraining from absorbing ourselves in materialistic sense-pleasures and from causing harm to sentient beings, while also cultivating a concentrated, mindful consciousness.  In so doing we improve our kamma, reduce our attachment to the impermanent world around us, and develop experiential insight into the nature of mind and reality.  Eventually, if we do well enough, we can break the endless cycle of samsara and enter nirvana (nibbana), and we no longer suffer and instead experience unimaginable bliss.

I feel it’s important to note again here that in the context of the Buddha’s original sutras — called the Pali Canon and the core texts of Theravada Buddhism — the Buddha can’t help you with all this, as he’s not an interfering Christ figure or God.  Praying to him won’t do anything.  Ultimately the responsibility for your enlightenment — or lack thereof — rests with yourself and your own practices.  There are no supernatural authorities to reward or punish you; instead you simply reap the results of your good or bad actions through kamma.

This is not necessarily the case in Mahayana or Vajrayana Buddhism, where Buddhas and Boddhisattvas are seen as enlightened beings that do in fact try to help the rest of us mooks achieve enlightenment.  But even in the most ritual-laden Tibetan practices, still these ‘deities’ are seen as ultimately symbolic of qualities we wish to cultivate in ourselves, rather than as real gods/goddesses or beings that can intercede directly in our affairs.

Wow, finally — that Right Mindfulness/Right Concentration stuff sounds like meditation to me, at last!  You started all this off with that and haven’t given me any details at all yet.  So get to it.

Sorry about that.  I like talking about Buddhist philosophy so I got a bit caught up.

Right, so from the start here I should say I’m again focusing on the Pali Canon and the original sutras from the Buddha.  So essentially I’m talking about meditation as practiced in Theravada Buddhism, which are practices also core to every Buddhist tradition.  There’s tons of other types of meditation in Mahayana and Vajrayana (Tibetan) Buddhism, but they differ widely and would make this post even more ridiculously long than it already is.

Right Mindfulness in the context of the Noble Eightfold Path can be cultivated via what we now call insight meditation (vipassana).  Insight meditation is about developing awareness in ourselves of the three marks of existence: impermanence, suffering, and non-self.  The way this is done is deceptively simple, and it all starts with watching the breath.

This means that we sit in a comfortable, stable posture, and simply observe our breathing as it rises and falls.  Other thoughts will rise constantly, but rather than attend to them or explore them, we simply note their arrival and return to watching our breathing.  Physical pains may develop as we sit as well, and we do much the same: note the sensation, observe it as it rises and falls, don’t become involved, and allow it to pass.  As we do this over weeks, months and years, eventually we find it easier to redirect our attention away from the various thoughts and sensations that come and go, and we begin to understand that all mental phenomena — and indeed all phenomena — are like this.  Things come, and they go, and if we gently allow that to happen and return to focusing on the breath, we likewise train our minds to stop attending so much to impermanent, momentary thoughts and sensations.  In this way we develop mindfulness — a clear awareness of impermanence (anicca), direct experience of the way our mind clings and thus suffers (dukkha), and knowledge of not-self as we see our mind as it really is, composed of a mess of thoughts and sensations with no permanency or independent existence (anatta).

Now, having success with vipassana also requires that we develop concentration, or the ability to focus on a single object within our minds.  This is the other main type of Buddhist meditation, called samatha (sometimes translated as ‘calm abiding’).  In samatha, conveniently, we also can use the breath as an object of focus, but instead of trying to develop insight into the arising and passing away of phenomena and observing this process, we focus on developing single-pointed concentration on the breath.  If other thoughts arise, we note them, immediately drop them, and return to the breath.  Over time, we can maintain this focus longer and longer, and enter states of deepening concentration known as the four jhanas.  I won’t go into these much but will just quote the Buddha here:

[i] Here, the monk, detached from sense-desires, detached from unwholesome states, enters and remains in the first jhana, in which there is applied and sustained thinking, together with joy and pleasure born of detachment;
[ii] And through the subsiding of applied and sustained thinking, with the gaining of inner stillness and oneness of mind, he enters and remains in the second jhana, which is without applied and sustained thinking, and in which there are joy and pleasure born of concentration;
[iii] And through the fading of joy, he remains equanimous, mindful and aware, and he experiences in his body the pleasure of which the Noble Ones say: “equanimous, mindful and dwelling in pleasure”, and thus he enters and remains in the third jhana;
[iv] And through the giving up of pleasure and pain, and through the previous disappearance of happiness and sadness, he enters and remains in the fourth jhana, which is without pleasure and pain, and in which there is pure equanimity and mindfulness.

Buddhism being Buddhism, of course, this is far from the end of the story.  There are four more jhanas beyond those, and the whole system is described differently in some Mahayana traditions and in Tibetan literature, so there’s tons more to discover on both main varieties of meditation.

Now these two meditation methods may seem rather closely related, or even hard to distinguish, and you’d be quite right — in fact in the early Buddhist canon the two seem to be intimately connected.  There’s a lot to talk about on this topic, which I won’t bore you with but instead will direct you to this free book on the close relationship between samatha and vipassanaA Swift Pair of Messengers.  Note however that this book assumes significant familiarity with meditational practices and Buddhist terminology, so it’s not recommended for beginners.

If you want to start practicing vipassana, again I’d direct you to Mindfulness in Plain English which is entirely about this kind of mindfulness meditation and is extremely clear and good.  Helpfully, the same author wrote a follow-up about samatha and the jhanas called Beyond Mindfulness in Plain English, which is equally excellent.

Right, so there are two main kinds of meditation, closely related but with different ultimate goals.  But aren’t they both, ultimately, about withdrawing from the world?  In meditation are we not just hiding from reality?

No, completely the opposite.  Mindfulness and meditation are about creating awareness of the world, how it functions and how our minds perceive it and relate to it.  In deep states of meditation, one understands how our normal states of mind are constantly polluted with unwanted thoughts, desires, and constant noise.  Buddhists call this the ‘monkey mind’ — the tendency of our minds to leap heedlessly from thought to thought, like monkeys cavorting in the jungle canopy.  We never settle, never allow ourselves to perceive anything in and of itself, but instead coat everything with conceptual thought and wallow in endless diversions.

When you start meditating, it won’t be long before you see this ‘monkey mind’ in action and realise that you are completely insane in a way you’ve never noticed before.  Focusing on the breath sounds simple, and turns out to be almost impossible.  Our brains leap from tree to tree, never allowing us a moment’s peace.  You might snap back to the breath and realise you’ve been stuck in a sexual fantasy for the last five minutes without even remembering how you got there.  Or you may try to count your breaths and never get past three as the distractions come so thick and so fast.  You may even discover — this is my problem — that you can have two or three independent, completely fleshed-out trains of thought going simultaneously, and it all seems like an unstoppable cacophony.

But it’s important to be gentle with yourself, and simply allow these things to pass away, and say to yourself ‘OK I was distracted, that’s fine — now back to the breath’.  The simple act of redirecting your attention back to the meditation object is mindfulness!  You are being aware of your mind’s constant straying, and consciously moving back to moment-to-moment experience.  Keep doing that, keep redirecting yourself, and eventually that redirection will become easier and easier to achieve.  Each time you do it, you’re retraining your mind and developing a new habit: instead of getting lost in conceptual thought, retreating from the world and the realities of mind and life, you will redirect yourself back to experience.  Eventually, meditation will allow you to carry over this training into your everyday life, and your awareness of every moment of existence is enhanced.

So, ultimately meditation is not about retreating from reality.  Meditation is about being present in reality in a way we normally never are.  We train ourselves to experience moment-to-moment existence as it really is, observe the comings and goings of our thoughts and the world around us, and become more aware of our reality than before.

OK that sounds a bit more positive.  But what about compassion and loving-kindness and all the stuff the Dalai Lama talks about?  This all seems really inwardly-focused.

Well, I wouldn’t necessarily agree with that — cultivating insight in one’s own mind naturally helps us to perceive the impermanence and suffering present in the world for everyone else, too.  As we become aware of our own crazy ‘monkey minds’ we understand how everyone suffers the same thing, and over time that helps us build compassion.

But yes, there are meditation methods specifically oriented around metta, or loving-kindness.  This is a Buddhist vision of compassion in which we experience pure, unconditional kindness toward all sentient beings; it’s often described as similar to the love a mother feels for her child.  In developing metta within ourselves, we attempt to give that same love to any and all sentient beings on Earth, whether they are friends, enemies, business competitors, or whatever.  Tibetan Buddhists like to say that, in all the hugely long kalpas of the universe’s history, all beings have at some point been our mother, so we should treat them with the same unconditional love and respect we do for our current mothers.

I’m truly becoming a broken record at this point, but our good friend Bhante Gunaratana wrote another helpful book on metta meditation, which again I highly recommend: Loving-Kindness in Plain English: The Practice of Metta.  Again this is well worth picking up if you want a readable, approachable introduction to metta.

In essence, this kind of practice revolves around entering a calm, meditative state, and then beginning to imagine you are offering pure love and compassion to others, but always starting with yourself.  This can take different forms depending on the practitioner; you may mentally recite a series of well-wishing phrases to yourself, then to a close friend, then your whole family, then your enemies, then to the entire world, for example.  Or it may involve visualisations, like fondly remembering a moment of pure compassion and then imagining that radiating outward from your body to encompass the entire planet.  However you do it, the idea is to bring that genuine feeling of compassion into your mind, then imagine giving that out to the world.  Over time, this practice trains your mind to offer compassion as your default response to other people.

One kind of metta meditation I particularly like is tonglen, which is a Tibetan practice.  Tonglen, like many Tibetan practices, centres on visualisations which is a method that I find easier to focus on.  Again there are many variations, but a common method of tonglen is to imagine your own suffering as piping hot black smoke emanating your body and mind.  As you breathe in a deep breath, that black smoke enters your body and is transformed into cool, clear vapour.  Do this a few times, then imagine a wider group of people, like friends or family, and again take all their suffering into your lungs, and breathe out only cool, clear vapour.  Then you can expand further and encompass the suffering of all sentient beings.  In this way, tonglen is meant to help us face suffering in ourselves and others, and be willing to take it into ourselves and offer something positive in return.  Tibetans particularly endorse this practice for helping us deal with ill health, or even terminal illness in ourselves or others.  In a way it helps us ‘toughen up’ and develop the mental strength to absorb bad things selflessly and compassionately.  If our dearest friend is dying, we might do tonglen while imagining their terrible situation, in order to build our strength so we can face it with them.

Metta is a very positive meditation experience for most people, and in my experience can help us be more compassionate toward ourselves as well as others.  Some might find it easier to get started with than vipassana or samatha, as well, since it has a more emotional, everyday focus.

OK great, thanks for that.  That’s quite enough for now.  Why did you write so much of this, anyway?

I felt maybe someone might find it interesting, I guess, but mainly it’s for me.  Buddhism is complex and studying it alone is difficult, so I felt the urge to get these core ideas down somewhere for my future reference.  As time passes and I get some more structured Buddhist experience and tuition, I’ll come back to this and adjust anything that doesn’t convey things well or introduces oversimplifications or mistakes.  Eventually I’ll probably write further posts in the future exploring some more detailed aspects of Buddhist thought.

In any case, I hope someone found this interesting, and perhaps even might be inspired to try some mindfulness/meditation practice.  As I said, even divorced from the Buddhist context these practices are often very helpful for people.  And if you try them and do find Buddhist philosophy intriguing, I hope this gives you a good idea of the basics.

So let’s say I read this and I do find it interesting, where do I go from here?

As I mentioned above I’ve focused on core ideas from the Pali Canon, the Buddha’s original teachings and the core texts of the Theravada Buddhist tradition.  This text alone is rather huge — you can find authoritative, complete translations by Bhikku Bodhi in hardcover in five exceedingly large volumes (one of them exceeds 2000 pages!).  There’s a ton to study there for a start.  If you want a great introduction to the Pali sutras with insightful commentary and a reasonable page count, pick up In the Buddha’s Words, also by Bhikku Bodhi.  If you decide to get deep into sutra study, you can follow Bhikku Bodhi’s extremely thorough lectures on all the Middle-Length Discourses here.

Update 29/12/17: For a bit more challenging reading on Theravada, check out this thorough translation of the Visuddimagga, The Path of Purification, in PDF format (853 pages!).  This is an extremely in-depth meditation manual and explanation of the Abhidhamma, often referred to as the core of ‘Buddhist Psychology’.  Highly recommended for philosophers, but it helps to have read the Pali Canon first.  If you’re feeling brave, throw in the Vimuttimagga, another manual on the Abhidhamma, available as a 433-page PDF and in numerous other formats.  Finally, if you’re feeling experienced enough in vipassana to tackle an 800+ page manual on the subject that digs deeply into the Pali Canon and related commentaries, you can try A Manual on Insight Meditation.

Beyond Theravada, there’s the Mahayana tradition, the ‘Greater Vehicle’, which embraces later teachings that focus much more on the ideal of the boddhisattva, or someone who reaches enlightenment but delays entering nirvana to help other sentient beings end their suffering.  Famous sutras to read in this tradition include the Heart Sutra, the Diamond Sutra and the Lotus Sutra.  The Lotus Sutra in particular presents a pretty major reconceptualisation of the Buddha(s), and is rather huge to boot, so should keep you busy for awhile, particularly if you dig into the nearly endless commentaries.  If you want to be a completist you can read English versions of the eighty-five volumes (!) of the Taisho Tripitaka.  For more approachable summaries of what Mahayana is about, Thich Nhat Hanh offers a well-written but still dense summary of his view on the tradition in his book The Heart of the Buddha’s Teaching.  The Dalai Lama’s books also cover some key concepts of the Mahayana tradition, particularly loving-kindness/metta.

Zen Buddhism is a Mahayana tradition, and probably one of Japan’s most famous exports.  It’s also the most popular form of Buddhism in the West.  Zen is known for its intense focus on zazen (sitting meditation), some rather crazy Zen masters, and mind-twisting koans that challenge the eager student’s perceptions of dharma.  If you want to read a classic of Zen thought, check out Dogen’s Shobogenzo, which is huge and dense but considered a masterpiece and the central text for Soto Zen.  For a more accessible intro to Zen, The Three Pillars of Zen is probably the best and most thorough.

Update 29/12/17:  Shasta Abbey Buddhist Monastery offers free PDFs of a number of books on Soto Zen practice here.  The page is helpfully laid out and suggests which books are best for beginners and for advanced practitioners.  The beginner books I’ve had a look at thus far (Zen is Eternal Life, Roar of the Tigress I, Serene Reflection Meditation) all seem to offer extensive introductions to both Soto Zen beliefs and practices, so do take advantage of these if you want a free intro to Zen thought.

After Mahayana comes Vajrayana, Tibet’s unique mixture of Mahayana Buddhism and yoga tantra.  Tantra came to Tibet back in the 8th century or so and melded with Mahayana practices and native Tibetan Bon shamanistic practices to produce a ritual-heavy esoteric tradition, characterised by heavy use of mantras, tons of ritual objects, and lengthy, complex tantric practices featuring detailed visualisations.  Vajrayana can be tough to get a grip on as it’s an esoteric tradition, meaning practitioners aren’t really supposed to talk about the tantric practices with non-practitioners, and you won’t normally be taught any of them unless you’ve received an initiation from a lama.  In order to be initiated you may be asked to complete the ngondro preliminary practices, which consist of things like 100,000 prostrations, 100,000 mantra repetitions, and 100,000 of other stuff besides, although from what I understand you can sometimes start with tantra immediately once you start doing ngondro.

However, you can get a great historical summary of Tibetan Buddhism and a detailed survey of practices in the four major Tibetan traditions in Introduction to Tibetan Buddhism by John Powers.  Tibetan Buddhism is also gaining lots of Western adherents these days, largely thanks to the global adoration for the Dalai Lama, so various cities around the US and Europe actually have proper Tibetan monasteries now.  Like any Buddhist group they’ll be more than happy to have you join in, so if you read Powers’ book and find it interesting, look up your local Tibetan group, ring them up and ask to join a meditation or puja.  Some Tibetan sects also offer teachings online, or even tantric initiations/empowerments via webcam.

That basic idea works for any tradition, really — if you like what you read and want to try meditation under proper instruction and with encouragement from a group, find one in your area and give them a ring.  Just be careful to mind how you go, as some unscrupulous types do take advantage of new maybe-Buddhists seeking answers and try to suck them into some cultish stuff (looking at you, New Kadampa Tradition and Diamond Way Buddhism — don’t join these folks).  Remember that Buddhist Dharma teachings should be offered free or at cost, and Buddhists aren’t really into converting people and should welcome you to just sit and meditate in peace, whether you’re interested in Buddhist practices or not.  Retreats might cost more money, generally to cover accommodation and food, and the attendance of the teacher who may be travelling quite some distance.

A few things to remember, if you do go to a group — if you borrow or are given any Buddhist literature while you’re there, don’t leave it on the floor, step over it, or put things on top of it, this is disrespectful.  Put it on a table or shelf with nothing resting on top of it.  In Tibetan practices, don’t point your feet towards the altar or the lama either — be mindful of this if you need to shift positions while meditating, for example.  And of course, try not to disturb anyone who’s meditating, and please turn off your phone!

If you ultimately decide you want to be a Buddhist and do something ‘proper’ to mark that commitment, you can do what’s called ‘taking refuge’.  In doing this you pledge yourself to take refuge in the Three Jewels — the Buddha, the dharma (the Buddha’s teachings), and the sangha (the monastic community).  Most traditions will have some kind of ceremony for this.  This means you decide to trust that you can reach enlightenment as the Buddha did; that you understand the Four Noble Truths and will follow the Noble Eightfold Path; and that you will trust in those already following that path.  As usual, if you do this and then don’t practice or fall out of Buddhism, there aren’t any supernatural judgments awaiting you — it’s up to you to make good on that promise, or not.

It’s worth noting that major Buddhist figures like Thich Nhat Hanh and the Dalai Lama have stressed that it’s not really important to ‘convert’ to Buddhism.  You’re a Buddhist once you start following Buddhist practices and teachings, you don’t have to prove that to anyone, since no god or gods are watching or judging you anyway.  You can even be a Buddhist while following other religions, though bear in mind some fundamental Buddhist concepts are quite at odds with certain religions (might be hard to still be a Christian if you don’t believe in a creator God or an eternal soul, for example).

Anyway, that’s more than enough for now.  If any of you decide to try meditation, good luck, and if any of the Buddhist philosophy stuff appeals to you, I hope you find some of these links and books interesting.

Tagged , ,

MRC-funded PhD studentships in Agent-Based Modelling

Here at the MRC/CSO Social and Public Health Sciences Unit at the University of Glasgow we recently announced a whole host of PhD topics for students looking to join us on our interdisciplinary quest to improve public health and reduce health inequalities.  The studentships are funded by either the Medical Research Council (MRC) or the University of Glasgow, and cover the full cost of tuition fees and provide a stipend.

Students who have a Masters-level degree already can jump right into the three-year funded PhD, or if you’re fresh out of undergraduate education you can join a four-year programme and get your Masters in the first year.

In the Complexity and Health Improvement Programme we are offering up a few potential topics on the application of agent-based models to public health challenges, supervised by myself, Rich Mitchell, Mark McCann, and Umberto Gostoli.  If you’re keen to get involved in this relatively new area of work in public health, do read through the topics and get in touch with the Programme Leader (and Unit Director) Laurence Moore as soon as you can, in order to discuss your proposal.

Tagged , , ,

Uncertainty Quantification workshop in Cambridge

Finally got confirmation that I’ll be attending the first of several workshops on uncertainty quantification at the Isaac Newton Institute in Cambridge in the second week of January.  The workshop — Key UQ Methodologies and Motivating Applications — has a great lineup of speakers, including Prof Tony O’Hagan from Sheffield, Prof Michael Goldstein from Durham, and good friend Prof Jakub Bijak from Southampton.

This is far from the only programme running on this topic — the INI is putting on a series of workshops and other programmes on UQ all the way through June next year!  The main UQ programme page has a summary of upcoming events.

Anyway, really looking forward to this — if the topic is of interest to you, be sure to sign up for one of the other workshops during this UQ season at the Institute.

Tagged , , , ,

My book will be released soon — and it will be open-access

Good news, open science fans — my upcoming book from Springer is now in editing/typesetting, and on track for a spring release under a Creative Commons with Attribution licence.  This means you can download, share, adapt and modify the work however you see fit, so long as you cite the original and link to a copy of the licence.

I have to take a moment here to thank the MRC/CSO Social and Public Health Sciences Unit, my new home, for supporting open science and widening the audience of this book.

Springer is keen to get this moving along so they’ve put up a website for the book here!   You can even pre-order it, if you want.

Tagged , , ,

Some light reading recommendations

So I just handed in the final draft of my upcoming book for Springer’s Methodos Series, which is about the application of agent-based modelling techniques to the social sciences, with some specific applications to demography.

I thought I’d share two other books related to this topic that just came out recently, both of which are open-access and freely downloadable as PDF or epub ebooks:

Model-Based DemographyEssays on Integrating Data, Technique and Theory by Thomas K Burch.  Tom has been in demography a long time (six decades, in fact), and has brought together this volume based on his methodological critiques of demography in recent years.  I very much share his view that demography is far more than applied statistics, and that the field has a lot to say about the development and evolution of society and the behaviour of those within it.  If you’re interested in a detailed examination of demography as a science I can highly recommend this book.

Agent-Based Modelling in Population Studies: Concepts, Methods and Applications edited by André Grow and Jan Van Bavel.  This is a collection of papers on agent-based modelling in population studies presented at the University of Leuven in 2014 — and, full disclosure, I’m an author on one of the papers so my views here may be biased!  Having said that, I think this weighty tome (over 500 pages) offers some fascinating perspectives on the use of ABMs to study population, as well as some interesting examples of the methodology in action.  

As for my book — it should appear in early 2018, from what I understand — I’ll post here of course when Springer sets a final publication date.

 

Tagged , , , ,
Advertisements