Exemple #1
0
Map *ReduceMap(Map *inputMap)
{
	Graph *g = GraphSearchConstants::GetGraph(inputMap);

	int biggest = LabelConnectedComponents(g);
	
	Map *m = new Map(inputMap->GetMapWidth(), inputMap->GetMapHeight());
	for (int x = 0; x < inputMap->GetMapWidth(); x++)
	{
		for (int y = 0; y < inputMap->GetMapHeight(); y++)
		{
			if (inputMap->GetTerrainType(x, y) == kTrees)
				m->SetTerrainType(x, y, kTrees);
			else if (inputMap->GetTerrainType(x, y) == kWater)
				m->SetTerrainType(x, y, kWater);			
			else m->SetTerrainType(x, y, kOutOfBounds);
		}
	}
	for (int x = 0; x < g->GetNumNodes(); x++)
	{
		if (g->GetNode(x)->GetLabelL(GraphSearchConstants::kTemporaryLabel) == biggest)
		{
			int theX, theY;
			theX = g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapX);
			theY = g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapY);
			if (g->GetNode(inputMap->GetNodeNum(theX+1, theY)) &&
				(g->GetNode(inputMap->GetNodeNum(theX+1, theY))->GetLabelL(GraphSearchConstants::kTemporaryLabel) == biggest) &&
				(!g->FindEdge(x, inputMap->GetNodeNum(theX+1, theY))))
			{
				m->SetTerrainType(theX, theY, kOutOfBounds);
			}
			else if (g->GetNode(inputMap->GetNodeNum(theX, theY+1)) &&
					 (g->GetNode(inputMap->GetNodeNum(theX, theY+1))->GetLabelL(GraphSearchConstants::kTemporaryLabel) == biggest) &&
					 (!g->FindEdge(x, inputMap->GetNodeNum(theX, theY+1))))
			{
				m->SetTerrainType(theX, theY, kOutOfBounds);
			}
//			else if (g->GetNode(inputMap->GetNodeNum(theX+1, theY+1)) &&
//					 (g->GetNode(inputMap->GetNodeNum(theX+1, theY+1))->GetLabelL(GraphSearchConstants::kTemporaryLabel) == biggest) &&
//					 (!g->FindEdge(x, inputMap->GetNodeNum(theX+1, theY+1))))
//			{
//				m->SetTerrainType(theX, theY, kOutOfBounds);
//			}
			else {
				if (inputMap->GetTerrainType(theX, theY) == kSwamp)
					m->SetTerrainType(theX, theY, kSwamp);
				else
					m->SetTerrainType(theX, theY, kGround);
			}
		}
		else if (inputMap->GetTerrainType(g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapX),
										  g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapY)) == kGround)
			m->SetTerrainType(g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapX),
							  g->GetNode(x)->GetLabelL(GraphSearchConstants::kMapY), kTrees);
	}
	return m;
}
void MapLineAbstraction::addNodes(Graph *g)
{
	int count = abstractions.back()->GetNumNodes();
	//printf("Initial count: %d\n", count);
	int xstep = pow(lineDistance,((abstractions.size()+1)/2));
	int ystep = pow(lineDistance,((abstractions.size())/2));
	int zstep = pow(lineDistance,((abstractions.size()-1)/2));
	Graph *toAbstract = abstractions.back();
	//printf("Size: %d.[%d] XStep: %d, YStep: %d, ZStep %d\n", abstractions.size(), lineDistance, xstep, ystep, zstep);

	if (abstractUniformly)
	{
		for (int x = 0; x < GetMap()->GetMapWidth(); x += xstep)
		{
			for (int y = 0; y < GetMap()->GetMapHeight(); y+= ystep)
			{
				//printf("Next check starts from (%d, %d)\n", x, y);
				std::vector<node *> nodes;
				std::vector<node *> parents;
				for (int z = 0; z < lineDistance; z++)
				{
					if ((abstractions.size()%2) != 0) // vertical abstraction
					{
						//printf("->(%d, %d)\n", x+z*zstep, y);
						nodes.push_back(GetNodeFromMap(x+z*zstep, y));
					}
					else {
						//printf("->(%d, %d)\n", x, y+z*zstep);
						nodes.push_back(GetNodeFromMap(x, y+z*zstep));
					}
					
					if (nodes.back() == 0)
					{
						//printf("(null)\n");
						nodes.resize(0);
						break;
					}
					parents.push_back(GetNthParent(nodes.back(), abstractions.size()-1));
					if ((parents.back() == 0) ||
							(parents.back()->GetLabelL(kParent) != -1) ||
							((z > 0) && (!toAbstract->FindEdge(parents[z]->GetNum(), parents[z-1]->GetNum()))))
					{
						//if (parents.back() == 0) printf("(null)\n");
						//else if (parents.back()->GetLabelL(kParent) != -1) printf("(parent)\n");
						//else printf("(disconnected)\n");
						parents.resize(0);
						break;
					}
				}
				if ((nodes.size() == 0) || (parents.size() == 0))
					continue;
				//printf("Match!\n");
				node *parent = createParent(g, parents[0]);
				//printf("-->Creating parent (%d)\n", parent->GetNum());
				for (unsigned int z = 0; z < parents.size(); z++)
				{
					if (parents[z]->GetLabelL(kParent) != -1)
						break;
					buildNodeIntoParent(parents[z], parent);
					count--;
					//printf("Abstracting %d, count now %d\n", parents[z]->GetNum(), count);
				}
			}
		}
	}
	
//	node_iterator ni = toAbstract->getNodeIter();
//	for (node *n = toAbstract->nodeIterNext(ni); n; n = toAbstract->nodeIterNext(ni))
	std::vector<node *> stopList; // nodes with no parents aren't abstracted
	while (count > 0)
	{
		// select a random node
		node *n = abstractions.back()->GetRandomNode();
		assert(n!=NULL);
		if ((n->GetLabelL(kParent) == -1) && (n->GetNumEdges() != 0))
		{
			node *parent = createParent(g, n);
			//printf("==>Creating parent (%d)\n", parent->GetNum());
			buildNodeIntoParent(n, parent);
			count -= 1;
			//printf("Abstracting %d, count now %d\n", n->GetNum(), count);

			for (int x = 0; x < lineDistance - 1; x++)
			{
				node *choice = 0;
				double prob = 0;
				neighbor_iterator nbi = n->getNeighborIter();
				for (long tmp = n->nodeNeighborNext(nbi); tmp != -1; tmp = n->nodeNeighborNext(nbi))
				{
					node *neighbor = toAbstract->GetNode(tmp);
					if (neighbor->GetLabelL(kParent) == -1)
					{
						prob++;
						if ((random()%100) < 100.0/prob)
						{
							choice = neighbor;
						}
					}
				}
				if (choice)
				{
					buildNodeIntoParent(choice, parent);
					count -= 1;
					n = choice;
					//printf("Abstracting %d, count now %d\n", n->GetNum(), count);
				}
				else {
					break;
				}
			}

		}
		else if (n->GetNumEdges() == 0)
		{
			if ((n->key < stopList.size()) && (stopList[n->key] == n))
			{
				//printf("Already Removed %d from consideration\n", n->GetNum());
			}
			else {
				//printf("Removing %d from consideration\n", n->GetNum());
				count--;
				n->key = stopList.size();
				stopList.push_back(n);
				//printf("Abstracting [%d], count now %d\n", n->GetNum(), count);
			}
		}
	}
}
Exemple #3
0
Graph *BuildInconsistentGraph(int d, int w, int h, int c1, int c2)
{
	//int d = 2;
	gFrom = d*w*h-1;
	gTo = 0;
	
	Graph *g = new Graph();
	int cost[d][w][h];
	int index[d][w][h];
	for (int z = 0; z < d; z++)
	{
		for (int x = 0; x < w; x++)
		{
			for (int y = 0; y < h; y++)
			{
				node *n;
				cost[z][x][y] = MAXINT;
				index[z][x][y] = g->AddNode(n = new node(""));
				n->SetLabelF(GraphSearchConstants::kXCoordinate, -1.0+2.0*(double)x/(double)(w-1.0));
				n->SetLabelF(GraphSearchConstants::kYCoordinate, -1.0+2.0*(double)y/(double)(h-1.0));
				if (d > 1)
					n->SetLabelF(GraphSearchConstants::kZCoordinate, -1.0+2.0*(double)z/(double)(d-1.0));
				else
					n->SetLabelF(GraphSearchConstants::kZCoordinate, 0);
			}
		}
	}
	for (int z = 0; z < d; z++)
	{
		for (int x = 0; x < w; x++)
		{
			for (int y = 0; y < h; y++)
			{
				if ((x == 0) && (y == 0) && (z == 0))
				{
					if (d > 1)
						g->AddEdge(new edge(index[z+1][x][y], index[z][x][y], c2));
					if (w > 1)
						g->AddEdge(new edge(index[z][x+1][y], index[z][x][y], c2));
					g->AddEdge(new edge(index[z][x][y+1], index[z][x][y], c2));
					continue;
				}
				if (z+1 <d)
					g->AddEdge(new edge(index[z+1][x][y], index[z][x][y], 1+random()%c1));
				if (x+1 <w)
					g->AddEdge(new edge(index[z][x+1][y], index[z][x][y], 1+random()%c1));
				if (y+1 < h)
					g->AddEdge(new edge(index[z][x][y+1], index[z][x][y], 1+random()%c1));
			}
		}
	}
	cost[0][0][0] = 0;
	while (1)
	{
		int changes = 0;

		for (int z = 0; z < d; z++)
		{
			for (int x = 0; x < w; x++)
			{
				for (int y = 0; y < h; y++)
				{
					if (z+1 <d)
					{
						int from1 = index[z][x][y];
						int to1 = index[z+1][x][y];
						edge *e = g->FindEdge(from1, to1);
						if (cost[z+1][x][y] > cost[z][x][y] + e->GetWeight())
						{
							cost[z+1][x][y] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z+1][x][y])->GetWeight();
							changes++;
						}
					}
					if (x+1 <w)
					{
						if (cost[z][x+1][y] > cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x+1][y])->GetWeight())
						{
							cost[z][x+1][y] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x+1][y])->GetWeight();
							changes++;
						}
					}
					if (y+1 < h)
					{
						if (cost[z][x][y+1] > cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x][y+1])->GetWeight())
						{
							cost[z][x][y+1] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x][y+1])->GetWeight();
							changes++;
						}
					}
				}
			}
		}

		if (changes == 0)
			break;
	}
	
	for (int z = 0; z < d; z++)
	{
		for (int x = 0; x < w; x++)
		{
			for (int y = 0; y < h; y++)
			{
				//printf("cost of node %d (%d, %d) is %d\n", x, y, index[x][y], cost[x][y]);
				node *n = g->GetNode(index[z][x][y]);
				if (cost[z][x][y] == 0)
					n->SetLabelF(GraphSearchConstants::kHCost, 0);
				else {
					//int val = ((random()%4) == 0)?(random()%(cost[z][x][y]+1)):0;
					int val = random()%(cost[z][x][y]+1);
					//printf("Assigning h = %d\n", val);
					n->SetLabelF(GraphSearchConstants::kHCost, (double)val);
				}
			}
		}
	}
	return g;
}