Sunday, June 8, 2014

instanceof for C++: typeid

Always be certain that pointers are dereferenced when using typeid. It can be a common error, and the compiler won't give any warning.

Tuesday, January 14, 2014

Variations in SCV harvesting rate

SCVs harvest the slowest of all workers because they have the lowest acceleration. However, sometimes they don't need to accelerate and seem to jump to max speed. This happens on certain minerals, depending on how they are placed on the map in relation to the command center. 

I observed this effect when I was trying to  measure how many SCVs can mine a mineral for maximum efficiency. I defined idle time for each mineral as any frame an SCV is travelling and not harvesting from it. Dividing by the total time gives the idle ratio. As seen below, the ratios for 1 SCV per patch is about 0.5, so in theory 2 SCVs can mine from each patch at full efficiency. 


Take note of the top 2 mineral patches. They both have had only one SCV (the 10th extra SCV stays around the bottom) for a while, and have stabilized to different idle values. The top one spends 48% idle, while the one under it is 55% idle. The SCV that mines the top patch is consistently moving faster than the SCV directly below it.

If we assume the difference is from the top SCV not having to accelerate, and that it is possible to micro the SCVs to move faster in mining, then 2 SCVs per patch should achieve perfect efficiency.

Friday, January 10, 2014

Where do SCVs go inside refineries?


SCV just before it enters the refinery. Its order is 'MoveToGas'.

SCV inside refinery. Notice the info panel is still being drawn in the same place. Its order is now 'HarvestGas'.

The refinery info panel, with SCV inside. BWAPI does not return any units loaded inside the refinery, or else it would have shown up on the info panel.

SCV as it leaves the refinery. Its order is now 'ReturnGas'.

I now believe that the SCV simply turns invisible while it is harvesting gas. Maybe it is also invincible. BWAPI's Unit::getLoadedUnits() cannot be used here as it returns no loaded units. The only way to check for SCVs inside refineries seems to be to see if the Order status is 'HarvestGas'. BWAPI documentation notes that Game::getAllUnits() does not include units inside refineries so the pointer to the SCVs must be saved before they enter.

Tuesday, September 25, 2012

Finding Base Locations



One very useful thing BWTA did was find the Baselocations on the map, places to build resource depots (hatchery, command center, nexus.) However, BWTA would sometimes place the baseloc on a suboptimal spot. The optimal spot to build a depot in every mineral cluster is the location that minimizes the distance to each mineral and vespene geyser. This problem is very easy for a human to solve, and seems to give BWTA-based bots some trouble.

I had a good idea how to solve this problem and I wondered if my solution would run into the same trouble, so I built a baselocation analyzer from scratch. The algorithm has 2 major steps. First I need to group the resources on the map into clusters. Then I can analyze each cluster in isolation and find the optimal spot to place a resource depot. The fact that this problem is trivial for a human to solve by inspection made it easy to   check accuracy by visualizing the data in graphical form.

Grouping resources into clusters

This was actually simple as I had already implemented the DBSCAN algorithm. I still needed to tweak the thresholds, as the previous incarnation was focused on clustering mobile units. I found it necessary to drastically raise the radius to 367 pixels due to a far away geyser in Tau Cross. The number of neighbors threshold I have at 3, and I reject all minerals with fewer than 250 remaining since the map Fortress has a few 249s that are only there to allow workers to pass through the gates into the expansions.

Once I had grouped the resources based on proximity, I had an issue to resolve. On Central Plains the two clusters at 6 o'clock were so close that they got merged into a single cluster. I separated such bridged clusters by ensuring that all resources in each cluster belonged to the same region. Thus it is important to tackle regions and choke points before base locations.

Finding Optimal Depot Positions

Now that the large set of resources has been segmented into smaller localized clusters, I am able to tackle each cluster in isolation. For each resource unit, I generated a potential field that decayed with distance. Resource depots cannot be built within 3 build tiles of resource units, so I generated a second field that wiped out the first within a radius of 3.

Now that I had the potential fields set up, all I had to do was sweep an area around each cluster and select the spot with highest potential, which would be where the summed field concentration is greatest. Since all resource depots are 4x3 build tiles in footprint, I checked the footprint to be sure that it was outside the 3-tile limit radius and also that it was on buildable terrain. For this case, walkability == buildabiliity. I initially tried to use BWAPI's isBuildable(x,y) function, but it seemed to take unexplored tiles as unbuildable, and I only wanted to work with static map data, so I ended up using isWalkable(x,y).

Results

The algorithm produced excellent results right away with no additional tuning required. Here are the images of detected base locations and the potential field overlaid onto the image of the map regions. I tested each map in the SSCAI 2012 tournament.

Benzene
Destination
Heartbreak Ridge
Neo Moon Glaive
Tau Cross
Andromeda
Circuit Breaker
Electric Circuit
Empire of the Sun
Icarus
Jade
Judgement Day
Python
Roadrunner
So far this algorithm has placed the base location in the optimal spot for each map that I have tested.

Wednesday, September 19, 2012

Analysis Results for SSCAI 2012 Maps

Results from my modified algorithm (based off Skynet). Details here and here. This is the set of maps to be used in the SSCAI competition. I am very happy with the results.

Benzene
Destination
Heartbreak Ridge
Neo Moon Glaive
Tau Cross
Andromeda
Circuit Breaker
Electric Circuit
Empire of the Sun
Icarus
Jade
Judgement Day
La Mancha 1.1
Python
Roadrunner

Tuesday, September 18, 2012

Choke Detection Tweaks

A statement I made earlier was incorrect: Skynet's algorithm does not find all regions first as BWTA does. Instead, it alternates, finding a region, then determining the limits of that region by closing it off with chokepoints. Thus the order of forming regions matters and will affect where the choke points are found. Skynet starts with the largest region and works its way down.

Two thresholds control the chokepoint detection process. The regionThreshold, originally set at 0.9, eliminates chokepoints that are too wide compared to the parent region. The tileThreshold controls how quickly the choke must widen out. It was originally 0.8, but changing it to 0.91 greatly improves accuracy over my map sample set.

 Orbital Relay is not shown since it remains identical.
 Python's top left resource area is now detected thanks to the bump to 0.91.

 Many changes in Tau Cross. The importance of the additional regions is that all resources are now partitioned into their own regions. I believe that this is important for strategic purposes.

Lost temple's center area is now cordoned off, which is the important part. It now looks similar to BWTA's output, but has extra regions. BWTA has an extra step in which it merges small regions together. I don't think it is necessary, since the AI should be able to deal with having extra regions. In any case, it is better to have too many divisions than too few.

Choke Detection Depends on Region Detection

I know why chokes are missing in Skynet's analysis. The algorithm finds regions first, then tries to narrow down where to choke should go between regions. The missing chokes are from missing regions - compared to BWTA's results.

These images are visualizations constructed by taking the number of adjacent tiles that have a different nearest obstacle. The colors start off deep blue for 0 and move up towards red. Pink circles are drawn at detected region centers. Compare these images with the chokes detected in my last post.

 You can see from the Orbital relay image that there are only 4 regions in the center ring, hence only 4 choke points dividing it, while BWTA must have found 8 regions.

 On Python it is apparent that the top left region has not been detected, explaining the lack of a choke there.

 On Tau Cross we see that there can be a choke point for every land path between two adjacent regions.

The lack of a bottom choke in the center area of Lost Temple is due to there only being one region center detected at the very bottom instead of in the middle intersection area. The algorithm decided to place the single choke a bit further south of the bottom entrance.

Adjusting the thresholds for what is considered a region will probably fix this issue. I am going to try to match BWTA's output.