Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Sunday, July 3, 2016

Random discrete distributions (decks) in game development

Randomness plays an important role in many games. Enemies appear randomly, or events occur at randomly spaced intervals. A relatively easy way to think of random events in a game is as a deck of note cards. The probability of a particular word being drawn is a function of the number of cards in the deck with that word on them, and the total number of cards in the deck.

This approach is called a discrete distribution.

In the case of circle the aardvark, I used a discrete distribution to determine whether an aardvark or an enemy spawned, and how long each character should remain on the screen before timing out and going away.

The game is written in Monkey X, but the code in this post is in Python.
You can find the Monkey X version here.


Read on for details

Monday, March 31, 2014

Python Numpy Munkres Hungarian matching algorithm

I didn't make this, but there is a numpy implementation of this algorithm available through scikit-learn in sklearn/utils/linear_assignment_.py . I'm glad I found it, as it seems to be about 4 times faster than the munkres package from PyPI (on which it is based, and which is the python implementation that comes up the most on google).

Reference:
http://stackoverflow.com/questions/1398822/the-assignment-problem-a-numpy-function

Thursday, August 22, 2013

Instantiating generic BioCyc (MetaCyc) reactions

Most databases of metabolic reactions include reactions that are generic. That is, some of the reactants and/or products are not specific molecules, but classes of molecules. This is a wonderfully abstract way of representing a large number of chemical transformations with a simple notation.
An example of a generic reaction is the CARBOXYLESTERASE-RXN in MetaCyc, which has the formula:  a carboxylic ester + water <=> an alcohol + a carboxylate + a proton

As useful and elegant as generic reactions are, they present a problem for people trying to generate mathematical models from a database.  The problem is that most mathematical modeling frameworks do not know how to deal with generic reactions.*  So reactions in mathematical models should generally be balanced and unambiguous.

Latendresse et al. (2012) describe a strategy for generating specific reactions ("instances") from generic reactions from the BioCyc family of databases.  Their strategy is to enumerate all possible combinations of reactants and products for a generic reaction, then check for mass balance. The set of mass balanced instances is further filtered by removing instances that include a reactant or product that appears in more than one balanced instance, as such instances are regarded as ambiguous. They also treat polymerization reactions as a special case and handle them differently from other instantiations.

Sunday, July 7, 2013

A python port of the nonlinear optimization method of Hooke and Jeeves

I ported the Hooke and Jeeves algorithm to Python from C.  I ported it because I've been writing a Python toolkit for kinetic modeling of biochemical systems.  There are certain modeling methods I want to use that are difficult or impossible to do with COPASI (I'll try to write more about this in a later post), so I'm making a library specific to my needs.  I found the Hooke and Jeeves parameter fitting routine from COPASI to be the most useful for fitting parameters to my current system of interest (Nelder-Mead, and Levenberg-Marquardt also seemed to work pretty well, the other ones not nearly as much).