示例#1
0
void removeElement(BinaryHeap<int> & h, int index){
	cout<< "Removing the number a position " << index << " from the heap." <<endl;
	int x = h.remove(index);
	cout<< x << " was successfully removed."<<endl;
	int size = h.size();
	cout<<"The size of the heap is now: "<<size<<endl;
}
示例#2
0
	void execute_new_sell_order(MarketInstruction * mi) {
		MarketInstruction * mostImportantBuyOrder = NULL;

		while(buyOrders->size() > 0) {
			if(mi->quantity == 0) break;

			mostImportantBuyOrder = buyOrders->rootElem();

			if(mi->price <= mostImportantBuyOrder->price) {

				unsigned int quantity = min(mi->quantity, mostImportantBuyOrder->quantity);

				mi->quantity -= quantity;
				mostImportantBuyOrder->quantity -= quantity;

				double price = mostImportantBuyOrder->price;

				fill(quantity, price, mostImportantBuyOrder->id, mi->id, mi->targetAssetName);

				if(mostImportantBuyOrder->quantity == 0) {
					buyOrders->deleteRootElem();
					free(mostImportantBuyOrder);
				}
			}
			else 
				break;
		}

		if(mi->quantity > 0) {
			rest_order(mi);
		}
	}
示例#3
0
void Pathfinder::search()
{
	init();

	BinaryHeap openHeap;

	mStart->g = 0;
	mStart->h = manhattan(mStart, mGoal);
	mStart->opened = true;

	openHeap.push(mStart);

	while (openHeap.size() > 0)
	{
		// Pop the node with the smallest f value
		Node* currentNode = openHeap.pop();

		// Check if we hit the target
		if (currentNode == mGoal)
		{
			// Compute the path and exit
			generatePath(currentNode);
			return;
		}

		currentNode->closed = true;

		std::vector<Node*> neighbors = getNeighborsAdjacentTo(currentNode);
		for (int i = 0; i < neighbors.size(); i++)
		{
			Node* neighbor = neighbors[i];
			if (neighbor->closed || neighbor->worldRef->getMaterial()->isCollidable())
				continue;

			int gScore = currentNode->g + 1;

			if(!neighbor->opened || gScore < neighbor->g)
			{
				neighbor->g = currentNode->g + 1;
				neighbor->h = manhattan(currentNode, neighbor);
				neighbor->parent = currentNode;
				neighbor->opened = true;
				openHeap.push(neighbor);
			}
		}
	}
}
示例#4
0
void repeatedBackwardAStar(bool** maze, int size, State* start, State* goal){
	int counter = 0;
	State** S = new State*[size];
	for (int i = 0; i < size; i++){
		S[i] = new State[size];
	}
	for (int r = 0; r < size; r++){
		for (int c = 0; c < size; c++){
			S[r][c].row = r;
			S[r][c].col = c;
			S[r][c].search = 0;
		}
	}

	while (!compareStatePos(start, goal)){
		counter = counter + 1;

		S[start->row][start->col].g = INF;
		S[start->row][start->col].search = counter;

	
		S[goal->row][goal->col].g = 0;
		S[goal->row][goal->col].h = manhattanDistance(start, goal);
		S[goal->row][goal->col].f = goal->g + goal->h;
		S[goal->row][goal->col].search = counter;

		BinaryHeap OPEN;
		list<State> CLOSED;

		// watch 4 states around the start state to update cost, if any 
		if (start->row > 0 && !maze[start->row - 1][start->col]){
			updateCostToInf(S, size, start->row - 1, start->col);
		}
		if (start->row< size - 1 && !maze[start->row + 1][start->col]){
			updateCostToInf(S, size, start->row + 1, start->col);
		}
		if (start->col > 0 && !maze[start->row][start->col - 1]){
			updateCostToInf(S, size, start->row, start->col - 1);
		}
		if (start->col < size - 1 && !maze[start->row][start->col + 1]){
			updateCostToInf(S, size, start->row, start->col + 1);
		}

		OPEN.insert(S[goal->row][goal->col]);
		computePath(S, maze, start, &OPEN, &CLOSED, counter, size, false);

		if (OPEN.size() == 0){
			cout << "I cannot reach the target\n";

			// clean up 
			for (int i = 0; i < size; i++){
				delete[] S[i];
			}
			delete[] S;

			return;
		}

		// follow the tree-pointers from sstart to sgoal 
		//stack<coord> path;
		coord current;
		current.row = start->row;
		current.col = start->col;
		//path.push(curCoord);

		while (current.row != goal->row || current.col != goal->col){
			int curRow = current.row;
			int curCol = current.col;
			
			// watch 4 states around this current state to update cost, if any 
			if (current.row > 0 && !maze[current.row - 1][current.col]){
				updateCostToInf(S, size, current.row - 1, current.col);
			}
			if (current.row < size - 1 && !maze[current.row + 1][current.col]){
				updateCostToInf(S, size, current.row + 1, current.col);
			}
			if (current.col > 0 && !maze[current.row][current.col - 1]){
				updateCostToInf(S, size, current.row, current.col - 1);
			}
			if (current.col < size - 1 && !maze[current.row][current.col + 1]){
				updateCostToInf(S, size, current.row, current.col + 1);
			}

			coord next; 

			next.row = S[curRow][curCol].treeRow;
			next.col = S[curRow][curCol].treeCol;
			int dir = direction(&current, &next);
			cout << "\tTried to move to: (" << next.row << ", " << next.col << ")... ";
			if (S[current.row][current.col].cost[dir] > 1){
				cout << "BLOCKED" << endl;
				start = &S[current.row][current.col];
				break;
			}
			cout << "OK! Moved start to: (" << next.row << ", " << next.col << ")" << endl;
			current = next;
		}
		start = &S[current.row][current.col];
	}

	cout << "I reached the target" << endl;


	// clean up 
	for (int i = 0; i < size; i++){
		delete[] S[i];
	}
	delete[] S;
}
示例#5
0
void getSize(BinaryHeap<int> &h){
	int size = h.size();
	cout<<"The size of the heap is: " << size<< endl;
}