e_brainState Brain::runFrame(Brain::s_brainBox situation) { currentSituation.survival = evaluateSurvival(situation); currentSituation.work = evaluateWork(situation); currentSituation.entertainment = 0; e_brainState newState = currentState; //std::cout << "Surival rating: " << currentSituation.survival << std::endl; //std::cout << "Work rating: " << currentSituation.work << std::endl; if ( currentSituation.survival >= currentSituation.work) { if (situation.survival.tired < 370) newState = e_idle; if (situation.survival.thirst > 500) newState = e_getWater; if (situation.survival.hunger > 500) newState = e_getFood; if (situation.survival.thirst < 500 && situation.survival.hunger < 500) newState = e_idle; if (situation.survival.tired > 800) newState = e_takeNap; } else { newState = makeDecision(situation); } if (situation.exclusions.size() != 0) { if (listContains(situation.exclusions,newState)) { newState = e_idle; } } // If the brain has changed its mind since the last time it made a decision // it announces that change and applies it to the current decision if (newState != currentState) { std::cout << "Brain state changed" << std::endl; std::cout << "New State: " << newState << std::endl; currentState = newState; } return currentState; }
//Main function int main() { timeval newTime; int tmp_return = -1; int ret = 0; unsigned long runTime = 0; float second = 0; List* node = NULL; List* result = NULL; memset (&newTime,0x00,sizeof(newTime)); //Initialize puzzle node = readNumber(); if (NULL == node){ printf ("Exit to input.\n"); return SUDOKU_NG; } //Change form to binary tmp_return = doBinChange (&(node->block)); if ( SUDOKU_NG == tmp_return){ printf ("Error in binary change.\n"); return SUDOKU_NG; } //Find the minimum subset while (1) { if ( 0 == node->block.number ){ printf ("Error to find result.\n"); return SUDOKU_NG; } tmp_return = findSubset(&(node->block)); if ( SUDOKU_NG == tmp_return){ printf ("Error in findSubset change.\n"); return SUDOKU_NG; } else if ( SUDOKU_OK == tmp_return ){ // Finish calculate, break break; } else if ( SUDOKU_DE == tmp_return ){ //Make decision result = makeDecision (node); if ( NULL == result){ printf ("Error in decision.\n"); return SUDOKU_NG; } node = result; result = NULL; } else if ( SUDOKU_RE == tmp_return ){ //Regress result = regress(node); if ( NULL == result){ printf ("Error in regress.\n"); return SUDOKU_NG; } node = result; result = NULL; } else { printf ("Error in findSubset change.\n"); return SUDOKU_NG; } } //change form to decimal doDecChange(&(node->block)); ret = gettimeofday( &newTime, NULL ); if (-1 == ret){ printf ("Call gettimeofday failed.\n"); } else { /* #if defined DEBUG printf ("Finished!Now sec is %u,Usec is %u.\n", newTime.tv_sec,newTime.tv_usec); #endif */ runTime = (newTime.tv_sec - currentTime.tv_sec)*1000000 + newTime.tv_usec - currentTime.tv_usec; second = runTime/1000000.0; printf ("Run time is %f second.\n",second); } //print result printBlock(&(node->block)); return SUDOKU_OK; }
RESULT Engine::search() { int starts = 0; int nof_conflicts = so.restart_base; int conflictC = 0; while (true) { if (so.parallel && slave.checkMessages()) return RES_UNK; if (!propagate()) { clearPropState(); Conflict: conflicts++; conflictC++; if (time(NULL) > so.time_out) { printf("Time limit exceeded!\n"); return RES_UNK; } if (decisionLevel() == 0) { return RES_GUN; } // Derive learnt clause and perform backjump if (so.lazy) { sat.analyze(); } else { sat.confl = NULL; DecInfo& di = dec_info.last(); sat.btToLevel(decisionLevel()-1); makeDecision(di, 1); } if (!so.vsids && !so.toggle_vsids && conflictC >= so.switch_to_vsids_after) { if (so.restart_base >= 1000000000) so.restart_base = 100; sat.btToLevel(0); toggleVSIDS(); } } else { if (conflictC >= nof_conflicts) { starts++; nof_conflicts += getRestartLimit((starts+1)/2); sat.btToLevel(0); sat.confl = NULL; if (so.lazy && so.toggle_vsids && (starts % 2 == 0)) toggleVSIDS(); continue; } if (decisionLevel() == 0) { topLevelCleanUp(); if (opt_var && so.verbosity >= 3) { printf("%% root level bounds on objective: min %d max %d\n", opt_var->getMin(), opt_var->getMax()); } } DecInfo *di = NULL; // Propagate assumptions while (decisionLevel() < assumptions.size()) { int p = assumptions[decisionLevel()]; if (sat.value(toLit(p)) == l_True) { // Dummy decision level: assert(sat.trail.last().size() == sat.qhead.last()); engine.dec_info.push(DecInfo(NULL, p)); newDecisionLevel(); } else if (sat.value(toLit(p)) == l_False) { return RES_LUN; } else { di = new DecInfo(NULL, p); break; } } if (!di) di = branching->branch(); if (!di) { solutions++; if (so.print_sol) { problem->print(); printf("----------\n"); fflush(stdout); } if (!opt_var) { if (solutions == so.nof_solutions) return RES_SAT; if (so.lazy) blockCurrentSol(); goto Conflict; } if (!constrain()) { return RES_GUN; } continue; } engine.dec_info.push(*di); newDecisionLevel(); doFixPointStuff(); makeDecision(*di, 0); delete di; } } }
void waitAndProcessMessages(pthread_t* threads, int port) { fd_set master, readfds; int sock, connectSockFd; int true1 = 1; char* sendCmd; struct sockaddr_in serv_address, cli_address; int sin_size; int recv_len; struct timeval tv; FD_ZERO(&readfds); FD_ZERO(&master); tv.tv_sec = 20; tv.tv_usec = 0; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { perror("Error creating socket"); exit(1); } if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &true1, sizeof(int)) == -1) { perror("Error setting socket options"); exit(1); } FD_SET(sock, &master); serv_address.sin_family = AF_INET; serv_address.sin_port = htons(port); serv_address.sin_addr.s_addr = INADDR_ANY; bzero(&(serv_address.sin_zero), 8); if (bind(sock, (struct sockaddr *) &serv_address, sizeof(struct sockaddr)) == -1) { perror("Error binding to the socket"); exit(1); } if (listen(sock, 100) == -1) { perror("Error listening to the socket"); exit(1); } int i = 1; while (true) { readfds = master; int val = select(sock + 1, &readfds, NULL, NULL, &tv); if (val == -1) { perror("Error Occured in Select"); exit(1); } else if (val == 0) { break; } else { if (FD_ISSET(sock, &readfds)) { connectSockets[i] = accept(sock, (struct sockaddr *) &cli_address, (socklen_t *) &sin_size); if (connectSockets[i] == -1) { perror("Error in accept"); break; } pthread_create(&threads[i], NULL, recvMesgAndSendToAll, (void *) &connectSockets[i]); i++; } } } for (int j = 1; j < i; j++) { pthread_join(threads[j], NULL); } makeDecision(); }