gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern) { DdNode *h, *l; hisnode *Found; char *curnode; gradientpair lvalue, hvalue, tvalue; double this_probability; double *gradient; if (params.debug) { curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); fprintf(stderr, "%s\n", curnode); } if (Current == MyManager.t) { tvalue.probability = 1.0; tvalue.gradient = 0.0; return tvalue; } if (Current == MyManager.f) { tvalue.probability = 0.0; tvalue.gradient = 0.0; return tvalue; } if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) { tvalue.probability = Found->dvalue; tvalue.gradient = *((double *)Found->dynvalue); return tvalue; } l = LowNodeOf(MyManager.manager, Current); h = HighNodeOf(MyManager.manager, Current); if (params.debug) fprintf(stderr, "l(%s)->", curnode); lvalue = CalcGradient(MyManager, l, TargetVar, TargetPattern); if (params.debug) fprintf(stderr, "h(%s)->", curnode); hvalue = CalcGradient(MyManager, h, TargetVar, TargetPattern); this_probability = sigmoid( MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart], params.sigmoid_slope); tvalue.probability = this_probability * hvalue.probability + (1 - this_probability) * lvalue.probability; tvalue.gradient = this_probability * hvalue.gradient + (1 - this_probability) * lvalue.gradient; if ((GetIndex(Current) == TargetVar) || ((TargetPattern != NULL) && patternmatch(TargetPattern, MyManager.varmap.vars[GetIndex(Current)]))) { tvalue.gradient += hvalue.probability - lvalue.probability; } gradient = (double *)malloc(sizeof(double)); *gradient = tvalue.gradient; AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue.probability, 0, gradient); return tvalue; }
void ItemGradient(COMMAND_T* = NULL) { int iCurPos = 0; int iNumSel = 0; // First, must count the number of selected items for (int i = 1; i <= GetNumTracks(); i++) { MediaTrack* tr = CSurf_TrackFromID(i, false); for (int j = 0; j < GetTrackNumMediaItems(tr); j++) if (*(bool*)GetSetMediaItemInfo(GetTrackMediaItem(tr, j), "B_UISEL", NULL)) iNumSel++; } // Now, set the colors for (int i = 1; i <= GetNumTracks(); i++) { MediaTrack* tr = CSurf_TrackFromID(i, false); for (int j = 0; j < GetTrackNumMediaItems(tr); j++) { MediaItem* mi = GetTrackMediaItem(tr, j); if (*(bool*)GetSetMediaItemInfo(mi, "B_UISEL", NULL)) { COLORREF cr = CalcGradient(g_crGradStart, g_crGradEnd, (double)iCurPos++ / (iNumSel-1)) | 0x1000000; GetSetMediaItemInfo(mi, "I_CUSTOMCOLOR", &cr); } } } Undo_OnStateChange(__LOCALIZE("Set selected item(s) to color gradient","sws_undo")); UpdateTimeline(); }
/* void Surface :: GetNormalVector (const Point<3> & p, Vec<3> & n) const { CalcGradient (p, n); n.Normalize(); } */ Vec<3> Surface :: GetNormalVector (const Point<3> & p) const { Vec<3> n; CalcGradient (p, n); n.Normalize(); return n; }
void Surface :: CalcHesse (const Point<3> & point, Mat<3> & hesse) const { double dx = 1e-5; Point<3> hp1, hp2; Vec<3> g1, g2; for (int i = 0; i < 3; i++) { hp1 = point; hp2 = point; hp1(i) += dx; hp2(i) -= dx; CalcGradient (hp1, g1); CalcGradient (hp2, g2); for (int j = 0; j < 3; j++) hesse(i, j) = (g1(j) - g2(j)) / (2 * dx); } }
void Surface :: SkewProject (Point<3> & p, const Vec<3> & direction) const { Point<3> startp(p); double t_old(0),t_new(1); Vec<3> grad; for(int i=0; fabs(t_old-t_new) > 1e-20 && i<15; i++) { t_old = t_new; CalcGradient(p,grad); t_new = t_old - CalcFunctionValue(p)/(grad*direction); p = startp + t_new*direction; } }
void Surface :: Project (Point<3> & p) const { Vec<3> n; double val; for (int i = 1; i <= 10; i++) { val = CalcFunctionValue (p); if (fabs (val) < 1e-12) return; CalcGradient (p, n); p -= (val / Abs2 (n)) * n; } }
// Gradient stuffs void TrackGradient(COMMAND_T* = NULL) { int iNumSel = NumSelTracks(); int iCurPos = 0; if (iNumSel < 2) return; for (int i = 1; i <= GetNumTracks(); i++) { MediaTrack* tr = CSurf_TrackFromID(i, false); if (*(int*)GetSetMediaTrackInfo(tr, "I_SELECTED", NULL)) { COLORREF cr = CalcGradient(g_crGradStart, g_crGradEnd, (double)iCurPos++ / (iNumSel-1)) | 0x1000000; GetSetMediaTrackInfo(tr, "I_CUSTOMCOLOR", &cr); } } Undo_OnStateChangeEx(__LOCALIZE("Set tracks to color gradient","sws_undo"), UNDO_STATE_TRACKCFG, -1); }
void AbstractTask::UpdateGlideSolutions(const AircraftState &state, const GlidePolar &glide_polar) { GlideSolutionRemaining(state, glide_polar, stats.total.solution_remaining, stats.current_leg.solution_remaining); if (positive(glide_polar.GetMC())) { GlidePolar polar_mc0 = glide_polar; polar_mc0.SetMC(fixed_zero); GlideSolutionRemaining(state, polar_mc0, stats.total.solution_mc0, stats.current_leg.solution_mc0); } else { // no need to re-calculate, just copy stats.total.solution_mc0 = stats.total.solution_remaining; stats.current_leg.solution_mc0 = stats.current_leg.solution_remaining; } GlideSolutionTravelled(state, glide_polar, stats.total.solution_travelled, stats.current_leg.solution_travelled); GlideSolutionPlanned(state, glide_polar, stats.total.solution_planned, stats.current_leg.solution_planned, stats.total.remaining_effective, stats.current_leg.remaining_effective, stats.total.solution_remaining, stats.current_leg.solution_remaining); CalculatePirker(stats.total.pirker, stats.total.planned, stats.total.remaining_effective); CalculatePirker(stats.current_leg.pirker, stats.current_leg.planned, stats.current_leg.remaining_effective); Copy(stats.current_leg.remaining, stats.current_leg.solution_remaining); Copy(stats.current_leg.travelled, stats.current_leg.solution_travelled); Copy(stats.current_leg.planned, stats.current_leg.solution_planned); stats.total.gradient = ::AngleToGradient(CalcGradient(state)); stats.current_leg.gradient = ::AngleToGradient(CalcLegGradient(state)); }
void TakeGradient(COMMAND_T* = NULL) { int tCurPos = 0; for (int i = 1; i <= GetNumTracks(); i++) { MediaTrack* tr = CSurf_TrackFromID(i, false); for (int j = 0; j < GetTrackNumMediaItems(tr); j++) { MediaItem* mi = GetTrackMediaItem(tr, j); if (*(bool*)GetSetMediaItemInfo(mi, "B_UISEL", NULL)) { for (int h = 0; h < CountTakes(mi); h++) { COLORREF cr = CalcGradient(g_crGradStart, g_crGradEnd, (double)tCurPos++ / (CountTakes(mi)-1)) | 0x1000000; GetSetMediaItemTakeInfo(GetTake(mi, h), "I_CUSTOMCOLOR", &cr); } tCurPos = 0; } } } Undo_OnStateChange(__LOCALIZE("Set takes in selected item(s) to color gradient","sws_undo")); UpdateTimeline(); }
int main(int argc, char **arg) { clock_t start, endc, endt; double elapsedc, elapsedt; extmanager MyManager; DdNode *bdd; bddfileheader fileheader; int i, ivarcnt, code; gradientpair tvalue; double probability = -1.0; char *varpattern; varpattern = NULL; code = -1; params = loadparam(argc, arg); if (params.errorcnt > 0) { printhelp(argc, arg); for (i = 0; i < params.errorcnt; i++) { fprintf(stderr, "Error: not known or error at parameter %s.\n", arg[params.error[i]]); } return -1; } if (params.online == 0 && params.loadfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: you must specify a loading file.\n"); return -1; } if (params.method != 0 && arg[params.method][0] != 'g' && arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && arg[params.method][0] != 'l') { printhelp(argc, arg); fprintf(stderr, "Error: you must choose a calculation method beetween " "[p]robability, [g]radient, [l]ine search, [o]nline.\n"); return -1; } if (params.debug) DEBUGON; RAPIDLOADON; SETMAXBUFSIZE(params.maxbufsize); #ifndef _WIN32 signal(SIGINT, termhandler); if (params.ppid != NULL) { signal(SIGALRM, pidhandler); alarm(5); } else { signal(SIGALRM, handler); alarm(params.timeout); } #endif if (params.online) { MyManager.manager = simpleBDDinit(0); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedVars(1, 0); bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); ivarcnt = GetVarCount(MyManager.manager); } else { fileheader = ReadFileHeader(arg[params.loadfile]); switch (fileheader.filetype) { case BDDFILE_SCRIPT: if (params.inputfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: an input file is necessary for this type of " "loading file.\n"); return -1; } MyManager.manager = simpleBDDinit(fileheader.varcnt); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedMultiVars( fileheader.varcnt, fileheader.varstart, fileheader.bvarcnt); if (LoadMultiVariableData(MyManager.manager, MyManager.varmap, arg[params.inputfile]) == -1) return -1; start = clock(); bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); endc = clock(); elapsedc = ((double)(endc - start)) / CLOCKS_PER_SEC; printf("elapsed_construction(%lf).\n", elapsedc); ivarcnt = fileheader.varcnt; break; case BDDFILE_NODEDUMP: if (params.inputfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: an input file is necessary for this type of " "loading file.\n"); return -1; } MyManager.manager = simpleBDDinit(fileheader.varcnt); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); ivarcnt = fileheader.varcnt; break; default: fprintf(stderr, "Error: not a valid file format to load.\n"); return -1; break; } } #ifndef _WIN32 alarm(0); #endif // problem specifics if (bdd != NULL) { ivarcnt = RepairVarcnt(&MyManager.varmap); code = 0; /* if (params.inputfile != -1) { if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; if (!all_loaded(MyManager.varmap, 1)) return -1; }*/ MyManager.his = InitHistory(ivarcnt); if (params.method != 0) { switch (arg[params.method][0]) { case 'g': for (i = 0; i < MyManager.varmap.varcnt; i++) { if (MyManager.varmap.vars[i] != NULL) { varpattern = extractpattern(MyManager.varmap.vars[i]); if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern); probability = tvalue.probability; double factor = sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope) * (1 - sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope)) * params.sigmoid_slope; if (varpattern == NULL) { printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient * factor); } else { varpattern[strlen(varpattern) - 2] = '\0'; printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], varpattern, tvalue.gradient * factor); } ReInitHistory(MyManager.his, MyManager.varmap.varcnt); } if (varpattern != NULL) free(varpattern); } else { fprintf(stderr, "Error: no variable name given for parameter.\n"); } } if (probability < 0.0) { // no nodes, so we have to calculate probability ourself tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); probability = tvalue.probability; } printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); break; case 'l': tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); probability = tvalue.probability; printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); break; case 'p': printf("probability(%1.12f).\n", CalcProbability(MyManager, bdd)); break; case 'o': onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); break; default: myexpand(MyManager, bdd); break; } } else { start = clock(); // simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, //"bdd.dot"); printf("probability(%1.12f).\n", ret_prob(MyManager, bdd)); endt = clock(); elapsedt = ((double)(endt - start)) / CLOCKS_PER_SEC; printf("elapsed_traversing(%lf).\n", elapsedt); // myexpand(MyManager, bdd); } if (params.savedfile > -1) SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, arg[params.savedfile]); if (params.exportfile > -1) simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, arg[params.exportfile]); ReInitHistory(MyManager.his, MyManager.varmap.varcnt); free(MyManager.his); } if (MyManager.manager != NULL) { KillBDD(MyManager.manager); exit(code); free(MyManager.varmap.dvalue); free(MyManager.varmap.ivalue); free(MyManager.varmap.dynvalue); for (i = 0; i < MyManager.varmap.varcnt; i++) { free(MyManager.varmap.vars[i]); free(MyManager.varmap.mvars[i].probabilities); free(MyManager.varmap.mvars[i].booleanVars); } free(MyManager.varmap.vars); free(MyManager.varmap.mvars); free(MyManager.varmap.bVar2mVar); } if (params.error != NULL) free(params.error); return code; }