double prune(config* c, double alpha, double beta) { config next; int weight = INT_MAX, board = INT_MAX; double score; int i,j; if(c->stage == 0) { //Adding Stage for(i = 1; i <= 12; i++) { //Check if you have this weight if(config_self(c, i) == 1) { for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) == 0) { config_copy(c, &next); config_selfPlace(&next, i, j); if(!config_tip(&next)) { //This move didn't cause tipping; Extract the score from each of my min nodes; Choose the child with max score score = prunemin(&next, 1, alpha, beta); if(score > alpha) { alpha = score; weight = i; board = j; } //If the score was 1.0. Bingo found one.This is called "Solve Optimization over minimax" if(alpha == 1.0) { printf("%d,%d\n",weight ,board); return alpha; } } } } } } if(weight == INT_MAX && board == INT_MAX) { //No optimal answer found; i.e every move caused a tipping . I will loose just choose a placement alpha = -1.0; for(i = 1; i <= 12; i++) { if(config_self(c, 1) == 1) { for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) == 0) { weight = i; board = j; break; } } break; } } } } else if (c->stage == 1) { //Removing Stage for(j = -15; j <=15; j++) { if(config_board(c,j) > 0) { //There exist a weight at this position config_copy(c, &next); config_remove(&next, j); if(!config_tip(&next)) { //Not tipping ; Compute a score for your min childs score = prunemin(&next, 1, alpha, beta); if(score > alpha) { alpha = score; weight = config_board(c, j); board = j; } if(alpha == 1.0) { printf("%d,%d\n", weight, board); return alpha; } } } } if(weight == INT_MAX && board == INT_MAX) { //No optimal answer found; i.e every move caused a tipping . I will loose just choose a placement alpha = -1.0; for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) > 0) { weight = config_board(c,j); board = j; break; } } } } else { return 0.0; } printf("%d,%d\n",weight,board); return alpha; }
/* * config_update -- * Reconcile and update the command line configuration against the * config we found. */ int config_update(WT_SESSION *session, char **list) { int found; const char *cfg[] = { NULL, NULL, NULL }; char **configp, **listp; const char **rm; static const char *rmnames[] = { "filename", "id", "checkpoint", "checkpoint_lsn", "version", "source", NULL }; /* * If the object has been renamed, replace all of the column group, * index, file and table names with the new name. */ if (cmdname != NULL) { for (listp = list; *listp != NULL; listp += 2) if (WT_PREFIX_MATCH(*listp, "colgroup:") || WT_PREFIX_MATCH(*listp, "file:") || WT_PREFIX_MATCH(*listp, "index:") || WT_PREFIX_MATCH(*listp, "table:")) if (config_rename(listp, cmdname)) return (1); /* * If the object was renamed, and there are configuration pairs, * rename the configuration pairs as well, because we don't know * if the user used the old or new names for the pair's URI. */ for (configp = cmdconfig; cmdconfig != NULL && *configp != NULL; configp += 2) if (config_rename(configp, cmdname)) return (1); } /* * Remove all "filename=", "source=" and other configurations * that foil loading from the values. New filenames are chosen * as part of table load. */ for (listp = list; *listp != NULL; listp += 2) for (rm = rmnames; *rm != NULL; rm++) if (strstr(listp[1], *rm) != NULL) config_remove(listp[1], *rm); /* * It's possible to update everything except the key/value formats. * If there were command-line configuration pairs, walk the list of * command-line configuration strings, and check. */ for (configp = cmdconfig; cmdconfig != NULL && *configp != NULL; configp += 2) if (strstr(configp[1], "key_format=") || strstr(configp[1], "value_format=")) return (util_err(0, "the command line configuration string may not " "modify the object's key or value format")); /* * If there were command-line configuration pairs, walk the list of * command-line URIs and find a matching dump URI. For each match, * rewrite the dump configuration as described by the command-line * configuration. It is an error if a command-line URI doesn't find * a single, exact match, that's likely a mistake. */ for (configp = cmdconfig; cmdconfig != NULL && *configp != NULL; configp += 2) { found = 0; for (listp = list; *listp != NULL; listp += 2) { if (strncmp(*configp, listp[0], strlen(*configp)) != 0) continue; /* * !!! * We support JSON configuration strings, which leads to * configuration strings with brackets. Unfortunately, * that implies we can't simply append new configuration * strings to existing ones. We call an unpublished * WiredTiger API to do the concatenation: if anyone * else ever needs it we can make it public, but I think * that's unlikely. We're also playing fast and loose * with types, but it should work. */ cfg[0] = listp[1]; cfg[1] = configp[1]; if (__wt_config_concat( (WT_SESSION_IMPL *)session, cfg, &listp[1]) != 0) return (1); ++found; } switch (found) { case 0: return (util_err(0, "the command line object name %s was not matched " "by any loaded object name", *configp)); case 1: break; default: return (util_err(0, "the command line object name %s was not unique, " "matching more than a single loaded object name", *configp)); } } /* Leak the memory, I don't care. */ return (0); }
// Max and Min functions will call themselves recursively double prunemin(config* c, int depth, double alpha, double beta) { if(depth > DEPTH) { //Its time to approx at this level return approx(c,0); } config next; double score = 0.0; int i,j,p = 0; if(c->stage == 0) { //Adding Stage p = 0; for(i = 1; i <= 12 ; i = i+1) { if(config_self(c, i) == 1) { //Try this weight at all positions for(j = -15; j <= 15; j++) { if(config_board(c, j) == 0) { //There is no weight at this position config_copy(c, &next); config_selfPlace(&next, i , j); if(!config_tip(&next)) { p = 1; //Some Progress score = prunemax(c, depth+1, alpha, beta); //Get the scores from all your min childrens and take the max one if(score < beta) { //Better score beta = score; } if(alpha >= beta) { // During the entire game for every node alpha should be less than beta. Remember this prunemax will be called prunemin // and beta is best possible min value move that it could make. so if this alpha will be greater than that i will never // make this move. So Cut the search space and just return beta return alpha; } else if(beta == -1.0) { //Found the best possible score; Return it to your opponent; Remember the goal is assume that your opponent will // make the best possible move and if this move makes me the winner I won't search further because my opponent will know // this and thereby never make this move return beta; } } } } } } } else if (c->stage == 1) { // Removing Stage p = 0; for(j = -15; j <=15; j++) { if(config_board(c,j) > 0) { //There exist a weight at this position config_copy(c, &next); config_remove(&next, j); if(!config_tip(&next)) { //Not tipping ; Compute a score for your min childs score = prunemax(&next, depth + 1, alpha, beta); if(score < beta) { beta = score; } if(alpha >= beta) { return alpha; } else if(beta == -1.0) { return beta; } } } } } else { //Game Ended beta = 0.0; } if(p == 0) { //I can't make a move so I loose beta = 1.0; } return beta; }