Posts mit dem Label HowTo werden angezeigt. Alle Posts anzeigen
Posts mit dem Label HowTo werden angezeigt. Alle Posts anzeigen

Montag, 17. Januar 2011

Installing Io on my Mac

Currently I'm reading Seven Languages in Seven Weeks by Bruce Tate. The frist new language to learn is Io, a prototype based language. So I installed it on my Mac running Snow Leopard.
These instructions are not by an expert, but just describes the way it worked for me. If you have any suggestions or improvements feel free to add a comment.

The easiest way to install Io is by using Homebrew.
brew install io
If you want the lastest version from github you can alternatively install Io by hand.

First we need CMake, a cross platform make tool. Download the OS X version and install it.

Update: If you want to use the Socket Addon, you must also install libevent.
sudo port install libevent
or use a the corresponding command if you want to install it with Homebrew.

Then we need the lastest version of Io from github. Unpack it and change to that directory, then issue the following commands:
mkdir build && cd build
cmake ..
sudo make install
This will install libraries to /usr/local/lib and add the commands to /usr/local/bin: io and io_static. Then we're ready to test Io's VM with:
io ../libs/iovm/tests/correctness/run.io
If you get an error like this:
dyld: Library not loaded: libbasekit.dylib
  Referenced from: /usr/local/bin/io
  Reason: image not found
Trace/BPT trap
Then we have to show the linker the way to the libraries:
DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib
export DYLD_LIBRARY_PATH
I added these two lines to my .profile.

After that you should be ready to get started.
io
Io 20090105
Io> "Hello Io" print
Hello Io==> Hello Io


Update: After playing around with Io, I wanted to try out the Socket Addon. But I got this error:
Io> Socket

  Exception: Object does not respond to 'Socket'
  ---------
  Object Socket                        Command Line 1
The missing libevent library was the cause for this error. Io doesn't complain about it, when compiling, but just skips the addon.

Mittwoch, 10. November 2010

Secure your steps, how to disengage from a move action

Update: This feature has been removed in with the beginning of the 3rd season. Instead use look_at for agents in level Clone War.

In Zero X the simulation and all agents are running continuously. That's one big difference to other programming games, that usually are round based. The consequence is, that in the time between an agent inspects his environment with a look_around and the action he takes, with a move_to for example, the environment can change.
For example a field is free, when he makes the look_around, but has been occupied in the meanwhile by another agent, when he moves. This would result in a unintended attack against this other agent.

Although this case is rare, as a look_around and a move_to are very close. But this time gape can grow bigger as the analysis of the environment takes more time. In general agents have to deal with this kind of insecurity about their perception. This one of the many challenges in this game.

But in order to handle better this kind of insecurity, starting with season 2, move_to can now take a block.
move_to(x,y) {|target| ... }
The argument passed to the block is another view of the target field at the exact time of the movement. This gives the agent the chance to check the preconditions of his action. If they are not fulfilled anymore, he can now disengage from the move action.

move_to(x,y) do |target|
  unless target.has_no_population?
     disengage 'field is not free anymore!'
  end
end
Disengage can take a message, that will then be displayed in your reports. The action is canceled and the agent remains on his starting field. But disengage comes not for free. A move_to would cost 6 action points, disengage from this action still costs 3 action points.

Disengage can only be used within an action block like the one in move_to.

Sonntag, 5. April 2009

Reading Binary Files with Ruby

For my current project I need to read some binary data with Ruby. The data file is formatted as 16-bit BINARY INTEGERS, hi-byte-first order with 2160 x 4320 data values.

From my Java days I knew the DataInputStream class. With it it's every easy to read the data, just use io.readShort().
Therefore I was looking everywhere in IO and File for some methods to read the data in an appropriate format. But I couldn't find anything really useful as all methods are working with strings and characters.

But then I found the pack method in Array and its corresponding method unpack in String. With this two methods it's easy to read and write binary files in various formats.

Here the code snippet where I read the gzip compressed file. @size is in my case 4320 and @height an array of 2160. After reading the file I can access the data with @data[line][row].
Zlib::GzipReader.open(filename) do |gz|
  x = 0
  # read one line (16-Bit-Integer = 2 bytes)
  while line = gz.read(2 * @size) 
    @data[x] = line.unpack('s' * @size)
    x += 1
  end
end
If you need more, have a look at BitStruct. It provides a nice DSL for handling binaries.

Dienstag, 4. November 2008

Compiling Ruby 1.9.1 on Leopard

Update
With the new Ruby 1.9.1 stable version, I only used the following configure (--with-readline-dir is not recognized anymore)
./configure --prefix=/usr/local --program-suffix=1.9
make
sudo make install


The new Ruby 1.9 Preview is out, time for a first impression. But already step one, compiling readline 5.2 failed. Thanx to Han's Hint, I was able to patch the responsible shobj-conf file.

So after patching readline's conf file, in the readline-5.2 directory:
./configure --prefix=/usr/local
make
sudo make install
Now compiling ruby should work as expected, don't forget to point to your new compilied readline library. In your ruby directory:
./configure --prefix=/usr/local --program-suffix=1.9 --with-readline-dir=/usr/local
make
sudo make install
Now you have both ruby versions in parallel, try ruby -v and ruby1.9 -v