HOG2
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MinimalSectorAbstraction Class Reference

MinimalSectorAbstraction. More...

#include <MinimalSectorAbstraction.h>

Collaboration diagram for MinimalSectorAbstraction:
Collaboration graph
[legend]

Public Member Functions

 MinimalSectorAbstraction (Map *map, int sectorSize)
 MinimalSectorAbstraction::MinimalSectorAbstraction() More...
 
void OpenGLDraw ()
 MinimalSectorAbstraction::OpenGLDraw() More...
 
void Draw (Graphics::Display &display) const
 
int GetSector (int x, int y)
 MinimalSectorAbstraction::GetSector() More...
 
int GetRegion (int x, int y)
 MinimalSectorAbstraction::GetRegion() More...
 
void GetXYLocation (unsigned int sector, unsigned int region, unsigned int &x, unsigned int &y) const
 MinimalSectorAbstraction::GetXYLocation() More...
 
void GetNeighbors (unsigned int sector, unsigned int region, std::vector< tempEdgeData > &edges) const
 MinimalSectorAbstraction::GetNeighbors() More...
 
int GetAdjacentSector (unsigned int sector, int direction)
 MinimalSectorAbstraction::GetAdjacentSector() More...
 
void OptimizeRegionLocations ()
 MinimalSectorAbstraction::OptimizeRegionLocations() More...
 
void InitializeOptimization ()
 MinimalSectorAbstraction::InitializeOptimization() More...
 
bool PerformOneOptimizationStep ()
 MinimalSectorAbstraction::PerformOneOptimizationStep() More...
 
int GetAbstractionBytesUsed ()
 

Private Member Functions

void BuildAbstraction ()
 MinimalSectorAbstraction::BuildAbstraction() More...
 
void GetEdges (std::vector< std::vector< int > > &areas, int xSector, int ySector, std::vector< tempEdgeData > &edges)
 MinimalSectorAbstraction::GetEdges() More...
 
void GetEdgeHelper (std::vector< int > &startRegion, int startIndex, int startOffset, std::vector< int > &targetRegion, int tarGetIndex, int targetOffset, std::vector< tempEdgeData > &edges, int direction)
 MinimalSectorAbstraction::GetEdgeHelper() More...
 
int GetSectorRegions (std::vector< int > &area, int absXSector, int absYSector)
 MinimalSectorAbstraction::GetSectorRegions() More...
 
void LabelRegion (std::vector< int > &area, int x, int y, int label)
 MinimalSectorAbstraction::LabelRegion() More...
 
void StoreSectorInMemory (sectorInfo &si, std::vector< int > &area, std::vector< tempEdgeData > &edges)
 MinimalSectorAbstraction::StoreSectorInMemory() More...
 
uint8_t GetAbstractEdge (tempEdgeData &data)
 MinimalSectorAbstraction::GetAbstractEdge() More...
 
uint8_t GetAbstractLocation (std::vector< int > area, int value)
 MinimalSectorAbstraction::GetAbstractLocation() More...
 
int FindParentRegion (int startLoc, std::vector< int > &parents, int mapXOffset, int mapYOffset)
 MinimalSectorAbstraction::FindParentRegion() More...
 
double GetRegionError (int fromSector, int fromRegion, int sx, int sy, double limit)
 MinimalSectorAbstraction::GetRegionError() More...
 
void MoveRegionCenter (int sector, int region)
 MinimalSectorAbstraction::MoveRegionCenter() More...
 
void ComputePotentialMemorySavings ()
 MinimalSectorAbstraction::ComputePotentialMemorySavings() More...
 
void ResetAbstractCenter (int sector, int region)
 MinimalSectorAbstraction::ResetAbstractCenter() More...
 

Private Attributes

int numXSectors
 
int numYSectors
 
std::vector< sectorInfosectors
 
std::vector< uint8_t > memory
 
std::vector< std::vector< double > > regionError
 
Mapmap
 
std::vector< std::vector< int > > areas
 
int sectorSize
 
int optimizationIndex
 

Detailed Description

MinimalSectorAbstraction.

Builds a map abstraction

Builds a map abstraction as described in AI Game Programming Wisdom 4.

The abstraction is optimized to use relatively little memory. This is the first version of the abstraction that I wrote. The code could be a bit cleaner...but it works

Definition at line 92 of file MinimalSectorAbstraction.h.

Constructor & Destructor Documentation

◆ MinimalSectorAbstraction()

MinimalSectorAbstraction::MinimalSectorAbstraction ( Map m,
int  theSectorSize 
)

MinimalSectorAbstraction::MinimalSectorAbstraction()

Constructor for minimal sector abstraction

Parameters
mThe map for which the abstraction is created
Returns
none

Definition at line 52 of file MinimalSectorAbstraction.cpp.

References BuildAbstraction(), Map::GetMapHeight(), Map::GetMapWidth(), map, numXSectors, numYSectors, optimizationIndex, sectors, and sectorSize.

Member Function Documentation

◆ BuildAbstraction()

void MinimalSectorAbstraction::BuildAbstraction ( )
private

MinimalSectorAbstraction::BuildAbstraction()

Start the abstraction buildling process.

The abstraction is built in 3 steps. First, we identify the regions in each sector. Then, we find the edges between each region. Finally we store a compact representation of the abstraction. An optimization phase can optionally be run.

As this isn't production code, we don't actually free any of the memory allocated during the construction of the abstraction.

Parameters
none
Returns
none

Definition at line 81 of file MinimalSectorAbstraction.cpp.

References areas, ComputePotentialMemorySavings(), GetEdges(), GetSectorRegions(), memory, numXSectors, numYSectors, sectors, and StoreSectorInMemory().

Referenced by MinimalSectorAbstraction().

◆ ComputePotentialMemorySavings()

void MinimalSectorAbstraction::ComputePotentialMemorySavings ( )
private

MinimalSectorAbstraction::ComputePotentialMemorySavings()

print how much memory would be saved using 'default' sectors

print how much memory would be saved using 'default' sectors

Parameters
none
Returns
none

Definition at line 1146 of file MinimalSectorAbstraction.cpp.

References GetNeighbors(), and sectors.

Referenced by BuildAbstraction().

◆ Draw()

void MinimalSectorAbstraction::Draw ( Graphics::Display display) const

◆ FindParentRegion()

int MinimalSectorAbstraction::FindParentRegion ( int  startLoc,
std::vector< int > &  parents,
int  mapXOffset,
int  mapYOffset 
)
private

MinimalSectorAbstraction::FindParentRegion()

Helper function for GetRegion()

Does a breadth-first search looking for the region associated with a particular point.

Parameters
startLocThe initial offset in the region
parentsThe possible region centers
mapXOffsetThe x offset of the top of the sector
mapYOffsetThe y offset of the top of the sector
Returns
The region the point is in

Definition at line 821 of file MinimalSectorAbstraction.cpp.

References Map::GetTerrainType(), kGround, map, and sectorSize.

Referenced by GetRegion().

◆ GetAbstractEdge()

uint8_t MinimalSectorAbstraction::GetAbstractEdge ( tempEdgeData data)
private

MinimalSectorAbstraction::GetAbstractEdge()

Turn the edge data structure into a compressed edge.

A quick helper function for turning tempEdgeData into a compressed (1byte) edge representation.

Parameters
dataedge data
Returns
A 1 byte representation of an edge

Definition at line 365 of file MinimalSectorAbstraction.cpp.

References tempEdgeData::direction, and tempEdgeData::to.

Referenced by StoreSectorInMemory().

◆ GetAbstractionBytesUsed()

int MinimalSectorAbstraction::GetAbstractionBytesUsed ( )
inline

Definition at line 108 of file MinimalSectorAbstraction.h.

References memory, and sectors.

◆ GetAbstractLocation()

uint8_t MinimalSectorAbstraction::GetAbstractLocation ( std::vector< int >  area,
int  value 
)
private

MinimalSectorAbstraction::GetAbstractLocation()

Choose a region center.

Find a region center, placing it at the point closest to the average weight of all points in the region.

Parameters
areaThe sector data for the computation
valueThe region number we are trying to analyze
Returns
The offset of the region center from the sector start

Definition at line 386 of file MinimalSectorAbstraction.cpp.

References sectorSize.

Referenced by ResetAbstractCenter(), and StoreSectorInMemory().

◆ GetAdjacentSector()

int MinimalSectorAbstraction::GetAdjacentSector ( unsigned int  sector,
int  direction 
)

MinimalSectorAbstraction::GetAdjacentSector()

Returns the sector in a given direction

Given a direction and a sector, compute which sector is immediately adjacent in that direction.

Parameters
sectorThe sector to use
directionThe direction to use
Returns
The sector in the particular direction

Definition at line 735 of file MinimalSectorAbstraction.cpp.

References numXSectors.

Referenced by Map2DSectorAbstraction::ApplyAction(), and Map2DSectorAbstraction::GetSuccessors().

◆ GetEdgeHelper()

void MinimalSectorAbstraction::GetEdgeHelper ( std::vector< int > &  startRegion,
int  startIndex,
int  startOffset,
std::vector< int > &  targetRegion,
int  targetIndex,
int  targetOffset,
std::vector< tempEdgeData > &  edges,
int  direction 
)
private

MinimalSectorAbstraction::GetEdgeHelper()

Generic procedure for iterating along the edge of a sector checking for edges.

GetEdgeHelper takes two (assumed) adjacent sectors and checks those sectors for all edges between them. For each sector a startIndex and offset is used so that the same code can be used for any two edges. Checks begin between the startIndex and targetIndex and at each step the indices are updated by the offset.

Parameters
startRegionThis vector holds the region data for the starting sector
startIndexThe first index to check in the startRegion
startOffsetThe offset (step) between indices we want to compare
targetRegionThe region data for the goal sector
targetIndexThe first index to check in the target region
targetOffsetThe offset (step) between indices we want to compare
edgesThe storage for any edges we find
directionThe direction that all edges will go
Returns
none

Definition at line 258 of file MinimalSectorAbstraction.cpp.

References tempEdgeData::direction, tempEdgeData::from, sectorSize, and tempEdgeData::to.

Referenced by GetEdges().

◆ GetEdges()

void MinimalSectorAbstraction::GetEdges ( std::vector< std::vector< int > > &  areas,
int  xSector,
int  ySector,
std::vector< tempEdgeData > &  edges 
)
private

MinimalSectorAbstraction::GetEdges()

Check for edges in and out of a sector.

This is the high-level code which checks for edges between sectors

Parameters
areasAn array which marks the region for each piece of the map
xSectorThe x-sector which we are checking
ySectorThe y-sector which we are checking
edgesA vector of edge data holding the edges from this particular region
Returns
none

Definition at line 132 of file MinimalSectorAbstraction.cpp.

References areas, tempEdgeData::direction, tempEdgeData::from, GetEdgeHelper(), numXSectors, numYSectors, sectorSize, and tempEdgeData::to.

Referenced by BuildAbstraction().

◆ GetNeighbors()

void MinimalSectorAbstraction::GetNeighbors ( unsigned int  sector,
unsigned int  region,
std::vector< tempEdgeData > &  edges 
) const

MinimalSectorAbstraction::GetNeighbors()

Find the edges in/out of the given sector

Extract the edges from the abstraction and return them.

Parameters
sectorThe sector to use
regionThe region to use
edgesOn return contains the edges from this sector/region
Returns
none

Definition at line 691 of file MinimalSectorAbstraction.cpp.

References tempEdgeData::direction, tempEdgeData::from, memory, sectors, and tempEdgeData::to.

Referenced by ComputePotentialMemorySavings(), Draw(), Map2DSectorAbstraction::GetActions(), Map2DSectorAbstraction::GetSuccessors(), and OpenGLDraw().

◆ GetRegion()

int MinimalSectorAbstraction::GetRegion ( int  x,
int  y 
)

MinimalSectorAbstraction::GetRegion()

Find the region associated with an x/y coordinate

Returns the region of the sector that the x/y coordinate is in. If coordinates are invalid, returns -1.

Parameters
xThe x-coordinate for the computation
yThe y-coordinate for the computation
Returns
The region for this coordinate

Definition at line 789 of file MinimalSectorAbstraction.cpp.

References FindParentRegion(), GetSector(), Map::GetTerrainType(), kGround, map, memory, sectors, and sectorSize.

Referenced by Map2DSectorAbstraction::GetAbstractState().

◆ GetRegionError()

double MinimalSectorAbstraction::GetRegionError ( int  fromSector,
int  fromRegion,
int  sx,
int  sy,
double  limit 
)
private

MinimalSectorAbstraction::GetRegionError()

Compute the "error" for the current region center

Given a point within a region, compute the cost between that point and every neighboring region. Use that cost to formulate an "error" term which is returned.

In this case we compuate the number of nodes expanded above what is optimal (the optimal path length)

An error bound is used to reduce computation. If this point is too expensive (above the error limit) we can stop early.

Parameters
fromSectorThe sector we start in
fromRegionThe region we start in
sxThe start x-location in the region
syThe start y-location in the region
limitIf the error exceeds this limit we can stop
Returns
none

Definition at line 1094 of file MinimalSectorAbstraction.cpp.

Referenced by MoveRegionCenter().

◆ GetSector()

int MinimalSectorAbstraction::GetSector ( int  x,
int  y 
)

MinimalSectorAbstraction::GetSector()

Given an x/y coordinate, return the sector

Given an x/y coordinate, return the sector

Parameters
xThe x-coordinate for the computation
yThe y-coordinate for the computation
Returns
The sector of the x/y coordinate

Definition at line 765 of file MinimalSectorAbstraction.cpp.

References numXSectors, numYSectors, and sectorSize.

Referenced by Map2DSectorAbstraction::GetAbstractState(), and GetRegion().

◆ GetSectorRegions()

int MinimalSectorAbstraction::GetSectorRegions ( std::vector< int > &  area,
int  absXSector,
int  absYSector 
)
private

MinimalSectorAbstraction::GetSectorRegions()

Do a BFS to find and label the regions in a sector

Does a BFS to find and label all the regions in a sector. Used when building the abstraction

Parameters
areaData structure containing the labelled points
absXSectorThe x offset of the sector
absYSectorThe y offset of the sector
Returns
The number of regions in the sector

Definition at line 888 of file MinimalSectorAbstraction.cpp.

References Map::GetTerrainType(), kGround, LabelRegion(), map, and sectorSize.

Referenced by BuildAbstraction().

◆ GetXYLocation()

void MinimalSectorAbstraction::GetXYLocation ( unsigned int  sector,
unsigned int  region,
unsigned int &  x,
unsigned int &  y 
) const

MinimalSectorAbstraction::GetXYLocation()

Given a sector and region, compute the x/y location.

Given a sector and region, compute the x/y location.

Parameters
sectorthe sector to use
regionthe region to use
xthe x-coordinate of the region center
ythe y-coordinate of the region center
Returns
none

Definition at line 667 of file MinimalSectorAbstraction.cpp.

References memory, numXSectors, sectors, and sectorSize.

Referenced by Draw(), Map2DSectorAbstraction::GetState(), and OpenGLDraw().

◆ InitializeOptimization()

void MinimalSectorAbstraction::InitializeOptimization ( )

MinimalSectorAbstraction::InitializeOptimization()

Initialize variables for optimization

Optimization can be run in one step or incrementally. This initializes the variables to run the optimization incrementally.

Parameters
none
Returns
none

Definition at line 962 of file MinimalSectorAbstraction.cpp.

References optimizationIndex, regionError, and sectors.

Referenced by OptimizeRegionLocations().

◆ LabelRegion()

void MinimalSectorAbstraction::LabelRegion ( std::vector< int > &  area,
int  x,
int  y,
int  label 
)
private

MinimalSectorAbstraction::LabelRegion()

Do a BFS within a region to label it

Performs the BFS within one region to label that region.

Parameters
areaThe stored labels
xThe starting location for the BFS
yThe starting location for the BFS
labelThe label to use
Returns
none

Definition at line 936 of file MinimalSectorAbstraction.cpp.

References sectorSize.

Referenced by GetSectorRegions().

◆ MoveRegionCenter()

void MinimalSectorAbstraction::MoveRegionCenter ( int  sector,
int  region 
)
private

MinimalSectorAbstraction::MoveRegionCenter()

Optimize a region center

Try every possible location for a region center. Choose the one which minimizes the error returned by GetRegionError.

Parameters
sectorThe sector to test
regionThe region in the sector to test
Returns
none

Definition at line 1048 of file MinimalSectorAbstraction.cpp.

References areas, fless(), GetRegionError(), memory, numXSectors, regionError, sectors, and sectorSize.

◆ OpenGLDraw()

void MinimalSectorAbstraction::OpenGLDraw ( )

MinimalSectorAbstraction::OpenGLDraw()

Draw the abstraction using OpenGL

Draws the abstraction & edges using OpenGL. If the abstraction is being optimized, draws the edges being optimized in red.

Parameters
none
Returns
none

Definition at line 444 of file MinimalSectorAbstraction.cpp.

References GetNeighbors(), Map::GetOpenGLCoord(), GetXYLocation(), map, numXSectors, numYSectors, optimizationIndex, sectors, and sectorSize.

◆ OptimizeRegionLocations()

void MinimalSectorAbstraction::OptimizeRegionLocations ( )

MinimalSectorAbstraction::OptimizeRegionLocations()

Perform complete optimization on map.

Parameters
none
Returns
none

Definition at line 1030 of file MinimalSectorAbstraction.cpp.

References InitializeOptimization(), and PerformOneOptimizationStep().

◆ PerformOneOptimizationStep()

bool MinimalSectorAbstraction::PerformOneOptimizationStep ( )

MinimalSectorAbstraction::PerformOneOptimizationStep()

Perform one optimization step. Must be called after InitializeOptimization().

Perform a single optimization on the next sector to be optimized.

Parameters
none
Returns
true if the optimization is finished, false otherwise

Definition at line 978 of file MinimalSectorAbstraction.cpp.

References optimizationIndex, and sectors.

Referenced by OptimizeRegionLocations().

◆ ResetAbstractCenter()

void MinimalSectorAbstraction::ResetAbstractCenter ( int  sector,
int  region 
)
private

MinimalSectorAbstraction::ResetAbstractCenter()

Reset a region to the weighted average location.

Resets a region center to the weighted average of the region. This is used when optimizing an abstraction a second time to reset the location of the abstract center.

Sometimes more than one region center will get moved to correct for some topology feature, when only one region center really needs to be moved. By moving the region center back to the default location we help improve this case when running optimization a second time

Parameters
sectorThe sector to reset
regionThe region in the sector to reset
Returns
none

Definition at line 1016 of file MinimalSectorAbstraction.cpp.

References areas, GetAbstractLocation(), memory, and sectors.

◆ StoreSectorInMemory()

void MinimalSectorAbstraction::StoreSectorInMemory ( sectorInfo si,
std::vector< int > &  area,
std::vector< tempEdgeData > &  edges 
)
private

MinimalSectorAbstraction::StoreSectorInMemory()

Store a sector and its edges/regions into the compressed data structure

Store a sector into the compressed representation. Takes the edges already computed and also computes the center of each region.

Parameters
siThe data structure with the high-level sector information
areaThe region we are storing information for
edgesa list of edges from this sector
Returns
none

Definition at line 314 of file MinimalSectorAbstraction.cpp.

References GetAbstractEdge(), GetAbstractLocation(), memory, sectorInfo::memoryAddress, sectorInfo::numEdges, and sectorInfo::numRegions.

Referenced by BuildAbstraction().

Member Data Documentation

◆ areas

std::vector<std::vector<int> > MinimalSectorAbstraction::areas
private

◆ map

Map* MinimalSectorAbstraction::map
private

◆ memory

std::vector<uint8_t> MinimalSectorAbstraction::memory
private

◆ numXSectors

int MinimalSectorAbstraction::numXSectors
private

◆ numYSectors

int MinimalSectorAbstraction::numYSectors
private

◆ optimizationIndex

int MinimalSectorAbstraction::optimizationIndex
private

◆ regionError

std::vector<std::vector<double> > MinimalSectorAbstraction::regionError
private

Definition at line 144 of file MinimalSectorAbstraction.h.

Referenced by InitializeOptimization(), and MoveRegionCenter().

◆ sectors

std::vector<sectorInfo> MinimalSectorAbstraction::sectors
private

◆ sectorSize

int MinimalSectorAbstraction::sectorSize
private

The documentation for this class was generated from the following files: