int init_game(const char *infile, const char *outfile) { FILE *f; if ((f = fopen(MACRO_FILE, "rb")) == NULL) log_status("INIT: Macro storage file %s is tweaked.", MACRO_FILE); else { macroload(f); fclose(f); } in_filename = (char *) string_dup(infile); if ((input_file = fopen(infile, "rb")) == NULL) return -1; #ifdef DELTADUMPS if ((delta_outfile = fopen(DELTAFILE_NAME, "wb")) == NULL) return -1; if ((delta_infile = fopen(DELTAFILE_NAME, "rb")) == NULL) return -1; #endif db_free(); init_primitives(); /* init muf compiler */ mesg_init(); /* init mpi interpreter */ SRANDOM(getpid()); /* init random number generator */ tune_load_parmsfile(NOTHING); /* load @tune parms from file */ /* ok, read the db in */ log_status("LOADING: %s", infile); fprintf(stderr, "LOADING: %s\n", infile); if (db_read(input_file) < 0) return -1; log_status("LOADING: %s (done)", infile); fprintf(stderr, "LOADING: %s (done)\n", infile); /* set up dumper */ if (dumpfile) free((void *) dumpfile); dumpfile = alloc_string(outfile); if (!db_conversion_flag) { /* initialize the _sys/startuptime property */ add_property((dbref) 0, "_sys/startuptime", NULL, (int) time((time_t *) NULL)); add_property((dbref) 0, "_sys/maxpennies", NULL, tp_max_pennies); add_property((dbref) 0, "_sys/dumpinterval", NULL, tp_dump_interval); add_property((dbref) 0, "_sys/max_connects", NULL, 0); } return 0; }
int he_main(int argc, char *argv[]) #endif { time_t seed; my_argv = argv; if (!strcmp(program_path, "") && argv) MakeProgramPath(argv[0]); /* Seed the random number generator */ #if !defined (RANDOM) srand((unsigned int)time((time_t *)&seed)); #else SRANDOM((unsigned int)time((time_t *)&seed)); #endif #if !defined (GLK) /* no command line under Glk */ ParseCommandLine(argc, argv); #endif hugo_init_screen(); #if defined (DEBUGGER) debug_getinvocationpath(argv[0]); SwitchtoGame(); #endif SetupDisplay(); strcpy(pbuffer, ""); gameseg = 0; LoadGame(); #if defined (DEBUGGER) LoadDebuggableFile(); StartDebugger(); #endif RunGame(); hugo_cleanup_screen(); hugo_blockfree(mem); mem = NULL; hugo_closefiles(); return 0; }
/** * Main random map routine. Generates a random map based on specified * parameters. * @param OutFileName * The path the map should have. * @param RP * Parameters for generation. * @return * Pointer to the generated map. */ mapstruct *generate_random_map(char *OutFileName, RMParms *RP) { char **layout; mapstruct *theMap; int i; /* pick a random seed, or use the one from the input file */ if (RP->random_seed == 0) { SRANDOM(time(0)); } else { SRANDOM(RP->random_seed); } if (RP->difficulty == 0) { /* use this instead of a map difficulty */ RP->difficulty = RP->dungeon_level; } else { RP->difficulty_given = 1; } if (RP->expand2x > 0) { RP->Xsize /= 2; RP->Ysize /= 2; } layout = layoutgen(RP); if (RP->level_increment > 0) { RP->dungeon_level += RP->level_increment; } else { RP->dungeon_level++; } /* rotate the layout randomly */ layout = rotate_layout(layout, RANDOM() % 4, RP); #ifdef RMAP_DEBUG dump_layout(layout, RP); #endif /* allocate the map and set the floor */ theMap = make_map_floor(RP->floorstyle, RP); /* set the name of the map. */ FREE_AND_COPY_HASH(theMap->path, OutFileName); FREE_AND_COPY_HASH(theMap->name, RP->dungeon_name[0] ? RP->dungeon_name : OutFileName); if (RP->bg_music[0] != '\0') { FREE_AND_COPY_HASH(theMap->bg_music, RP->bg_music); } theMap->difficulty = RP->dungeon_level; make_map_walls(theMap, layout, RP->wallstyle, RP); put_doors(theMap, layout, RP->doorstyle, RP); place_exits(theMap, layout, RP->exitstyle, RP->orientation, RP); place_monsters(theMap, RP->monsterstyle, RP->difficulty, RP); put_decor(theMap, layout, RP); unblock_exits(theMap, layout, RP); set_map_darkness(theMap, RP->darkness); for (i = 0; i < RP->Xsize; i++) { efree(layout[i]); } efree(layout); return theMap; }
int NetKeepaliveInit(char *src, char *dst, int maximal_keepalive, int family) { struct timeval tv; #ifdef WIN32 hndliphlp = LoadLibrary("IPHLPAPI.DLL"); if (!hndliphlp) { Display(LOG_LEVEL_3, ELError, "NetKeepaliveInit", HEX_STR_CANT_CREATE_ICMP_HNDL_FILE); return 1; } if (family == AF_INET6) { // Try to load IPv6 // support functions // from iphlpapi // Icmp6CreateFile = (HANDLE (WINAPI *)(void)) GetProcAddress(hndliphlp, "Icmp6CreateFile"); IcmpCloseHandle = (BOOL (WINAPI *)(HANDLE)) GetProcAddress(hndliphlp, "IcmpCloseHandle"); Icmp6SendEcho2 = (DWORD (WINAPI *)(HANDLE, HANDLE, FARPROC, PVOID, struct sockaddr_in6*, struct sockaddr_in6*, LPVOID, WORD, PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD)) GetProcAddress(hndliphlp, "Icmp6SendEcho2"); Icmp6ParseReplies = (DWORD (WINAPI *)(LPVOID, DWORD)) GetProcAddress(hndliphlp, "Icmp6ParseReplies"); if( (Icmp6CreateFile == NULL) || (IcmpCloseHandle == NULL) || (Icmp6SendEcho2 == NULL) || (Icmp6ParseReplies == NULL) ) { Display(LOG_LEVEL_3, ELError, "NetKeepaliveInit", HEX_STR_CANT_CREATE_ICMP_HNDL_FILE); return 1; } hHandle = Icmp6CreateFile(); } #ifdef V4V6_SUPPORT else if (family == AF_INET) { // Try to load IPv4 // support functions // from iphlpapi // IcmpCreateFile = (HANDLE (WINAPI *)(VOID)) GetProcAddress(hndliphlp, "IcmpCreateFile"); IcmpCloseHandle = (BOOL (WINAPI *)(HANDLE)) GetProcAddress(hndliphlp, "IcmpCloseHandle"); IcmpSendEcho2 = (DWORD (WINAPI *)(HANDLE, HANDLE, FARPROC, PVOID, IPAddr, LPVOID, WORD, PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD)) GetProcAddress(hndliphlp, "IcmpSendEcho2"); IcmpParseReplies = (DWORD (WINAPI *)(LPVOID, DWORD)) GetProcAddress(hndliphlp, "IcmpParseReplies"); if ( (IcmpCreateFile == NULL) || (IcmpCloseHandle == NULL) || (IcmpSendEcho2 == NULL) || (IcmpParseReplies == NULL) ) { Display(LOG_LEVEL_3, ELError, "NetKeepaliveInit", HEX_STR_CANT_CREATE_ICMP_HNDL_FILE); return 1; } hHandle = IcmpCreateFile(); } #endif if (hHandle == INVALID_HANDLE_VALUE) { Display(LOG_LEVEL_3, ELError, "NetKeepaliveInit", HEX_STR_CANT_CREATE_ICMP_HNDL_FILE); return 1; } #endif /* Load the structure with passed in AND * initial values */ seqn = 0; if (strncpy(ka.host, dst, sizeof(ka.host)) == NULL) return 1; ka.maximal_keepalive = maximal_keepalive; ka.current_keepalive = KA_INITIAL_KEEPALIVE; ka.major_fuzz = (float) 0.60; ka.minor_fuzz = (float) 0.075; /* And initialize the next event */ /* Initial timeout is set to five seconds */ GETTIMEOFDAY(&tv, NULL); ka.next_event.tv_sec = tv.tv_sec + KA_INITIAL_KEEPALIVE; ka.next_event.tv_usec = tv.tv_usec; /* initialize the random source */ SRANDOM(tv.tv_usec); /* set the family */ ka.family = family; /* get a ping socket and a monitor socket */ if (family == AF_INET6) { INET_PTON(AF_INET6, dst, &ka.sa6.sin6_addr); ka.sa6.sin6_family = AF_INET6; INET_PTON(AF_INET6, src, &ka.sa6_local.sin6_addr); ka.sa6_local.sin6_family = AF_INET6; #ifndef WIN32 ka.keepalive_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); #endif } #ifdef V4V6_SUPPORT else if (family == AF_INET) { INET_PTON(AF_INET, dst, &ka.sa.sin_addr); ka.sa.sin_family = AF_INET; INET_PTON(AF_INET, src, &ka.sa_local.sin_addr); ka.sa_local.sin_family = AF_INET; #ifndef WIN32 ka.keepalive_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); #endif } #endif /* V4V6_SUPPORT */ ka.consecutive_timeout = 0; ka.still_up = 0; ka.doit = 1; ka.count = 0; ka.got_read = 0; ka.got_write = 0; ka.pinged = 1; /* ok, we are inialized */ ka.init = 1; Display(LOG_LEVEL_3, ELInfo, "NetKeepaliveInit", HEX_STR_KEEPALIVE_INITIALIZED, dst, maximal_keepalive); return 0; }
/** * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator * can be seeded. * * @param seed the seed to use */ void GNUNET_CRYPTO_seed_weak_random (int32_t seed) { SRANDOM (seed); }
int user_find_cuts(void *user, int varnum, int iter_num, int level, int index, double objval, int *indices, double *values, double ub, double etol, int *num_cuts, int *alloc_cuts, cut_data ***cuts) { vrp_cg_problem *vrp = (vrp_cg_problem *)user; int vertnum = vrp->vertnum; network *n; vertex *verts = NULL; int *compdemands = NULL, *compnodes = NULL, *compnodes_copy = NULL; int *compmembers = NULL, comp_num = 0; /*__BEGIN_EXPERIMENTAL_SECTION__*/ int *compdemands_copy = NULL; double *compcuts_copy = NULL, *compdensity = NULL, density; /*___END_EXPERIMENTAL_SECTION___*/ double node_cut, max_node_cut, *compcuts = NULL; int rcnt, cur_bins = 0, k; char **coef_list; int i, max_node; double cur_slack = 0.0; int capacity = vrp->capacity; int cut_size = (vertnum >> DELETE_POWER) + 1; cut_data *new_cut = NULL; elist *cur_edge = NULL; int which_connected_routine = vrp->par.which_connected_routine; int *ref = vrp->ref; double *cut_val = vrp->cut_val; char *in_set = vrp->in_set; char *cut_list = vrp->cut_list; elist *cur_edge1 = NULL, *cur_edge2 = NULL; /*__BEGIN_EXPERIMENTAL_SECTION__*/ #ifdef COMPILE_OUR_DECOMP edge *edge_pt; #endif /*___END_EXPERIMENTAL_SECTION___*/ int node1 = 0, node2 = 0; int *demand = vrp->demand; int *new_demand = vrp->new_demand; int total_demand = demand[0]; int num_routes = vrp->numroutes, num_trials; int triangle_cuts = 0; char *coef; if (iter_num == 1) SRANDOM(1); /*__BEGIN_EXPERIMENTAL_SECTION__*/ #if 0 CCutil_sprand(1, &rand_state); #endif /*___END_EXPERIMENTAL_SECTION___*/ /*__BEGIN_EXPERIMENTAL_SECTION__*/ #if 0 if (vrp->dg_id && vrp->par.verbosity > 3){ sprintf(name, "support graph"); display_support_graph(vrp->dg_id, (p->cur_sol.xindex == 0 && p->cur_sol.xiter_num == 1) ? TRUE: FALSE, name, varnum, xind, xval, etol, CTOI_WAIT_FOR_CLICK_AND_REPORT); } #endif /*___END_EXPERIMENTAL_SECTION___*/ /* This creates a fractional graph representing the LP solution */ n = createnet(indices, values, varnum, etol, vrp->edges, demand, vertnum); if (n->is_integral){ /* if the network is integral, check for connectivity */ check_connectivity(n, etol, capacity, num_routes, cuts, num_cuts, alloc_cuts); free_net(n); return(USER_SUCCESS); } #ifdef DO_TSP_CUTS if (vrp->par.which_tsp_cuts && vrp->par.tsp_prob){ tsp_cuts(n, vrp->par.verbosity, vrp->par.tsp_prob, vrp->par.which_tsp_cuts, cuts, num_cuts, alloc_cuts); free_net(n); return(USER_SUCCESS); } #endif /*__BEGIN_EXPERIMENTAL_SECTION__*/ if (!vrp->par.always_do_mincut){/*user_par.always_do_mincut indicates whether we should just always do the min_cut routine or whether we should also try this routine*/ /*___END_EXPERIMENTAL_SECTION___*/ /*UNCOMMENT FOR PRODUCTION CODE*/ #if 0 { #endif verts = n->verts; if (which_connected_routine == BOTH) which_connected_routine = CONNECTED; new_cut = (cut_data *) calloc(1, sizeof(cut_data)); new_cut->size = cut_size; compnodes_copy = (int *) malloc((vertnum + 1) * sizeof(int)); compmembers = (int *) malloc((vertnum + 1) * sizeof(int)); /*__BEGIN_EXPERIMENTAL_SECTION__*/ compdemands_copy = (int *) calloc(vertnum + 1, sizeof(int)); compcuts_copy = (double *) calloc(vertnum + 1, sizeof(double)); #ifdef COMPILE_OUR_DECOMP compdensity = vrp->par.do_our_decomp ? (double *) calloc(vertnum+1, sizeof(double)) : NULL; #endif /*___END_EXPERIMENTAL_SECTION___*/ do{ compnodes = (int *) calloc(vertnum + 1, sizeof(int)); compdemands = (int *) calloc(vertnum + 1, sizeof(int)); compcuts = (double *) calloc(vertnum + 1, sizeof(double)); /*------------------------------------------------------------------*\ * Get the connected components of the solution graph without the * depot and see if the number of components is more than one \*------------------------------------------------------------------*/ rcnt = (which_connected_routine == BICONNECTED ? biconnected(n, compnodes, compdemands, compcuts) : connected(n, compnodes, compdemands, compmembers, /*__BEGIN_EXPERIMENTAL_SECTION__*/ compcuts, compdensity)); /*___END_EXPERIMENTAL_SECTION___*/ /*UNCOMMENT FOR PRODUCTION CODE*/ #if 0 compcuts, NULL)); #endif /* copy the arrays as they will be needed later */ if (!which_connected_routine && /*__BEGIN_EXPERIMENTAL_SECTION__*/ (vrp->par.do_greedy || vrp->par.do_our_decomp)){ /*___END_EXPERIMENTAL_SECTION___*/ /*UNCOMMENT FOR PRODUCTION CODE*/ #if 0 vrp->par.do_greedy){ #endif compnodes_copy = (int *) memcpy((char *)compnodes_copy, (char*)compnodes, (vertnum+1)*sizeof(int)); /*__BEGIN_EXPERIMENTAL_SECTION__*/ compdemands_copy = (int *) memcpy((char *)compdemands_copy, (char *)compdemands, (vertnum+1)*ISIZE); compcuts_copy = (double *) memcpy((char *)compcuts_copy, (char *)compcuts, (vertnum+1)*DSIZE); /*___END_EXPERIMENTAL_SECTION___*/ n->compnodes = compnodes_copy; comp_num = rcnt; } if (rcnt > 1){ /*---------------------------------------------------------------*\ * If the number of components is more then one, then check each * component to see if it violates a capacity constraint \*---------------------------------------------------------------*/ coef_list = (char **) calloc(rcnt, sizeof(char *)); coef_list[0] = (char *) calloc(rcnt*cut_size, sizeof(char)); for(i = 1; i<rcnt; i++) coef_list[i] = coef_list[0]+i*cut_size; for(i = 1; i < vertnum; i++) (coef_list[(verts[i].comp)-1][i >> DELETE_POWER]) |= (1 << (i & DELETE_AND)); for (i = 0; i < rcnt; i++){ if (compnodes[i+1] < 2) continue; /*check ith component to see if it violates a constraint*/ if (vrp->par.which_connected_routine == BOTH && which_connected_routine == BICONNECTED && compcuts[i+1]==0) continue; if (compcuts[i+1] < 2*BINS(compdemands[i+1], capacity)-etol){ /*the constraint is violated so impose it*/ new_cut->coef = (char *) (coef_list[i]); new_cut->type = (compnodes[i+1] < vertnum/2 ? SUBTOUR_ELIM_SIDE:SUBTOUR_ELIM_ACROSS); new_cut->rhs = (new_cut->type == SUBTOUR_ELIM_SIDE ? RHS(compnodes[i+1],compdemands[i+1], capacity): 2*BINS(compdemands[i+1], capacity)); cg_send_cut(new_cut, num_cuts, alloc_cuts, cuts); } else{/*if the constraint is not violated, then try generating a violated constraint by deleting customers that don't change the number of trucks required by the customers in the component but decrease the value of the cut*/ cur_bins = BINS(compdemands[i+1], capacity);/*the current number of trucks required*/ /*current slack in the constraint*/ cur_slack = (compcuts[i+1] - 2*cur_bins); while (compnodes[i+1]){/*while there are still nodes in the component*/ for (max_node = 0, max_node_cut = 0, k = 1; k < vertnum; k++){ if (verts[k].comp == i+1){ if (BINS(compdemands[i+1]-verts[k].demand, capacity) == cur_bins){ /*if the number of trucks doesn't decrease upon deleting this customer*/ for (node_cut = 0, cur_edge = verts[k].first; cur_edge; cur_edge = cur_edge->next_edge){ node_cut += (cur_edge->other_end ? -cur_edge->data->weight : cur_edge->data->weight); } if (node_cut > max_node_cut){/*check whether the value of the cut decrease is the best seen so far*/ max_node = k; max_node_cut = node_cut; } } } } if (!max_node){ break; } /*delete the customer that exhibited the greatest decrease in cut value*/ compnodes[i+1]--; compdemands[i+1] -= verts[max_node].demand; compcuts[i+1] -= max_node_cut; cur_slack -= max_node_cut; verts[max_node].comp = 0; coef_list[i][max_node >> DELETE_POWER] ^= (1 << (max_node & DELETE_AND)); if (cur_slack < 0){/*if the cut is now violated, impose it*/ new_cut->coef = (char *) (coef_list[i]); new_cut->type = (compnodes[i+1] < vertnum/2 ? SUBTOUR_ELIM_SIDE:SUBTOUR_ELIM_ACROSS); new_cut->size = cut_size; new_cut->rhs = (new_cut->type == SUBTOUR_ELIM_SIDE ? RHS(compnodes[i+1], compdemands[i+1], capacity): 2*cur_bins); cg_send_cut(new_cut, num_cuts, alloc_cuts, cuts); break; } } } } FREE(coef_list[0]); FREE(coef_list); } which_connected_routine++; FREE(compnodes); FREE(compdemands); FREE(compcuts); }while((!(*num_cuts) && vrp->par.which_connected_routine == BOTH)
//############################################################################# void MibSHeuristic::greedyHeuristic() { MibSModel * model = MibSModel_; //OsiSolverInterface * oSolver = model->getSolver(); OsiSolverInterface * oSolver = model->solver(); double uObjSense(oSolver->getObjSense()); double lObjSense(model->getLowerObjSense()); int lCols(model->getLowerDim()); int uCols(model->getUpperDim()); int * uColIndices = model->getUpperColInd(); int * lColIndices = model->getLowerColInd(); double * lObjCoeffs = model->getLowerObjCoeffs(); double * intCost = model->getInterdictCost(); double intBudget = model->getInterdictBudget(); int tCols(uCols + lCols); assert(tCols == oSolver->getNumCols()); int i(0), ind_min_wt(0); double usedBudget(0.0); double * fixedVars = new double[lCols]; double * testsol = new double[tCols]; CoinZeroN(fixedVars, lCols); CoinZeroN(testsol, tCols); std::multimap<double, int> lObjCoeffsOrd; for(i = 0; i < lCols; i++) lObjCoeffsOrd.insert(std::pair<double, int>(lObjCoeffs[i] * lObjSense, i)); if(!bestSol_) bestSol_ = new double[tCols]; //initialize the best solution information //bestObjVal_ = model->getSolver()->getInfinity() * uObjSense; //CoinZeroN(bestSol_, tCols); std::multimap<double, int>::iterator iter; //std::multimap<double, int>::iterator first; //std::multimap<double, int>::iterator last; //int dist = std::distance(first, last); SRANDOM((unsigned) time(NULL)); int randchoice(0); if(0) std::cout << "randchoice " << randchoice << std::endl; double cost(0.0); //starting from the largest, fix corr upper-level variables //then, with these fixed, solve the lower-level problem //this yields a feasible solution iter = lObjCoeffsOrd.begin(); while((usedBudget < intBudget) && (iter != lObjCoeffsOrd.end())){ ind_min_wt = iter->second; cost = intCost[ind_min_wt]; testsol[uColIndices[ind_min_wt]] = 1.0; double min_wt = iter->first; if(0){ std::cout << "upper: " << ind_min_wt << " " << uColIndices[ind_min_wt] << " " << oSolver->getColUpper()[uColIndices[ind_min_wt]] << " " << oSolver->getColLower()[uColIndices[ind_min_wt]] << std::endl; std::cout << "lower: " << ind_min_wt << " " << lColIndices[ind_min_wt] << " " << oSolver->getColUpper()[lColIndices[ind_min_wt]] << std::endl; } //if((oSolver->getColUpper()[uColIndices[ind_min_wt]] == 1.0) //&& (oSolver->getColUpper()[lColIndices[ind_min_wt]] > 0)){ if(oSolver->getColUpper()[uColIndices[ind_min_wt]] > etol_){ //if(((usedBudget + cost) <= intBudget) // && checkLowerFeasibility(oSolver, testsol)){ if((usedBudget + cost) <= intBudget){ //FIXME: SHOULD BE CHECKING FOR CURRENT BOUNDS HERE //fix the corresponding upper-level variable to 1 randchoice = RANDOM() % 2; if(0) std::cout << "randchoice " << randchoice << std::endl; if(randchoice){ fixedVars[ind_min_wt] = 1.0; usedBudget += intCost[ind_min_wt]; } } } else{ testsol[uColIndices[ind_min_wt]] = 0; //break; } iter++; } /* now we find a feasible solution by fixing upper-level vars and solving the lower-level problem */ double * incumbentSol = new double[tCols]; double * colsol = new double[tCols]; CoinZeroN(colsol, tCols); for(i = 0; i < uCols; i++){ colsol[uColIndices[i]] = fixedVars[i]; if(fixedVars[i] == 1.0) if(0) std::cout << "fixed " << i << std::endl; } bfSol * sol = getBilevelSolution(colsol, lObjSense * oSolver->getInfinity()); if(sol){ double incumbentObjVal = sol->getObjVal(); CoinCopyN(sol->getColumnSol(), tCols, incumbentSol); MibSSolution * mibSol = new MibSSolution(tCols, incumbentSol, incumbentObjVal, model); model->storeSolution(BlisSolutionTypeHeuristic, mibSol); } //bestObjVal_ = incumbentObjVal; //CoinCopyN(incumbentSol, tCols, bestSol_); delete [] incumbentSol; delete [] testsol; //delete [] colsol; //delete [] fixedVars; //delete sol; }