Tuesday, November 8, 2011

WattDepot Katas

If you had read my last post, I wrote that I was going to do some katas about WattDepot. WattDepot is web-server that collect energy data from various places. It is a ongoing project that try to reduce the waste of energy and increase awareness of proper management of the electricity. The data can be retrieved as a XML format and parsed into some form of data that can be analyze and manipulated for research purposes.
I have completed all of the katas, but I really had a hard time doing some of those. I had to do a quick scan through some APIs such as Calendar, XMLGregorianCalendar, Tstamp, Date, and the WattDepot. Below is a summary from each kata I did.


Kata 1: SourceListing


Implement a class called SourceListing, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and their descriptions, sorted in alphabetical order by source name.  Use the System.out.format method to provide a nicely formatted list.
  • Simplest kata out of the 6. I just did a loop through all the sources from the server. I got from each one their name and description.
  • Time taken : 15 minutes.

Kata 2: SourceLatency


Implement a class called SourceLatency, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the number of seconds since data was received for that source, sorted in ascending order by this latency value.  If no data has every been received for that source, indicate that.  Use the System.out.format method to provide a nicely formatted list.
  • Things got complicated when I had to retrieve data from each source. For this kata I had to compute the latency of each server for which I need to get the properties from each source by retrieving the sensordata. After I had figure it out how to get the latency, I then had to sort it out in order by the latency. I went back to the Collection API, and I decided to use the SortedMap. SortedMap will automatically sort the keys by their "natural ordering". However SortedMap allows duplicates so I had to do some checking for that. I just create a condition to check if the latency to be stored is already set in the collection. If it is, then I just retrieve the value ( a list of the sources) and just add the new source to the list. 
  • Time taken: 1 hour. 



Kata 3: SourceHierarchy




Implement a class called SourceHierarchy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a hierarchical list of all sources defined on that server.  The hierarchy represents the source and subsource relationship between sources.

  • Well, for this kata I think I overestimated it. I thought I will just have to write a recursive method to check all the sources. But wait, what if the next one to be checked is a sub-source of the first one? Then I just delete that one I continue with the rest of list. But what if that sub-source has also other sub-sources?. After hitting my head many times on my desk, I decided to do it a dumb way. I checked that the sources in the WattDepot server I was testing, there is only two level of hierarchy. So what I did is to get all the parent sources first, and then check each of them for their sub-sources. It should work so far until two level of hierarchy. If a sub-source has other sub-sources, my code will  not work properly. 
  • Time taken: > 3 hours. 

Kata 4: EnergyYesterday


Implement a class called EnergyYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the amount of energy in watt-hours consumed by that source during the previous day, sorted in ascending order by watt-hours of consumption.  If no energy has every been consumed by that source, indicate zero.  Use the System.out.format method to provide a nicely formatted list.
  • This kata was as bad as the last one. Not of the difficulty of coding it, but I had to look through a bunch of API to get yesterday's date. I spent hours and hours looking through the Calendar API, the Date API, and XMLGregorianCalendar API. Without success in getting the date, I got frustrated and I haven't worked on the katas for the whole last weekend. Sunday at night, I resumed my research about dates API, and came across the Tstamp API. Wow, Tstamp was all I need (and some methods from the DateFormat and Calendar API) to get what I was looking for. After I got the two times (one from yesterday's date at 00:00:00 and the other at 23:59:59) I just used the EnergyConsumed method from WattDepot and compute the energy consumed. After that I used again the TreeMap to sort the list according to the energy. 
  • Time taken: > 3 hours. 

Kata 5: HighestRecordedPowerYesterday


Implement a class called HighestRecordedPowerYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the highest recorded power associated with that source during the previous day, sorted in ascending order by watts.  Also indicate the time when that power value was observed. If no power data is associated with that source, indicate that.  Use the System.out.format method to provide a nicely formatted list.
  • If you had done the previous, then this shouldn't be that bad. The problem is with the non-virtual and virtual sources. Non-virtual sources have all the energy data, but virtual sources does not. What I did (as recomended by Prof. Philip Johnson) is to increase the interval of the time (in XMLGregorianCalendar) by 60 minutes. This will get us 24 energy data for each sources. Overall there are more than 1000 data retrieved, so it takes around 4-5 minutes for my code to query all the data. I just used the Tstamp again  (Thanks!) to increase the time. Then I will just create another method to get the time at which the highest energy recorded occurred for each source. This method I had to do a little research also, because I had to convert the current XMLGregorianCalendar format to a Date format. Again I used the another SortedMap with key as the energy to sorted the list, and the value as a TreeMap that stores both the source name and the time.
  • Time taken: 45 minutes.

Kata 6: MondayAverageEnergy


Implement a class called MondayAverageEnergy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the average energy consumed by that source during the previous two Mondays, sorted in ascending order by watt-hours.  Use the System.out.format method to provide a nicely formatted list.
  • After going through all the hard work of reading a bunch of times API, this one was pretty easy. After I got the two previous Monday, which is basically almost the same as the last two previous katas, but changing some lines of code, I just computed average of both. Then I stored in the computed average (as the key), and the source name (as the value) in a SortedMap.
  • Time taken: 30 minutes. 


Overall, after doing this katas I think I just tasted the bitterness and joy of doing programming. I got so annoyed when I couldn't figure out the solution, but I was jumping around when it worked. I just got also some more experiences on reading APIs, which in the past I barely looked at them. But now I understand that actually looking at every API, it will not only help you a lot, but also will make you write faster coding.

Tuesday, November 1, 2011

Hawaii: "Go Green" seems so far, but doable.

Hawaii is known as the one of the "addicted" consumer of oil for its daily energy consumption. 77% of energy consumption in Hawaii comes from oil, compared to the tiny 1% amount in the mainland. Because of its addiction to the oil, the cost rate consumption in Oahu is 2-3 times higher than the mainland. Even in neighbor islands, the cost can reach to the double amount of what Oahu residents pay. The reason is that the oil is imported into the Hawaiian islands, and just because of that, oil is way too expensive in Hawaii. Not only that, but if we compare to the mainland, all the states are interconnected among each other. Therefore, exchanging energy among them is possible. However, obviously among the Hawaiian islands, the exchanging is impossible. The islands are separated by the ocean and it would hard to find a way to connect all the of them and share their energy. Each island has its own power supply plant to feed energy to every household in every island, which is rather expensive and inefficient. 

Hawaii do have a way to change the problem of oil consumption. Because of its great nature environment, Hawaii is presented with a vast major opportunities to use renewable energy. Some examples are the solar, wind, biomass, and geothermal energy. However, if Hawaii has those kind of excellent opportunities to reduce the cost of energy consumption, then why not use them? One reason is that those energy production will vary depending of the time and weather. For example, if we produce solar energy, we will need some kind of source to produce energy at night. Or maybe some days we get strong winds, some days not. It does vary. Another reason is the unequal distribution of the renewable energy. An example is the Lanai island that can produces a great amount of energy using wind farms, but actually that energy is more needed in Oahu. Finally, the high cost to build the mechanisms to produce those renewable energy is an obstacle. If it is decided to build those mechanisms, then taxes will be raised and people will most likely complain about it.  

In a way to approach the energy consumption problem, the governor of Hawaii signed an approval to initiate a project that would allow Hawaii to produce its energy using 70% of clean energy by the year 2030. It will be composed of 40% of the renewable energy mentioned above and 30% of energy management consumption. Energy management should be then promoted to all residents living in Hawaii in an attempt to reduce their daily energy consumption. Using fluorescent bulbs, taking the bus or walking to school instead of driving a car are some examples of good energy management that will help a lot in this project approved by the governor. 

Tools for researchs and creating products that allow us to manage our energy consumption are still under development. Good software programs are needed to create this tools. Therefore in an attempt to learn more about energy and software development, I will attempt to do some katas about WattDepot. WattDepot is an open-source software that attempts to contribute to help Hawaii with its energy consumption problem. I will talk about this software later on, and how I did on the katas. 

Monday, October 24, 2011

5 RedBulls = 1 Midterm coming up!

So another midterm is coming up and this is gonna be a really hard one, I guess.
Below is my 5 study guide questions:

1. What are the 6 steps for an Inspection review to be formal?
Planning - Orientation - Preparation - Review Meeting - Rework - Verify.

2. What are the three "classical" problems that might occur during a configuration management?
The double maintenance problem - The shared data problem - The simulataneous update problem.

3. If we set the return value to 1 when we override the hashCode () method, does java complains about it? Is it a good coding or bad coding?
Java will not complain about it, but it is really a bad coding. Because every different objects must have a different hashcode value. However, all the objects will recieve a hashCode value of 1. 

4. What is another build tool that was talked in class, apart from Ant? Give a brief description of it. 
The other tool is Make, which is widely popular than Ant. It can support any kind of programming language. However, its syntax is more complex than Ant, which require a lot of use of the TAB character. 
It is Unix-based.

5.  What are some strengths and weaknesses when doing Manual Quality Assurance?
Strengths: Find defects involving requirements - Low false positive rate.
Weaknesses: Redone for all projects - Difficult approach for low-level implementation defects.

Good Luck to everyone on the midterm!

Friday, October 21, 2011

Cloudy Management for Coding

After a tedious coding for my Robocode that took me days, I had learnt another tools called "Software Configuration Management" and "Project Hosting". Well, if you are a sole developer then you should skip this post. However, take in account that every software project to be successful has to be done in groups and the tool I have mentioned will make life easier for project management.

For this project, I used the "Google Project Housing" and the "TortoiseSVN". Subversions, like TortoiseSVN, helps to maintain current or old versions of coding files, web pages or documentation. It was my first time using some kind of Subversion to manage my Robocode project. It was pretty straightforward in which you can commit changes easily to the whole project at once instead going one file by file. I haven't explored any deeper the TortoiseSVN to check for any available features, but so far I haven't ran into any problems.

Google Project Hosting is like your cloudy place to share your project with any other person out there. I like cloudy stuff, but so far I think there are some flaws in this Project Hosting. First, its wikis markup can be annoying. It is just some minor things, but I think I am more comfortable using HTML. Then, there is the problem when you checked out the project, and committed some changes, there might be another person who did some changes already and uploaded to the Project Hosting. Then the files might be out of sync.

Overall, I think I found SVN as a useful tool for software development that will  help me a lot. Google Project Hosting is nicely formatted to do coding project management, but still there are some flaws, and some improvements need to be done.

Google Project Hosting - SnooPSan's Robocode http://code.google.com/p/robocode-cla-snoopsan/.

Tuesday, October 11, 2011

Now into a serious Robocode

Here I am with a new Robocode blog entry.

Having a general idea on how Robocode works by doing the katas, now I am facing with a extremely difficult task: Create a robot that can defeat some of the sample robots. I had put it some time and effort, but from lack of time, my robot is able to defeat 100% 5 out of the 8 samples (SittingDuck, Walls, RamFire, SpinBot, Crazy, Fire, Corners, and Tracker).

Design.
My robot basic movement is a very newbie dodging bullets which I tried to implement by myself. It takes in account the energy of the enemy robot. So if its energy drops by 0.1 or 3, then it is high a chance that it fired a bullet. My robot will then try to dodge it. Sounds good right? But I never take in account that if I hit my enemy, his energy will also drop. Or when my enemy hits a wall or ram onto me.
My targeting was an upgrade of the robocode katas "Boom04" which 95% of the time, my robot will point its gun to my enemy.
My firing also takes in account the robocode katas "Boom03" which the power of the fire is proportional to the distance of the enemy. The farther the enemy is, the stronger the firepower. Also I realized that if my robot is most of the time pointing its gun to the enemy, why not fire a bullet with stronger power? So if my robot's gun faces directly to its enemy, then it will fire a bullet with power of 2.



Results.
Out of 10 rounds this are my results.
SittingDuck : 100%.
Walls: 100%.
RamFire: 50%.
SpinBot: 0%.
Crazy: 0%.
Fire: 100%.
Corners: 100%.
Tracker: 100%.

My robot has a flaw: it fires linearly. Also my dodging bullets is not the best.

Testing.
Due to time constraint, I did some unit test of some helper methods I created. I tested them using some "obvious" inputs to correctly output the expected value. All tests passed.
I also did a behavioral test in which I test whether my robot should fire or not. My robot will fire only if it is directly pointing to the enemy, or its gunHeat is 0. So for each turn, I verify its angle and gunHeat, which 100% of time my robot fires as expected.

Lessons.
I think my code looks "beautiful" thanks to the automated quality assurance I had. I learned how hard is to keep up with the coding standards in Java. For every tiny error you had in your code, the xml files will not let you to build successfully. Also the testing part I find that it will save a lot of time. I didnt have to run every time the robocode application to see if my robot behave as I expected. I just need to write test cases that will run immediately.

Thursday, September 29, 2011

Ant katas

So I continue learning new skills about programming, and this time I did some katas using Ant. Ant is Java tool used for building files, allowing to compile, assemble, test, and run Java code. It is similar to the make tool, but Ant offers you more than that. For example, projects builds in a Ant script can be transported across any system. Also, Ant supports any OS environments.

So below are the Ant katas I did:
  1. Ant Hello World
  2. Ant Immutable Properties
  3. Ant Dependencies
  4. Hello Ant Compilation
  5. Hello Ant Execution
  6. Hello Ant Documentation
  7. Cleaning Hello Ant
  8. Packaging Hello Ant
Ant was totally a new tool for me, and it is written in XML which I haven't touch it for some time. I then had to open up the Ant manual, which was somehow a bit confusing to understand. Then, I attempted to understand my professors' Ant codes, which took me around 1-2 hours to go over a couple of them. It was pretty straightforward, even without comments. However, it is still hard to remember all tags and their attributes.



So I start doing my katas, and like the Robocodes' one, the first 4 were pretty straightforward. I had a hard time doing the "Hello Ant Execution" which it had to run the Hello Ant java code. After doing some research about the <java> Ant tag, I realized that the classname attribute is just the name and not the path to the .class file. After that, the documentation and packaging katas were pretty straightforward also.

I still have to play around with this awesome tool to kata it to the perfection. I like also how it works well Ivy, which allows you to download third party libraries. It makes life so much easier so you don't have to download and import all the libraries required to run a Java application all by yourself. 


Tuesday, September 20, 2011

Kata it to the Perfection

The word "Kata" comes from the Japanese martial arts which describes a series of choreographed movements to develop the "perfect" form. Similarly, "Code Kata" is a series of small programming projects that allow programmers to kata their programming skills. Well, I tried to kata my knowledge in programming by creating thirteen robots using Robocode.
Robocode is an open-source engine game written in Java that allows us to create our own robots and battle with other creators. It is quite popular, since it was first started by IBM. However, IBM dropped the project,but some die-hard fans of Robocode decided to resume and improve the game.


This is how my thirteen robots behave:

  1. Position01: The minimal robot. Does absolutely nothing at all. 
  2. Position02: Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
  3. Position03: Each turn, move forward a total of N pixels per turn, then turn right. N is initialized to 15, and increases by 15 per turn.
  4. Position04: Move to the center of the playing field, spin around in a circle, and stop.
  5. Position05: Move to the upper right corner. Then move to the lower left corner. Then move to the upper left corner. Then move to the lower right corner.
  6. Position06: Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
  7. Follow01: Pick one enemy and follow them.
  8. Follow02: Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
  9. Follow03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  10. Boom01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
  11. Boom02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  12. Boom03: Sit still. Rotate gun. When it is pointing at an enemy, use bullet power proportional to the distance of the enemy from you. The farther away the enemy, the less power your bullet should use (since far targets increase the odds that the bullet will miss). 
  13. Boom04: Sit still. Pick one enemy and attempt to track it with your gun. In other words, try to have your gun always pointing at that enemy. Don't fire (you don't want to kill it). 

Honestly, I was super-hyper excited to start building my robots. I like games and this was my first time doing some game programming. But Hey!, I didn't expect to use trigonometry to create my robots so they can behave as mentioned above. I used a good amount of my muscular brain and I think I had enough of Math related topics for the rest of the semester. 


The first 3 robots were pretty simple, since it is just to test that our Robocode environment is set up correctly.  The next 3 which involves a lot of trigonometry knowledge (for me it is a lot!) were the hardest. I had to stop here for a couple of days and start refreshing all trigonometry I had learnt from high school. With some help from some classmates I was able to model these 3 robots. A problem that I still can't figure out is in the Position05 robot. I had calculated all the angles of all the corners, and my robot was behaving as expected until it tried to move from the bottom left to the upper left. It moved only 1/3 and hit wall, then moved to the lower right corner. I had changed my code with the help of a classmate and now it works fine, but my robot won't be facing exactly to the upper right corner, so I had to do some simple tricks to at least looks like my robot hits the corner. Position04 is just finding the distance between my robot's coordinates and the center coordinates, and the angle using the Math.atan method. Position06 builds off from Position04.


The next 3, which kata our knowledge about scanning in Robocode, does not require any kind of trigonometry so it was much easier than the previous 3 (I was so glad, really!). One thing is that in Follow02, my robot will only stop when it hits the enemy robot it is tracking. I am not sure if it behaves correctly, but I will just leave there to debug in the future if time is allowed. In Follow03, I had to rotate 360 degrees to scan all the enemy robots and compare each other to find the closest one to my robot. I am not sure also if my robot behaves as expected, but at the end my robot will eventually hits wall and stop rotating there forever. 


The last 4 robots allows us to kata our knowledge in scanning enemies and firing at them. Again, it didn't involve any trigonometry. However, it was as hard as the last 3 Position robots. We have to scan and fire at the enemy now. Boom02 and Boom03 builds from Boom01, differing in one thing each other. Boom02 has to  keep track of the enemy chosen, and Boom03 fires using a power proportional to the distance between my robot and the enemies. Boom04 was hardest in this group. One odd thing in Boom04 is that my robot will sometimes rotate 360 degrees if the heading of my robot is directly opposite to the heading of its gun. 


I had some headaches doing this "Code kata", but overall I had fun also. Doing this set of kata I could kata a little bit of my programming skills, especially in Java since I haven't touched it for a while. Now I need to kata a robot for the upcoming tournament and doing these katas I think I got an idea of how my robot would be.