Showing posts with label cs. Show all posts
Showing posts with label cs. Show all posts

Tuesday, June 17, 2008

AI algorithm idea

I was playing Battle for Wesnoth earlier and became interested in the problem of partitioning all the enemy units according to which defensive position they're headed for. That interest was actually motivated by watching my enemy react to the movement of my units.

I don't know what approach Wesnoth uses ... it's probably much more advanced, but I proposed a simple solution that should be pretty accurate.

After the enemy units take a turn, compare the current position of each enemy unit to its previous position. If the new position reduces the distance from the enemy unit to one or more defensive position, take whichever one is closest and assume that the enemy is moving towards that defensive position.

The first solution you might think of is picking arbitrary units and doing a kind of breadth-first search around them until it finds clusters of enemies, then tracking these clusters. The simple yes/no question of whether a unit occupies a given hex grid asked repeatedly by such an algorithm would make it a lot cheaper than having to number crunch the distance for each enemy unit and defensive position ... when enemies move together in tight groups. But in a game like Wesnoth, units can spread out when using attrition tactics and even when using conventional tactics, if certain units have much greater movement rates than others. So the BFS approach doesn't work generally.

Still, this approach isn't computationally terrible. Since the total number of distances calculated is defensive positions times enemies, doubled because the current position has to be compared with the previous position in each pairing of defensive positions with enemy units. So the algorithm consistently runs at .

That's the basic approach, but there are a few ways it could be improved. A few suggestions from myself and others:

One of the considerations I had immediately after coming up with the algorithm is that feints and even honest temporary retreats sometimes break up an approach. A simple way to handle this fact is to assign each defensive position a number and keep a running average of how many times an enemy unit attempts to approach which defensive positions. The expected value, then, is the most likely target of the enemy unit. For instance, say a castle is labeled zero and an outlying keep is labeled one. If an enemy unit is seen to approach the castle four times but has to head away from powerful guards towards the keep one time, the expected value is .20. Round that down to zero and the enemy is considered to be approaching the castle.

Neural networks would be ideal for detecting feints and other maneuvers, but I don't want to get bogged down in that shit right now. Maybe when I'm doing grad research.

If parts of the terrain are impassible, it's reasonable to assume that enemy units moving in a certain corridor are headed in a certain direction. The way to track enemies according to those data would be to flag passages in between large tracts of difficult terrain—passages that are shorter than the paths around the masses that surround them—and take note whenever an enemy crosses them. That might not even be worth the effort though.

Another idea is to look at the algorithm sailors use to anticipate collisions, Closest Point of Approach. A variant of this algorithm can be very useful when the defenders are moving and when movement of units is continuous. (On a game board, time and space are discrete.)

Someone else suggested that, if there's any ambiguity in whether an enemy unit is approaching a given position (i.e., might be approaching two or more positions), there should be a probability of attack attached to each position to clear up the ambiguity. Calculating that probability might be difficult though. A castle is worth more for the enemy than a small keep, but, for example, a few lesser units stand no chance against a hardened facility. There are so many factors to take into account and it would probably be necessary to analyze games for each possible approach. Overall, I feel that this algorithm should be limited to gathering data on movement; threat assessment should be a separate task altogether.

Friday, April 18, 2008

why I'd like to specialize in bioinformatics

I was at the local public radio station last Sunday when I met up with Dr. Jack Zilfou, who hosts the Arabic music program then about every other week. He's a biochemist who develops new medicines. If you Google his name, he has actually authored or co-authored a good deal of research cited in other publications. The last time I saw him was when I was in high school and so we caught up. In addition to finding out that he listened to a lot of thrash metal when he was my age, I learned about the growing opportunities in bioinformatics from him.

“Biology easily has 500 years of exciting problems to work on.” Donald Knuth, noted 31337 h4x0r


I had heard about this field vaguely before, but didn't pay it much mind. Jack speaks very highly of this frontier in science. Apparently, he depends on bioinformatics specialists to glean useful information from scads of raw biological data generated every day. After he explained the field in some more detail, it sounded pretty interesting. I did some research on it and found it even more appealing after a closer look. The algorithms entailed in bioinformatics appear to deal mainly with discrete areas of mathematics that I really enjoy using.1 I communicated this interest of mine to the professor in Bioinformatics at Lehigh when I was asking about how I could study this field and he said that there isn't a Bioinformatics degree program per se, but there is a relevant track in the Computer Science department and bade me good luck.

So, double win. Since I'm already a CS major, I don't have to make a 180 degree turn. Maybe 30 degrees. Here's the triple win for the trifecta:



Wow. Without the labels, I would have guessed that was the graph of the velocity of something being shot out of a mass driver. There is a great demand to deal with this glut of new data and, if the median salary figures mean anything, a relative shortage of qualified people. I can definitely see myself getting avenged for middle school and a little less than half of high school. Eat shit, assholes! Of course, if I had a billion dollars tomorrow I would still enter a CS / math career—because I love these two fields with all my heart. However:



And on top of that, the excitement of knowing that people in the next room are using cancer-causing restriction enzymes. Dipset!

The single fail in this situation, albeit not an epic one, is that Perl seems to be really popular in bioinformatics. That makes sense since the field is mainly concerned with crunching assloads of text, and esp. a lot of pattern matching. However, Perl has an ugly object system, no exception handling and can easily make a Linear A tablet look like Curious George. If I use BioPerl in my career, I just hope no one on my team likes writing illegible garbage. Because I've seen that shit and it's not nice.

1 - I am also busting my ass retracing my steps over all the important theorems I learned in calculus that were taken for granted in school. I'm surprised I understood anything at all after we basically skipped over the very basic idea of infinitesimals. I'm virtually certain discrete math will always be my strongest suit though.

Wednesday, April 16, 2008

haiku for a CS test

I had an exam
It was about assembly
I kicked its dumb ass

classes next semester

So in the fall I'm taking 19 fucking credits worth of classes to make up for my horrible 14-credit pace of the past three semesters. I kind of look forward to the challenge because at the current rate it's boring the hell out of me. Namely, I (tentatively) have Data Structures & Algorithms, Calc III, Physics for Science and Engineering I (1337), Macroeconomics and Ethics & Moral Problems. On top of that, I have Introduction to Psychology (fuck that) as a web class in the summer, just to round off that two year degree so I can get the hell out of here and start attending Lehigh University.

Due to a scheduling conflict, I have no choice but to take Calc III with Ken Krauss, rather than Alex Rolón. Granted, Rolón's classes are pretty difficult, but he challenges us to think hard about what we do and is overall a better teacher than Krauss. Hopefully, the nearly unanimous disapproval I've heard of his classes is at least part bullshit, because I have Data Structures & Algorithms with him as well. The only other class I've taken with Krauss was Comp Sci I and my brain was in power-saving mode for most of the time. Sure, I got an A, but that's only because I had already seen almost every damn thing he put up on the board. I'm not sure I can put up with him for seven hours a week.

Man, I think I would probably already have an associate's degree if it weren't for the all the bullshit electives I've had to take. Of the six classes I'm going to take in the summer and fall, only three are directly related to the computer science / math track. I can't believe Macroeconomics is a 'Western Culture elective' and that I have to take either it or some other even worse class. If that's true, why can't Calc III be a Western culture elective? Newton (England) and Leibniz (Germany) came up with it in its modern form, right? Every country has macroeconomic affairs, and economics has nothing to do with 'culture', at least from an academic perspective. Even so, even Macroeconomics is not as worthless and irrelevant to my major as something like Ethics & Moral Problems. Like I've said before, 'ethics' is a fictional abstraction. I'm not interested in ethics. Can that shit so I can take cool classes.

Come what may, I will continue to kick ass next semester. Dags för strid!