void IntrepidKernel<Scalar>::evaluate( Teuchos::ArrayRCP<Scalar> &function_values, const Teuchos::ArrayRCP<Scalar> &coeffs, const Teuchos::ArrayRCP<Scalar> &dfunc_values ) { int dim1 = this->b_cardinality; testPrecondition( dim1 == (int) coeffs.size(), "Function coefficients size does not match basis cardinality" ); MDArray function_coeffs( 1, dim1 ); for ( int m = 0; m < dim1; ++m ) { function_coeffs(0,m) = coeffs[m]; } MDArray basis_eval( 1, dim1, 1 ); for ( int i = 0; i < dim1; ++i ) { basis_eval( 0, i, 0 ) = dfunc_values[i]; } Teuchos::Tuple<int,2> function_dimensions; function_dimensions[0] = 1; function_dimensions[1] = 1; MDArray function_eval( function_dimensions, function_values ); Intrepid::FunctionSpaceTools::evaluate<Scalar>( function_eval, function_coeffs, basis_eval ); }
int function_f(const gsl_vector * params, void * void_data, gsl_vector * f) { struct data * d = (struct data*) void_data; double p[3]; size_t i; for (i=0; i<d->p; i++) p[i] = gsl_vector_get(params, i); for (i=0; i<d->n; i++) gsl_vector_set(f, i, (function_eval(d->X[i], p) - d->Y[i])/d->sigma[i]); return GSL_SUCCESS; }
static FunctionNode* TPAExpr_toFunctionNode(TPA_Expr* expr, Env* env) { if(expr==0 || expr->type==TPA_UNDEF) return 0; if(expr->type==TPA_VALUE) return ftree_newBool(expr->val==1); if(expr->type==TPA_VARIABLE) return ftree_newVar((char)expr->val); if(expr->type==TPA_OPERATOR) { return (expr->val=='!') ? ftree_newNot(TPAExpr_toFunctionNode(expr->left, env)) : ftree_newBin(TPAExpr_toFunctionNode(expr->left, env), expr->val, TPAExpr_toFunctionNode(expr->right, env)); } if(expr->type==TPA_CALL) { Function * f = interp_getFunctionByName(env, expr->call); if(f==0) { printf("Warning: no function was found for name: '%s'. Assuming false node.\n", expr->call); return ftree_newBool(0); } return ftree_newBool(function_eval(f, expr->point)); } return 0; }
extern void function_printAsKarnaugh(Function* f, FILE* out) { int rows, cols; char * rowsVars, * colsVars; Points * colsP, * rowsP; PointItem * rowPointItem, * colPointItem; rows = function_getVarsLength(f) / 2; cols = function_getVarsLength(f) - rows; rowsVars = calloc(rows+1, sizeof(char)); colsVars = calloc(cols+1, sizeof(char)); // Variable names memcpy(colsVars, f->vars, cols); colsVars[cols] = '\0'; memcpy(rowsVars, &(f->vars[cols]), rows); rowsVars[cols] = '\0'; colsP = points_grayCode(cols); rowsP = points_grayCode(rows); // Column fprintf(out, "%-*s%s\n", cols+2, colsVars, str_strips(points_toString(colsP),"(),") ); // Rows fprintf(out, "%s\n", rowsVars); // Rows line by line rowPointItem = rowsP->point; do { // Row value fprintf(out, "%-*s", cols+2, str_strips(point_toString(rowPointItem->p),"(),") ); // Columns colPointItem = colsP->point; do { fprintf(out, "%-*d ", cols, function_eval(f, point_concat(colPointItem->p, rowPointItem->p) ) ); colPointItem = colPointItem->next; } while (colPointItem != 0); fprintf(out, "\n"); rowPointItem = rowPointItem->next; } while(rowPointItem != 0); }
nlopt_result cdirect_unscaled(int n, nlopt_func f, void *f_data, const double *lb, const double *ub, double *x, double *minf, nlopt_stopping *stop, double magic_eps, int which_alg) { params p; int i; double *rnew; nlopt_result ret = NLOPT_OUT_OF_MEMORY; p.magic_eps = magic_eps; p.which_diam = which_alg % 3; p.which_div = (which_alg / 3) % 3; p.which_opt = (which_alg / (3*3)) % 3; p.lb = lb; p.ub = ub; p.stop = stop; p.n = n; p.L = 2*n+3; p.f = f; p.f_data = f_data; p.xmin = x; p.minf = HUGE_VAL; p.work = 0; p.iwork = 0; p.hull = 0; p.age = 0; rb_tree_init(&p.rtree, cdirect_hyperrect_compare); p.work = (double *) malloc(sizeof(double) * (2*n)); if (!p.work) goto done; p.iwork = (int *) malloc(sizeof(int) * n); if (!p.iwork) goto done; p.hull_len = 128; /* start with a reasonable number */ p.hull = (double **) malloc(sizeof(double *) * p.hull_len); if (!p.hull) goto done; if (!(rnew = (double *) malloc(sizeof(double) * p.L))) goto done; for (i = 0; i < n; ++i) { rnew[3+i] = 0.5 * (lb[i] + ub[i]); rnew[3+n+i] = ub[i] - lb[i]; } rnew[0] = rect_diameter(n, rnew+3+n, &p); rnew[1] = function_eval(rnew+3, &p); rnew[2] = p.age++; if (!rb_tree_insert(&p.rtree, rnew)) { free(rnew); goto done; } ret = divide_rect(rnew, &p); if (ret != NLOPT_SUCCESS) goto done; while (1) { double minf0 = p.minf; ret = divide_good_rects(&p); if (ret != NLOPT_SUCCESS) goto done; if (p.minf < minf0 && nlopt_stop_f(p.stop, p.minf, minf0)) { ret = NLOPT_FTOL_REACHED; goto done; } } done: rb_tree_destroy_with_keys(&p.rtree); free(p.hull); free(p.iwork); free(p.work); *minf = p.minf; return ret; }
/** * battle has three rounds: 1. range combat, 2. areal and melee combat rounds * 3. steal resources * * this function calculates the inflicted damage in the first two rounds * and calls the propagation function approprietly. this is the right * place to modify the attack values and inflicted damages by chance * or every other method. */ Battle *calcBattleResult (Battle *battle, const struct Cave *cave, int get_resources) { double q; int i, j; int round; debug(DEBUG_BATTLE, "entering calc battle"); battle_copyBeforeToAfter(battle); // intialize survivors debug(DEBUG_BATTLE, "call recalc"); battle_recalc(battle); // calculate all battle - values battle_rememberBattleValues(battle); ///////////// first round: range battle /////////////////////////////////// // range_battle debug(DEBUG_BATTLE, "starting first battle round"); debug(DEBUG_BATTLE, "Attacker: Range %d Hitpoints %d, " "Defender: Range %d Hitpoints-Units %d Hitpoints-DS %d", battle->attackers_acc_range, battle->attackers_acc_hitpoints_defenseSystems + battle->attackers_acc_hitpoints_units, battle->defenders_acc_range, battle->defenders_acc_hitpoints_units, battle->defenders_acc_hitpoints_defenseSystems); battle_propagateRangeDamage(battle, FLAG_DEFENDER, battle->attackers_acc_range * HIT_PROBABILITY_RANGE); battle_propagateRangeDamage(battle, FLAG_ATTACKER, battle->defenders_acc_range * HIT_PROBABILITY_RANGE); battle_recalc(battle); // recalculate the survivors' values ///////////// second round: areal damage and melee combat ///////////////// debug(DEBUG_BATTLE, "starting second battle round"); // areal damage (only attackers can inflict such damage) for (round = 0; round < BATTLE_ROUNDS; ++round) { debug(DEBUG_BATTLE, "Attacker: Areal %d Hitpoints(DS) %d, " "Defender: Areal %d Hitpoints(DS) %d", battle->attackers_acc_areal, battle->attackers_acc_hitpoints_defenseSystems, battle->defenders_acc_areal, battle->defenders_acc_hitpoints_defenseSystems); battle_propagateArealDamage(battle, FLAG_DEFENDER, battle->attackers_acc_areal * HIT_PROBABILITY_AREAL); // calculate the "overpower" modifier. if (battle->attackers_acc_hitpoints_units + battle->attackers_acc_hitpoints_defenseSystems == 0) { q = 1000; } else if (battle->defenders_acc_hitpoints_units + battle->defenders_acc_hitpoints_defenseSystems == 0) { q = 0.001; } else { q = (double) (battle->defenders_acc_hitpoints_units + battle->defenders_acc_hitpoints_defenseSystems) / (double) (battle->attackers_acc_hitpoints_units + battle->attackers_acc_hitpoints_defenseSystems); } battle->overpower_factor = q; // melee damage debug(DEBUG_BATTLE, "Attacker: Melee %d Hitpoints %d, " "Defender: Melee %d Hitpoints %d, q:(Def/Att) %g", battle->attackers_acc_melee, battle->attackers_acc_hitpoints_defenseSystems + battle->attackers_acc_hitpoints_units, battle->defenders_acc_melee, battle->defenders_acc_hitpoints_defenseSystems + battle->defenders_acc_hitpoints_units, q); battle_propagateNormalDamage(battle, FLAG_DEFENDER, sqrt(1/q) * battle->attackers_acc_melee * HIT_PROBABILITY_MELEE); battle_propagateNormalDamage(battle, FLAG_ATTACKER, sqrt(q) * battle->defenders_acc_melee * HIT_PROBABILITY_MELEE); battle_recalc(battle); // recalculate the survivors' values } // iterate battle rounds if (!cave) return battle; /* TODO integrate test_calc better */ ///////////// cleanup: who has won? /////////////////////////////////////// debug(DEBUG_BATTLE, "Survivors: Attacker: Hitpoints %d, Defender: Hitpoints %d", battle->attackers_acc_hitpoints_defenseSystems + battle->attackers_acc_hitpoints_units, battle->defenders_acc_hitpoints_defenseSystems + battle->defenders_acc_hitpoints_units); if (battle->attackers_acc_hitpoints_units + battle->attackers_acc_hitpoints_defenseSystems == 0) { q = 10; } else { q = (double) (battle->defenders_acc_hitpoints_units + battle->defenders_acc_hitpoints_defenseSystems) / (double) (battle->attackers_acc_hitpoints_units + battle->attackers_acc_hitpoints_defenseSystems); } battle->winner = q < 1 ? FLAG_ATTACKER : FLAG_DEFENDER; ///////////// steal resources ///////////////////////////////////////////// // ACHTUNG: hier ist 1 angreifer EINGEBRANNT! // DEFENDER 0 is the cave owner's army EINGEBRANNT if (battle->winner == FLAG_ATTACKER) /* get_resources zeigt an ob man resis mitnehmen darf */ /* gewonnen, daher bekommt man AQ * 0.5 jeder Ressource des Verteidigers */ for (i = 0; i < MAX_RESOURCE; ++i) { int capacity = 0; const struct Resource *resource = (struct Resource *) resource_type[i]; int safe_storage = function_eval(resource->safeStorage, cave); int res_attacker = battle->attackers[0].resourcesBefore[i]; int res_defender = battle->defenders[0].resourcesBefore[i]; for (j = 0; j < MAX_UNIT; ++j) capacity += battle->attackers[0].units[j].amount_after * ((struct Unit *) unit_type[j])->encumbranceList[i]; if (!get_resources && res_defender > safe_storage) { int amount = (1-q) * 0.5 * res_defender; /* steal amount */ if (res_defender - amount < safe_storage) amount = res_defender - safe_storage; if (res_attacker + amount > capacity) amount = capacity - res_attacker; battle->attackers[0].resourcesAfter[i] += amount; battle->defenders[0].resourcesAfter[i] -= amount; } } else /* verloren, daher verliert man alle Ressourcen */ for (i = 0; i < MAX_RESOURCE; ++i) { battle->attackers[0].resourcesAfter[i] = 0; battle->defenders[0].resourcesAfter[i] += battle->attackers[0].resourcesBefore[i]; } return battle; }
extern void function_printEvalPoint(Function* f, Point p, FILE* out) { char * sfree; fprintf(out, "%s%s = %d\n", f->symbol, sfree = point_toString(p), function_eval(f,p) ); if (sfree != 0) free(sfree); }