void processNode(Knapsack* ks, Heap* h, Node* n) { //printf("Process: value=%d, weight=%d, total value %d\n", item->_value, item->_weight, n->_value); char* x = (char*)malloc(sizeof(char)*(n->_depth+2)); int i; for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } Item* nextItem = ks->_items[n->_depth+1]; x[n->_depth+1] = 0; double est = upperBound(ks, n->_depth+2, n->_capacity); // Following the convention that choosing (x_i = 0) means "branch to the right" // Rounding up. double rightUB = n->_value + est; Node* rightNode = initNode(n->_value, n->_depth+1, rightUB, n->_capacity, x); inspectNode(ks, h, rightNode); if (nextItem->_weight <= n->_capacity) { x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 1; int leftValue = n->_value + nextItem->_value; int leftCapacity = n->_capacity - nextItem->_weight; est = upperBound(ks, n->_depth+2, leftCapacity); double leftUB = leftValue + est; Node* leftNode = initNode(leftValue, n->_depth+1, leftUB, leftCapacity, x); inspectNode(ks, h, leftNode); } destroyNode(n); }
void handleInspection() { if (nodeQueue->size == 0) { event_schedule(sim_time + 1, EVENT_END_SIMULATION); return; } Node* node = MTNodeQueue_next(nodeQueue); int status = inspectNode(node); int event_type; if (status == DIRTY) { event_type = node->isMeter ? EVENT_DIRTY_RESULT : EVENT_DIRTY_INSPECTOR; if (!node->isMeter) { if (inspectNode(node->leftChild) == CLEAN && !node->leftChild->isMeter && !node->rightChild->isMeter) { MTNodeQueue_push(node->rightChild->leftChild, nodeQueue); MTNodeQueue_push(node->rightChild->rightChild, nodeQueue); } else { MTNodeQueue_push(node->leftChild, nodeQueue); MTNodeQueue_push(node->rightChild, nodeQueue); } } } else { event_type = node->isMeter ? EVENT_CLEAN_RESULT : EVENT_CLEAN_INSPECTOR; } event_schedule(sim_time + 1, event_type); }
int inspectNode(Node* node) { if (node->isMeter || node->status > -1) { return node->status; } node->status = inspectNode(node->leftChild) || inspectNode(node->rightChild); return node->status; }
void processNode(Knapsack* ks, Heap* h, Node* n) { char* x; int i; double est; Item* nextItem = ks->_items[n->_depth+1]; // right node stuff x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 0; est = upperBound(ks, n->_depth+2, n->_capacity); double rightUB = n->_value + est; Node* rightNode = initNode(n->_value, n->_depth+1, rightUB, n->_capacity, x); //inspectNode(ks, h, rightNode); Node* leftNode = NULL; double leftUB = 0.0; // left node stuff if (nextItem->_weight <= n->_capacity) { x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 1; int leftValue = n->_value + nextItem->_value; int leftCapacity = n->_capacity - nextItem->_weight; est = upperBound(ks, n->_depth+2, leftCapacity); leftUB = leftValue + est; leftNode = initNode(leftValue, n->_depth+1, leftUB, leftCapacity, x); pthread_rwlock_rdlock(&ks->_lblock); if (leftUB >= (double)ks->_lowerBound) { pthread_rwlock_unlock(&ks->_lblock); inspectNode(ks, h, rightNode); tryProcessNode(ks, h, leftNode); } else { pthread_rwlock_unlock(&ks->_lblock); destroyNode(leftNode); destroyNode(rightNode); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed) += 2; pthread_mutex_unlock(&ks->_counterMutex); } } else { pthread_rwlock_rdlock(&ks->_lblock); if (rightUB >= (double)ks->_lowerBound) { pthread_rwlock_unlock(&ks->_lblock); tryProcessNode(ks, h, rightNode); } else { pthread_rwlock_unlock(&ks->_lblock); destroyNode(rightNode); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed)++; pthread_mutex_unlock(&ks->_counterMutex); } } destroyNode(n); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed)++; pthread_mutex_unlock(&ks->_counterMutex); }
AStar::SearchResult AStar::search(int step, AStarRecorder* recorder) { FDK_ASSERT(m_searchResult == SearchResult_Proceeding); if (!m_bInitedInspect) { inspectNode(m_startNodeID, INVALID_NODEID, 0, recorder); m_bInitedInspect = true; } int proceededStep = 0; std::vector<SuccessorNodeInfo> successors; while (!m_openList.empty()) { OpenListItem current = m_openList.front(); m_openList.pop_front(); if (m_nodeStates[current.nodeID] != NodeState_Open) { FDK_ASSERT(m_nodeStates[current.nodeID] == NodeState_Closed); continue; } m_nodeStates[current.nodeID] = NodeState_Closed; m_currentClosed = current; if (recorder) { recorder->onCloseNode(m_env, current.nodeID); } if (m_nodeDatas[current.nodeID].hValue < m_minHValue) { m_minHValue = m_nodeDatas[current.nodeID].hValue; m_closest = current; } bool bComplete = false; if (m_completeCondition) { bComplete = m_completeCondition->checkCondition(m_env, m_startNodeID, m_targetNodeID, current.nodeID); } else { bComplete = (current.nodeID == m_targetNodeID); } if (bComplete) { m_searchResult = SearchResult_Completed; return SearchResult_Completed; } successors.clear(); getSuccessorNodes(m_env, current.nodeID, m_nodeDatas[current.nodeID].parentNodeID, successors); for (size_t i = 0; i < successors.size(); ++i) { SuccessorNodeInfo& successor = successors[i]; inspectNode(successor.nodeID, current.nodeID, m_nodeDatas[current.nodeID].gValue+successor.cost, recorder); } ++proceededStep; if (step >=1 && proceededStep >= step) { return SearchResult_Proceeding; } } m_searchResult = SearchResult_PathUnexist; return SearchResult_PathUnexist; }