Java Kohonen Neural Network Library – bug

posted by Karsten

I’ve been preparing a 3 week seminar on AI for my day job at Victoria University of Wellington. In this seminar I want the students to see a few different AI methods in action before they code up some small AI examples.

One of these examples is a Kohonen network for organising colours. Instead of coding from scratch I decided to use the Java Kohonen Neural Network Library which can be found at http://jknnl.sourceforge.net/. It provides all the functionalities I need for my seminar, but when I had made the examples the code simply didn’t run. None of the neurons in the “brain” changed values, even though when running it with comments turned on it clearly was changing the values internally. The setup I was using was a network which used the WTALearningFunction, similar to the example on the the library website.

The initial thought was that I was using the library incorrectly, however I couldn’t find any other internal data structures storing neurons. I then went into the source of the library to investigate and seemingly the code was correct. It was only when I realised that the method that changes the weights was changing the weights on a cloned version of the neuron weights that I spotted that the changes where only stored locally!

To rectify this I had to add the following line at line 261 in WTALearningFunction.java:

I have sent an error report to the project team. I probably spend more time debugging this issue than it would have taken me hard-coding an example, but at least there is an output for the community 😉


Create a new Game Project

posted by Karsten

I have had a few questions in my “Begin Programming” course on FutureLearn.com on how to start a new project when you for instance want to create another version of the game.

This used to be quite a process in the old days in Eclipse, but under Android Studio it is actually much simpler than one would expect. You just make a copy of the folder with the game (e.g. ctrl-c, ctrl-v on Windows). Rename both folders to something appropriate, and you can then open the new folder in Android Studio, and everything should be starting up correctly.

You now have the project in two different folders, and you can start making your fancy new game 😉

P.S. I usually rename both folders for two reasons. Firstly I like to have an original folder with the original code, and secondly I want to be sure that there are no “absolute” links in any of the folders that might disturb functionality in any of the other folders (it is unlikely but I just prefer belt and braces)

P.P.S. If you want to do this as a “pro” you would normally use a versioning system, such as git or subversion. However this is somewhat out of scope of the “Begin Programming” course, but might be something you’d like to look at for further studies…


Google Play Services

posted by Karsten

I am currently working on integrating Google Play Services (GPS) into one of my games. I have run into a problem. I could relatively easy get the app to login, and everything is almost done.

I need to be able to register the SHA1 of my release key, but when I try to link a new app through the Google Play Services dashboard then I am stuck. Because an unexpected error occurs:

Google Play Services error

I have been trying for a couple of days now without success. I have also tried making new apps and linking other apps, but it just won’t link anything.

I just hope they fix this error soon!!


Begin Programming: Solution to week 2’s challenges

posted by Karsten

Comments Off on Begin Programming: Solution to week 2’s challenges

SPOILER ALERT: This post will show you how to move the ball at double speed, and how to move the ball directly to where you clicked on the screen.


There are several ways to move the ball at double speed. I think it is best to do this in the updateGame method:

26-02-2015 13-44-09 updateGame

Here the position of the ball is changed depending on the speed of the ball. If that is multiplied by two, then it will move faster:

26-02-2015 13-44-52 updateGame after

This would do the trick! Notice that I don’t need brackets because the precedence of the *’s mean that they will be performed before +.

However there is a small problem. Now the mBallSpeedX and mBallSpeedY actually aren’t reflecting pixel/sec any more. If you want to keep that then you need to multiply the evaluation of mBallSpeedX and mBallSpeedY, for instance in actionOnTouch:

26-02-2015 13-49-28 actionOnTouch

You could change that to:

26-02-2015 13-48-46 actionOnTouch after


Notice I use brackets to enforce that the – operation happens before the *.

You would also need to change in actionWhenPhoneMoved to:

26-02-2015 14-12-22 actionWhenPhoneMoved


To change the ball’s behaviour to move instantly is actually simpler. In actionOnTouch you would need to move the ball directly to the x and y position of the touch. You do that this way:

26-02-2015 13-50-29 actionOnTouch move



We prefer discussions about the code in FutureLearn, so I will not publish comments here. I hope you understand 😉


Installing Android Studio on Ubuntu 64bit

posted by Karsten

Installing Android Studio on Ubuntu 64bit is difficult at the moment, at least on Ubuntu 12.04 LTS. I personally spent a lot of (frustrating) time getting it working. To eliminate some of this frustration for others I record the steps I had to take here.


First we need to install Java JDK 7. This is different to how this is done on any other platform. First we need to download the latest version. Go to Oracle’s Java website. Here you should scroll down to the “Java SE 7uXX/XX” part. XX/XX will be two numbers that indicate the sub-version number. As long as it says 7u then things should be ok. Press the DOWNLOAD button that is located under the JDK option

Screenshot from 2015-01-09 10:51:23


Go down to the box with the title “Java SE Development Kit 7uXX”. Accept the license agreement, and download the Linux x64 tar.gz version. My browser downloads to ~/Downloads, you need to find out where your’s store downloaded files.

Screenshot from 2015-01-09 11:07:34


Now it is time to install Java:

First we need to uncompress the downloaded file (remember to substitute XX for the actual sub-version you downloaded (e.g. in my example it is 71). Open the terminal (Ctrl + Alt + T) and run

tar xzvf ~/Downloads/jdk-7uXX-linux-x64.tar.gz

Then move the new folder to a suitable place for this sort of program

sudo mv jdk1.7.0_XX/ /usr/local/java/

Now we have to notify Ubuntu that  we want to use this Java version using the update-alternatives application. (Remember to substitute the XX with the actual version number)

sudo update-alternatives --install /usr/bin/java java 
/usr/local/java/jdk1.7.0_XX/bin/java 1 
sudo update-alternatives --install /usr/bin/javac javac 
/usr/local/java/jdk1.7.0_XX/bin/javac 1 
sudo update-alternatives --install /usr/bin/javaws javaws 
/usr/local/java/jdk1.7.0_XX/bin/javaws 1
sudo update-alternatives --config java 
sudo update-alternatives --config javaws

Test that it works by running

java -version
java version "1.7.0_XX"
Java(TM) SE Runtime Environment (build 1.7.0_XX-XXX)
Java HotSpot(TM) 64-Bit Server VM (build 24.XX-XXX, mixed mode)


javac -version
javac 1.7.0_XX

If you get similar responses then Java has been set up succesfully.

On a 32 bit machine it is as simple as running

apt-get install lib32ncurses5 ia32-libs

However on a 64 bit Linux OS Android Studio will be using 32bit libraries, even though you are running 64 bit. So in order to enable this we first need to install those. This is where it gets tricky. Because there are several ways to do this, depending on you version of Ubuntu. Furthermore if you get it wrong the error messages from Ubuntu and Android Studio will be less than useful!

First we need to add the i386 architecture. First try

sudo dpkg --add-architecture i386

If that gives an error message, then test that you OS is a multiarch system. If

ls /etc/dpkg/dpkg.cfg.d/

gives the output


then it is and you can run

sudo sh -c "echo 'foreign-architecture i386' > /etc/dpkg/dpkg.cfg.d/multiarch"

and you have now added the i386 architecture.

If it isn’t a multiarch system, I don’t know how to do this, because I haven’t seen it before. Please make a comment about which Ubuntu system you are running and I’ll look at it…

Now we can install the 32bit libraries

sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386

After this we need to install the lib32z21 libraries, which aren’t provided in the i386 architecture files. This library enables working with images within Android Studio. Run

sudo apt-get install lib32z1

and this should be set up.

Android Studio

Go to the Android Studio download site and follow the instructions there. It might (still) not be able to find the Java JDK (it happened on two of our test systems), but just point to /usr/local/java/jdk1.7.0_XX manually in the box they provide.

You should now have a fully functioning Android Studio. If you want to debug on a phone you need to go to the site and follow the instructions there.


BattleRunner in C# running C++ robots

posted by Karsten

There is a nice example in C# where you can run battles automatically. This works nicely with both Java and C# robots, however to run C++ you need to do a few steps before it will run.

First navigate to your [robocode]\libs\control folder and open BattleRunnnerSample project in VS (spelling mistake intended). Once it has finished loading you need to open the solution’s properties. Right click the solution (not the project) and click Properties.



Change the Target Framework to 4.5.




After that you are good to go. Try and change one of the robots to “myCppRobot.CplusplusTestRobot” (namespace.classname) and run the code. You’ll see the robots fighting. If you want to speed things up you can change the visibility of the engine to false.



Robocode: C++ Robots

posted by Karsten

In my Evolutionary Computation class I have a (great) course work where the students develop Evolutionary Killerrobots in robocode. This is a great game in Java with a port to .NET. The website is well-documented for Java/C#.

For people who wants to develop robots in managed C++ the help is a bit old. This is intended as a guide, I have made the screenshots in Visual Studio 2013.


Begin by creating a project in VS.


Then select a C++ CLR Class Library project and give it an appropriate name. Press ok.



Once it has loaded, right click on the project (not the solution)




Select properties



Select “Common Properties”. Press References, and then “Add New References”



Click Browse to the left, and then Browse to the right.



Locate your robocode.dll file. It is usually in c:\robocode\libs. Press Add.



It will now show up under References.



Under “Configuration Properties” press General. Select “Common Language Runtime Support”. From the drop down choose “Safe MSIL Common Language Runtime Support (/clr:safe). Press ok



Go to the robot .cpp file. In my example it is called TestRobot.cpp



Create a robot. I always extend AdvancedRobot, because it gives me more possibilities. Build the project.





To test out the robot. Open Robocode and under options click Preferences.



Open the “Development Options” and press Add



Locate the folder where the robot is build. I only add Debug here, because I am only testing the robot. If I build a release I would also need to add the release build folder.



You can now find the robot when you set up a new battle.



Emulator vs. Real device

posted by Karsten

Comments Off on Emulator vs. Real device

When setting up a development environment for Android apps, one of the more tricky steps is to get a place to run and debug the code. Setting up an emulator should be a relatively simple process, but through my FutureLearn course I have experienced that it really isn’t. The emulator is very intensive on the computer hardware, it is at the best of times very slow, and even with the smallest setting which isn’t the norm the emulator often just doesn’t start. This is very annoying! Especially because there won’t be any error messages, and if there are they are close to useless.

One the other hand if you have access to a real device it is usually simple to connect it via usb and get it connected instead following Google’s guidelines.

So why not just use a real device?

You can. If you are developing only for this phone, then this is fine. However, if you want to test the app on many different devices the cheapest and simplest is to test the app on many different types of emulators. This is why developers often have both emulators and real devices when coding. The real device is used to test code that needs a phone. E.g. animations in a game can be too slow on the emulator,  or an app can use features that are not available on an emulator such as a phone line or accelerometer.  The emulator is then used to test if the app works on different screensizes, memory size etc. There is a limit to how many devices you can own, and this provides you with a way to sanity check unusual device settings…


(This post was made for the FutureLearn course “Begin Programming”. To avoid discussions in multiple places. Hope you understand.)


U2 and Apple “fun”

posted by Karsten

So I wanted to hear the newest U2 album. Last time I used iTunes to get music was in 2008. I hoped it would be a better experience – It was not!!….

Here is the short version of my experience (over two days – more than 4 hours spent):

Karsten: Opens iTunes on his Mac mini, as he doesn’t dare do this on the Windows box. (last time it corrupted several files on his computer)
iTunes: Here is all your music (nothing is shown)
Karsten: Goes to the iTunes shop to find the U2 album. He has apparently already purchased it. So he goes back to his library.
iTunes: Nope, you do not own anything…
Karsten: iTune please update library.
iTunes: This is better. There is a lot of music on this machine. Please have audio fun.
Karsten: But where is the new U2 album?
iTunes: You haven’t got it!
Karsten: Goes to iTunes Account to see if the album is listed there.
iTunes Account: No, you haven’t bought it.
Karsten: iTunes store are you sure I have bought it?
iTunes store: Yes, you have bought it – Why do you keep asking? You should rate it instead!
Karsten: iTunes store why don’t you play the music.
iTunes store: Sure here is a preview…
Karsten: I want to hear the full version!
iTunes store: I can’t do that! Why not Twitter about this album?
Karsten: Oh Google can you help me out?
Google: Yes, you need to authorise your computer. Go to iTunes->Store->Authorise this computer
Karsten: Thanks! He does this (thinking that he is happy that he does not normally use iTunes, because they would not like him using 5+ computers regularly) and goes to the iTunes in anticipation
iTunes: You do not have the U2 album in your library – stop asking!
Karsten: Is desperate! Please check for available downloads.
iTunes: So am I! Because you haven’t bought anything!
Karsten: Goes to iTunes Account to check that the machine is authorised.
iTunes Account: Yes, you are authorised.
Karsten: iTunes Account – why can’t I download purchased albums.
iTunes Account: Perhaps because you haven’t bought it?
Karsten: iTunes store are you sure I bought u2’s latest album?
iTunes Store: YES! Why don’t you share a link on Facebook about it?
Karsten: Google – why can’t I download U2’s album?
Google: Please, no one has that problem! Everybody wants to delete U2’s new album!
Karsten: Google – why can’t I download an album that I have purchased from iTunes?
Google: Oh, that is because you need to tick music under iTunes->Preferences->Store->Automatic Downloads.
Karsten: Does this, and goes to iTunes.
iTunes: Here is the U2 album, wasn’t that just easy! I am proud how easy that was.
Karsten: Experienced how things can “just work” when using computers…


Students should have fun

posted by Karsten

In my teaching I want to engage my students in the programming exercises so that they have as much fun as possible! I like games design and graphical exercises over string manipulation and sorting problems.

Go here and you will find one of my student’s code. This is the result of the first exercise in our 2nd year Java course. This student clearly had fun – success 🙂