Sep 29, 2009
Sep 23, 2009
Plumbers and Lawyers
I heard an interesting joke the other day in one of my economics classes:
A lawyer one day has a problem with his sink, and has to call in a plumber to fix it. The plumber shows up, fixes it in 20 minutes, and says, "that'll be $200."
"What?", replied the lawyer, "you only worked for 20 minutes! That means it is $600/hour! I'm a lawyer and I only charge $250/hour."
"I understand your pain," said the plumber, "I used to be a lawyer too."
A lawyer one day has a problem with his sink, and has to call in a plumber to fix it. The plumber shows up, fixes it in 20 minutes, and says, "that'll be $200."
"What?", replied the lawyer, "you only worked for 20 minutes! That means it is $600/hour! I'm a lawyer and I only charge $250/hour."
"I understand your pain," said the plumber, "I used to be a lawyer too."
Sep 22, 2009
Compiz Effects Speed
I found this little tutorial here which tells you how to make Compiz's effects look much nicer, just by upping the refresh rate to that of your monitor.
And well, it really really works. Everything runs much more smoothly - of course you'd need a video card that can handle it, but it will probably work on most decent ones these days.
My question is, why does Compiz default to 50Hz when the average monitor runs much higher than that?
And well, it really really works. Everything runs much more smoothly - of course you'd need a video card that can handle it, but it will probably work on most decent ones these days.
My question is, why does Compiz default to 50Hz when the average monitor runs much higher than that?
Sep 20, 2009
Prices of Different Liquids
I stumbled across this post through a friend today, it is very interesting. I decided to actually dig a little deeper and see if these things are as they say they are.
I was unable to find a price on human blood, penicillin, and the 3M PF-5030 (which is a refrigerant), but I got a price on everything else. Note that all of this is done using CAD and prices as of today (Sept. 20, 2009).
Crude Oil:
Using the price from here (Bloomberg, converting USD to CAD using Google) of $76.63 CAD per barrel, and the volume of a barrel (Wikipedia) of 158 987 mL you get $0.00048/mL.
By the time that gets to your car, if we use $1.00/L which is what it seems to be hovering at in Quebec, that's $0.001/mL.
Bottled Water:
This one I guesstimated the price of a bottle of water at around $1.60 for a 500mL bottle, so that makes $0.0032/mL - 6.67 times the price of the crude oil.
Red Bull:
Also guesstimated at $2.99 for a 250mL can, giving $0.012/mL.
Vodka:
Using the price of the most expensive one at the SAQ called Belvedere (SAQ) which gives $42.75 for a 750mL bottle we get $0.057/mL.
Finally (since I skipped a bunch) we have HP #45, which is their normal black ink. The price is $42.77 (NCIX) for a 42mL cartridge, giving $1.02/mL.
So printer ink is 1020 times more expensive than the gas you put in your car. Yikes!
Note: These numbers are not 100% accurate since some of these have taxes factored in and others do not.
I was unable to find a price on human blood, penicillin, and the 3M PF-5030 (which is a refrigerant), but I got a price on everything else. Note that all of this is done using CAD and prices as of today (Sept. 20, 2009).
Crude Oil:
Using the price from here (Bloomberg, converting USD to CAD using Google) of $76.63 CAD per barrel, and the volume of a barrel (Wikipedia) of 158 987 mL you get $0.00048/mL.
By the time that gets to your car, if we use $1.00/L which is what it seems to be hovering at in Quebec, that's $0.001/mL.
Bottled Water:
This one I guesstimated the price of a bottle of water at around $1.60 for a 500mL bottle, so that makes $0.0032/mL - 6.67 times the price of the crude oil.
Red Bull:
Also guesstimated at $2.99 for a 250mL can, giving $0.012/mL.
Vodka:
Using the price of the most expensive one at the SAQ called Belvedere (SAQ) which gives $42.75 for a 750mL bottle we get $0.057/mL.
Finally (since I skipped a bunch) we have HP #45, which is their normal black ink. The price is $42.77 (NCIX) for a 42mL cartridge, giving $1.02/mL.
So printer ink is 1020 times more expensive than the gas you put in your car. Yikes!
Note: These numbers are not 100% accurate since some of these have taxes factored in and others do not.
Aug 28, 2009
Interfacing between JRuby and Scala
One of the major features of JVM languages is their interoperability. It's really easy to call Java code from a JRuby script, and (while slightly less easy) you can run a Ruby script from within a Java program.
However, how difficult is it to interface between two non-Java JVM languages? It turns out that it isn't really that hard at all! For this post, we'll talk about how to interface between JRuby and Scala, it's really rather simple.
So here's our scenario. We are writing our main app in Scala, because Scala is faster than Ruby. However there are some situations where we don't want to have to recompile the whole thing to make changes, and we want the code to be nice and easy to understand for people who don't know Scala (admittedly, Scala is not a good first language as it is rather complex). We're going to embed some Ruby scripts within our app.
Here's a basic Scala outline in ScalaTest.scala:
So this is pretty cool, but how do we get information between Scala and Ruby? That part can be a little bit tricky since the type systems in Scala and Ruby are very different. However for our example here, we will stick to simple things. Here is our new Scala code:
However, how difficult is it to interface between two non-Java JVM languages? It turns out that it isn't really that hard at all! For this post, we'll talk about how to interface between JRuby and Scala, it's really rather simple.
So here's our scenario. We are writing our main app in Scala, because Scala is faster than Ruby. However there are some situations where we don't want to have to recompile the whole thing to make changes, and we want the code to be nice and easy to understand for people who don't know Scala (admittedly, Scala is not a good first language as it is rather complex). We're going to embed some Ruby scripts within our app.
Here's a basic Scala outline in ScalaTest.scala:
import javax.script._ // import Java's scripting APIAnd test.rb:
import java.io.FileReader
object ScalaTest extends Application {
var engine = (new ScriptEngineManager).getEngineByName("jruby")
engine.eval(new FileReader("test.rb"))
}
puts "Hello from Ruby!"Before we attempt to run this though, we'll need to get both JRuby, and the JRuby engine. You can grab JRuby from their website (as of this writing the latest version is 1.3.1), just extract that and grab jruby.jar from the lib folder. As for the JRuby Engine, you'll have to grab the big engine tarball from the Java site and extract jruby-engine.jar from the jruby/build folder. Once you have those put them in the same folder as your code, then compile and run:
scalac ScalaTest.scalaWhat you should see is "Hello from Ruby!" pop up on the screen.
scala -cp .:jruby-engine.jar:jruby.jar ScalaTest
So this is pretty cool, but how do we get information between Scala and Ruby? That part can be a little bit tricky since the type systems in Scala and Ruby are very different. However for our example here, we will stick to simple things. Here is our new Scala code:
import javax.script._And the Ruby code:
import java.io.FileReader
object ScalaTest extends Application {
val engine = (new ScriptEngineManager).getEngineByName("jruby")
val name = Console.readLine("What is your name?\n")
// assign a variable in the engine
engine.put("name", name)
// cast the result of the execution into a string
val colour = engine.eval(new FileReader("test.rb")).asInstanceOf[String]
Console.print(name + ", your favourite colour is ")
// this part is unnecessary, but let's have some fun with colours :)
Console.print(
if (colour.toLowerCase == "blue")
Console.BLUE
else if (colour.toLowerCase == "red")
Console.RED
else if (colour.toLowerCase == "green")
Console.GREEN
else
""
)
Console.println(colour + Console.RESET + ".")
}
# the name variable is passed in as $name, which is a StringWhile this is a trivial example, we could use this to embed all sorts of functionality within our application. In fact, this isn't even limited to JRuby. By changing the getEngineByName call to say, "Javascript", we can execute Javascript code instead of Ruby code. Or any other JVM language included in that big engine tarball, including Scheme, Jython, and Groovy to name a few. You can even use Java as a scripting language, although you have to jump through a few hoops since Java doesn't support global variables.
puts $name + ", what is your favourite colour?"
gets.strip
Aug 16, 2009
Lisp and Code Mutation
So exams for summer courses are done, and I have no job. I do have a small contract, but other than that I have plenty of time on my hands. So I'm working on a tiny little idea I had (if you've been following this blog for any amount of time I tend to get a fair few of those).
One thing I've always found interesting about Lisp (for this article we can assume I'm talking about Common Lisp) is the homoiconicity. Well I don't know enough of the language to comment on its practical nature - I've been having a nightmare of a time getting into it, documentation online is not great and there seems to be a lot of simple things (like string padding) that aren't there, or at least not that I can find - some of the things you can do with the language are downright amazing. Ruby likes to boast that it can do metaprogramming, but it is nowhere near as powerful as what Lisp can do.
Anyway, enough on the impressions of the language (I'm a language snob, I know). Let's get to this idea. A friend of mine who is doing her master's at University of Calgary in swarm theory was out visiting about a month ago for a conference on genetic algorithms and evolutionary computing called GECCO. So of course, we ended up talking about the subject. She said that with much of the current research, what they do with genetic algorithms is they put a set of inputs into a program, and then let the program find an "optimal" version of those variables. I thought this was interesting, but limited. See, the variables will change, but the code? Nope, it will stay fixed. The algorithm doesn't change, just the variables within it. Now when working with large neural networks this isn't bad at all, but sometimes maybe the program is the thing that should be changed and not the inputs.
So it got me thinking. In Lisp, because of homoiconicity, code is data and therefore can be manipulated as data. So what if you wrote a program that took as an input another program and a specification, and would alter the program in random ways and check if the program still matched the specification? It would be very interesting to see what would come out.
An even more interesting thing is this: what if the specification was incomplete? If it is too incomplete, the program might not even work anymore. However if the specification just checks the important bits, it's possible that a program outputted by the program re-writer could do some interesting extra things outside of the specification. Most likely not, but it could happen.
Finally as a closing note, there will be programmers who will point out that you could write a program re-writer in any language. It's just the nice thing about Lisp is that you can read in and parse the program by going (read input), which returns an s-expression representing the code that was just read in. It is trivially simple. Then on top of that you can manipulate this s-expression all you like, and then output it to a new file by going (pprint sexp out) and you have an output which is still Lisp. So the whole time, you're working with a single language and there is no real need for any intermediate forms.
One thing I've always found interesting about Lisp (for this article we can assume I'm talking about Common Lisp) is the homoiconicity. Well I don't know enough of the language to comment on its practical nature - I've been having a nightmare of a time getting into it, documentation online is not great and there seems to be a lot of simple things (like string padding) that aren't there, or at least not that I can find - some of the things you can do with the language are downright amazing. Ruby likes to boast that it can do metaprogramming, but it is nowhere near as powerful as what Lisp can do.
Anyway, enough on the impressions of the language (I'm a language snob, I know). Let's get to this idea. A friend of mine who is doing her master's at University of Calgary in swarm theory was out visiting about a month ago for a conference on genetic algorithms and evolutionary computing called GECCO. So of course, we ended up talking about the subject. She said that with much of the current research, what they do with genetic algorithms is they put a set of inputs into a program, and then let the program find an "optimal" version of those variables. I thought this was interesting, but limited. See, the variables will change, but the code? Nope, it will stay fixed. The algorithm doesn't change, just the variables within it. Now when working with large neural networks this isn't bad at all, but sometimes maybe the program is the thing that should be changed and not the inputs.
So it got me thinking. In Lisp, because of homoiconicity, code is data and therefore can be manipulated as data. So what if you wrote a program that took as an input another program and a specification, and would alter the program in random ways and check if the program still matched the specification? It would be very interesting to see what would come out.
An even more interesting thing is this: what if the specification was incomplete? If it is too incomplete, the program might not even work anymore. However if the specification just checks the important bits, it's possible that a program outputted by the program re-writer could do some interesting extra things outside of the specification. Most likely not, but it could happen.
Finally as a closing note, there will be programmers who will point out that you could write a program re-writer in any language. It's just the nice thing about Lisp is that you can read in and parse the program by going (read input), which returns an s-expression representing the code that was just read in. It is trivially simple. Then on top of that you can manipulate this s-expression all you like, and then output it to a new file by going (pprint sexp out) and you have an output which is still Lisp. So the whole time, you're working with a single language and there is no real need for any intermediate forms.
Aug 14, 2009
Actor Simulation Synchronization Issues
A while back I did up a post about using Scala actors in a synchronized simulation. I'm hoping to use this to model a simple economy, however I've run into a major problem and I'm trying to find a solution to it - which is why it took so long for me to write a follow-up to that last post.
The way the other simulation worked is by using synchronized time steps. Normally this is good because it means that actors which take varying amounts of time to process can all finish their job before the next time step starts. This also helps when you decide to scale and use a distributed system, you will end up having slower machines and network delay, but can still maintain synchronization because the faster machines will either do more work, or will wait for the slower machines.
Synchronization in my simulation was done using the Act and DoneAct messages. When an agent receives an Act message, it means that the next time step is starting and that they should begin processing. Then when they are done, they send a DoneAct message to the simulation driver to say that they have finished processing. The problem with this is that some agents may send messages to other agents that are finished. There is the scenario that an agent, A, sends a message to another agent, B, and then A sends the DoneAct message to the simulator. However B may well still be processing away the message, which may in turn affect agent C, which may then wrap around and affect agent A. Effectively this could turn into an infinite loop, and due to the nasty halting problem there is not really any general way of detecting this.
I have a solution, however I am not sure if it will work. Basically agents cannot communicate with one another if they are using the ! approach (asynchronous messaging). They can use the ?! method (synchronous method) where when agent A sends a message to agent B, agent A will wait for agent B to return before continuing execution (basically the same as a method call in traditional OO languages like C++ or Java). However in the case where agent A wants to send an asynchronous message to agent B, it can do so by sending a Delayed() object to the simulator, which will then send the message to B at the next time step.
Basically I'm thinking out loud here, hoping that by explaining things here it might give me some insights on how to go about building this thing. If anybody is interested in this type of thing and wants to give their two cents, feel free to comment :)
The way the other simulation worked is by using synchronized time steps. Normally this is good because it means that actors which take varying amounts of time to process can all finish their job before the next time step starts. This also helps when you decide to scale and use a distributed system, you will end up having slower machines and network delay, but can still maintain synchronization because the faster machines will either do more work, or will wait for the slower machines.
Synchronization in my simulation was done using the Act and DoneAct messages. When an agent receives an Act message, it means that the next time step is starting and that they should begin processing. Then when they are done, they send a DoneAct message to the simulation driver to say that they have finished processing. The problem with this is that some agents may send messages to other agents that are finished. There is the scenario that an agent, A, sends a message to another agent, B, and then A sends the DoneAct message to the simulator. However B may well still be processing away the message, which may in turn affect agent C, which may then wrap around and affect agent A. Effectively this could turn into an infinite loop, and due to the nasty halting problem there is not really any general way of detecting this.
I have a solution, however I am not sure if it will work. Basically agents cannot communicate with one another if they are using the ! approach (asynchronous messaging). They can use the ?! method (synchronous method) where when agent A sends a message to agent B, agent A will wait for agent B to return before continuing execution (basically the same as a method call in traditional OO languages like C++ or Java). However in the case where agent A wants to send an asynchronous message to agent B, it can do so by sending a Delayed() object to the simulator, which will then send the message to B at the next time step.
Basically I'm thinking out loud here, hoping that by explaining things here it might give me some insights on how to go about building this thing. If anybody is interested in this type of thing and wants to give their two cents, feel free to comment :)
Subscribe to:
Posts (Atom)