void crf1dc_beta_score(crf1d_context_t* ctx) { int i, t; floatval_t *cur = NULL; floatval_t *row = ctx->row; const floatval_t *next = NULL, *state = NULL, *trans = NULL; const int T = ctx->num_items; const int L = ctx->num_labels; const floatval_t *scale = &ctx->scale_factor[T-1]; /* Compute the beta scores at (T-1, *). */ cur = BETA_SCORE(ctx, T-1); vecset(cur, *scale, L); --scale; /* Compute the beta scores at (t, *). */ for (t = T-2;0 <= t;--t) { cur = BETA_SCORE(ctx, t); next = BETA_SCORE(ctx, t+1); state = EXP_STATE_SCORE(ctx, t+1); veccopy(row, next, L); vecmul(row, state, L); /* Compute the beta score at (t, i). */ for (i = 0;i < L;++i) { trans = EXP_TRANS_SCORE(ctx, i); cur[i] = vecdot(trans, row, L); } vecscale(cur, *scale, L); --scale; } }
static TYPE *getvec(int npad, TYPE padval, int N, int incX) { TYPE *X, *x; int i, n; if (N <= 0) return(NULL); incX = Mabs(incX); n = 2*npad + 1+(N-1)*incX; X = malloc( ATL_sizeof*n ); assert(X); vecset(n, padval, X); #ifdef TCPLX npad *= 2; incX *= 2; #endif x = X + npad; for (i=0; i < N; i++, x += incX) { #ifdef TREAL *x = dumb_rand(); #else *x = dumb_rand(); x[1] = dumb_rand(); #endif } return(X); }
static float linetest(const vec3* pos, const vec3* dir, vec3* hitPos, vec3* normal) { vecset(normal, 0.f, 0.f, 1.f); float dot = -dir->z; if (dot>0.f) { float distance = pos->z / dot; vecaddscale(hitPos, pos, dir, distance); return distance; } else { vecset(hitPos, 0.f, 0.f, -1000.f); return 1000.f; } }
static void tsuroCardSetAllColours(tsuroCard* card, float r, float g, float b) { for (int i=0; i<8; i++) { vecset(&card->colour[i], r, g, b); } }
//--------------------------------------------------------------------- // generate the test problem for benchmark 6 // makea generates a sparse matrix with a // prescribed sparsity distribution // // parameter type usage // // input // // n i number of cols/rows of matrix // nz i nonzeros as declared array size // rcond r*8 condition number // shift r*8 main diagonal shift // // output // // a r*8 array for nonzeros // colidx i col indices // rowstr i row pointers // // workspace // // iv, arow, acol i // aelt r*8 //--------------------------------------------------------------------- static void makea(int n, int nz, double a[], int colidx[], int rowstr[], int firstrow, int lastrow, int firstcol, int lastcol, int arow[], int acol[][NONZER+1], double aelt[][NONZER+1], int iv[]) { int iouter, ivelt, nzv, nn1; int ivc[NONZER+1]; double vc[NONZER+1]; //--------------------------------------------------------------------- // nonzer is approximately (int(sqrt(nnza /n))); //--------------------------------------------------------------------- //--------------------------------------------------------------------- // nn1 is the smallest power of two not less than n //--------------------------------------------------------------------- nn1 = 1; do { nn1 = 2 * nn1; } while (nn1 < n); //--------------------------------------------------------------------- // Generate nonzero positions and save for the use in sparse. //--------------------------------------------------------------------- for (iouter = 0; iouter < n; iouter++) { nzv = NONZER; sprnvc(n, nzv, nn1, vc, ivc); vecset(n, vc, ivc, &nzv, iouter+1, 0.5); arow[iouter] = nzv; for (ivelt = 0; ivelt < nzv; ivelt++) { acol[iouter][ivelt] = ivc[ivelt] - 1; aelt[iouter][ivelt] = vc[ivelt]; } } //--------------------------------------------------------------------- // ... make the sparse matrix from list of elements with duplicates // (iv is used as workspace) //--------------------------------------------------------------------- sparse(a, colidx, rowstr, n, nz, NONZER, arow, acol, aelt, firstrow, lastrow, iv, RCOND, SHIFT); }
static void vehicleSubTick(Chassis* c, float dt) { if (g_step==0) return; if (g_step&1) g_step = 0; vec3* chassisPos = &c->pose.v[3].v3; vec3* x = &c->pose.v[0].v3; vec3* y = &c->pose.v[1].v3; vec3* z = &c->pose.v[2].v3; // This bit is done by the physics engine if(1) { vecaddscale(chassisPos, chassisPos, &c->vel, dt); mtx rot; matrixRotateByVelocity(&rot, &c->pose, &c->angVel, dt); matrixCopy33(&c->pose, &rot); } // Damp vecscale(&c->vel, &c->vel, expf(-dt*1.f)); vecscale(&c->angVel, &c->angVel, expf(-dt*1.f)); if (fabsf(c->angVel.x)<0.01f) c->angVel.x = 0.f; if (fabsf(c->angVel.y)<0.01f) c->angVel.y = 0.f; if (fabsf(c->angVel.z)<0.01f) c->angVel.z = 0.f; ClampedImpulse frictionImpulse[numWheels][2]; c->steer = g_steer; //g_steer *= expf(-dt*3.f); //g_speed *= expf(-dt*3.f); static float latf = 10.f; static float angSpeed = 0.f; if (g_handBrake>0.f) { g_handBrake *= expf(-4.f*dt); g_speed *= expf(-4.f*dt); if (g_handBrake < 0.1f) { g_handBrake = 0.f; } } // Prepare for (int i=0; i<numWheels; i++) { Suspension* s = c->suspension[i]; Wheel* w = s->wheel; // Calculate the world position and offset of the suspension point vec3mtx33mulvec3(&s->worldOffset, &c->pose, &s->offset); vec3mtx43mulvec3(&s->worldDefaultPos, &c->pose, &s->offset); w->pos = s->worldDefaultPos; vec3 pointVel = getPointVel(c, &s->worldOffset); vecadd(&w->vel, &w->vel, &pointVel); float maxFriction0 = 2.0f * dt * c->mass * gravity * (1.f/(float)numWheels); clampedImpulseInit(&frictionImpulse[i][0], maxFriction0); float latfriction = 10.f; float newAngSpeed = vecdot(z, &c->angVel); float changeAngSpeed = (newAngSpeed - angSpeed)/dt; angSpeed = newAngSpeed; printf("changeAngSpeed = %f\n", changeAngSpeed); float speed = fabsf(vecdot(y, &c->vel)); const float base = 0.5f; if (g_speed>=0 && i>=2) { latfriction = 1.f*expf(-5.f*g_handBrake) + base; // latfriction = 1.f*expf(-2.f*fabsf(speed*changeAngSpeed)) + base; // if (angSpeed*g_steer < -0.1f) // { // latfriction = base; // } //if (g_steer == 0.f) //{ // latf += (10.f - latf) * (1.f - exp(-0.1f*dt)); //} //else //{ // latf += (0.1f - latf) * (1.f - exp(-10.f*dt)); //} //latfriction = latf; } else { latfriction = 10.f; } float maxFriction1 = latfriction * dt * c->mass * gravity * (1.f/(float)numWheels); clampedImpulseInit(&frictionImpulse[i][1], maxFriction1); vecset(&s->hitNorm, 0.f, 0.f, 1.f); float steer = w->maxSteer*c->steer * (1.f + 0.3f*s->offset.x*c->steer); vecscale(&w->wheelAxis, x, cosf(steer)); vecsubscale(&w->wheelAxis, &w->wheelAxis, y, sinf(steer)); w->frictionDir[0]; veccross(&w->frictionDir[0], z, &w->wheelAxis); w->frictionDir[1] = w->wheelAxis; w->angSpeed = -40.f*g_speed; } //============= // VERBOSE //============= #define verbose false #define dump if (verbose) printf dump("==========================================\n"); dump("START ITERATION\n"); dump("==========================================\n"); float solverERP = numIterations>1 ? 0.1f : 1.f; float changeSolverERP = numIterations>1 ? (1.f - solverERP)/(0.01f+ (float)(numIterations-1)) : 0.f; for (int repeat=0; repeat<numIterations; repeat++) { dump(" == Start Iter == \n"); for (int i=0; i<numWheels; i++) { Suspension* s = c->suspension[i]; Wheel* w = s->wheel; const bool axisError = true; const bool friction = true; // Friction if (friction) { vec3 lateralVel; vecaddscale(&lateralVel, &w->vel, &s->hitNorm, -vecdot(&s->hitNorm, &w->vel)); vecaddscale(&lateralVel, &lateralVel, &w->frictionDir[0], +w->angSpeed * w->radius); { int dir = 0; float v = vecdot(&lateralVel, &w->frictionDir[dir]); float denom = 1.f/w->mass + w->radius*w->radius*w->invInertia; float impulse = clampedImpulseApply(&frictionImpulse[i][dir], - solverERP * v / denom); vec3 impulseV; vecscale(&impulseV, &w->frictionDir[dir], impulse); vecaddscale(&w->vel, &w->vel, &impulseV, 1.f/w->mass); w->angSpeed = w->angSpeed + (impulse * w->radius * w->invInertia); } if (1) { int dir=1; float v = vecdot(&lateralVel, &w->frictionDir[dir]); float denom = 1.f/w->mass; float impulse = clampedImpulseApply(&frictionImpulse[i][dir], - solverERP * v / denom); vec3 impulseV; vecscale(&impulseV, &w->frictionDir[dir], impulse); vecaddscale(&w->vel, &w->vel, &impulseV, 1.f/w->mass); } //dump("gound collision errorV = %f, vel of wheel after = %f\n", penetration, vecdot(&w->vel, &s->hitNorm)); } if (axisError) // Axis Error { vec3 offset; vecsub(&offset, &w->pos, chassisPos); vec3 pointvel = getPointVel(c, &offset); vec3 error; vecsub(&error, &pointvel, &w->vel); vecaddscale(&error, &error, z, -vecdot(&error, z)); vec3 norm; if (vecsizesq(&error)>0.001f) { dump("axis error %f\n", vecsize(&error)); vecnormalise(&norm, &error); float denom = computeDenominator(1.f/c->mass, 1.f/c->inertia, &offset, &norm) + 1.f/w->mass; vecscale(&error, &error, -solverERP/denom); addImpulseAtOffset(&c->vel, &c->angVel, 1.f/c->mass, 1.f/c->inertia, &offset, &error); vecaddscale(&w->vel, &w->vel, &error, -solverERP/w->mass); } //dump("axis error vel of wheel after = %f, inline = %f\n", vecdot(&w->vel, &s->hitNorm), vecdot(&w->vel, &s->axis)); } } solverERP += changeSolverERP; } for (int i=0; i<numWheels; i++) { Suspension* s = c->suspension[i]; Wheel* w = s->wheel; vec3 pointVel = getPointVel(c, s); // Convert suspension wheel speed back to car space vecsub(&w->vel, &w->vel, &pointVel); } }
static floatval_t l2sgd_calibration( encoder_t *gm, dataset_t *ds, floatval_t *w, logging_t *lg, const training_option_t* opt ) { int i, s; int dec = 0, ok, trials = 1; int num = opt->calibration_candidates; clock_t clk_begin = clock(); floatval_t loss = 0.; floatval_t init_loss = 0.; floatval_t best_loss = DBL_MAX; floatval_t eta = opt->calibration_eta; floatval_t best_eta = opt->calibration_eta; const int N = ds->num_instances; const int S = MIN(N, opt->calibration_samples); const int K = gm->num_features; const floatval_t init_eta = opt->calibration_eta; const floatval_t rate = opt->calibration_rate; const floatval_t lambda = opt->lambda; logging(lg, "Calibrating the learning rate (eta)\n"); logging(lg, "calibration.eta: %f\n", eta); logging(lg, "calibration.rate: %f\n", rate); logging(lg, "calibration.samples: %d\n", S); logging(lg, "calibration.candidates: %d\n", num); logging(lg, "calibration.max_trials: %d\n", opt->calibration_max_trials); /* Initialize a permutation that shuffles the instances. */ dataset_shuffle(ds); /* Initialize feature weights as zero. */ vecset(w, 0, K); /* Compute the initial loss. */ gm->set_weights(gm, w, 1.); init_loss = 0; for (i = 0;i < S;++i) { floatval_t score; const crfsuite_instance_t *inst = dataset_get(ds, i); gm->set_instance(gm, inst); gm->score(gm, inst->labels, &score); init_loss -= score; gm->partition_factor(gm, &score); init_loss += score; } init_loss += 0.5 * lambda * vecdot(w, w, K) * N; logging(lg, "Initial loss: %f\n", init_loss); while (num > 0 || !dec) { logging(lg, "Trial #%d (eta = %f): ", trials, eta); /* Perform SGD for one epoch. */ l2sgd( gm, ds, NULL, w, lg, S, 1.0 / (lambda * eta), lambda, 1, 1, 1, 0., &loss); /* Make sure that the learning rate decreases the log-likelihood. */ ok = isfinite(loss) && (loss < init_loss); if (ok) { logging(lg, "%f\n", loss); --num; } else { logging(lg, "%f (worse)\n", loss); } if (isfinite(loss) && loss < best_loss) { best_loss = loss; best_eta = eta; } if (!dec) { if (ok && 0 < num) { eta *= rate; } else { dec = 1; num = opt->calibration_candidates; eta = init_eta / rate; } } else { eta /= rate; } ++trials; if (opt->calibration_max_trials <= trials) { break; } } eta = best_eta; logging(lg, "Best learning rate (eta): %f\n", eta); logging(lg, "Seconds required: %.3f\n", (clock() - clk_begin) / (double)CLOCKS_PER_SEC); logging(lg, "\n"); return 1.0 / (lambda * eta); }
static int l2sgd( encoder_t *gm, dataset_t *trainset, dataset_t *testset, floatval_t *w, logging_t *lg, const int N, const floatval_t t0, const floatval_t lambda, const int num_epochs, int calibration, int period, const floatval_t epsilon, floatval_t *ptr_loss ) { int i, epoch, ret = 0; floatval_t t = 0; floatval_t loss = 0, sum_loss = 0; floatval_t best_sum_loss = DBL_MAX; floatval_t eta, gain, decay = 1.; floatval_t improvement = 0.; floatval_t norm2 = 0.; floatval_t *pf = NULL; floatval_t *best_w = NULL; clock_t clk_prev, clk_begin = clock(); const int K = gm->num_features; if (!calibration) { pf = (floatval_t*)malloc(sizeof(floatval_t) * period); best_w = (floatval_t*)calloc(K, sizeof(floatval_t)); if (pf == NULL || best_w == NULL) { ret = CRFSUITEERR_OUTOFMEMORY; goto error_exit; } } /* Initialize the feature weights. */ vecset(w, 0, K); /* Loop for epochs. */ for (epoch = 1;epoch <= num_epochs;++epoch) { clk_prev = clock(); if (!calibration) { logging(lg, "***** Epoch #%d *****\n", epoch); /* Shuffle the training instances. */ dataset_shuffle(trainset); } /* Loop for instances. */ sum_loss = 0.; for (i = 0;i < N;++i) { const crfsuite_instance_t *inst = dataset_get(trainset, i); /* Update various factors. */ eta = 1 / (lambda * (t0 + t)); decay *= (1.0 - eta * lambda); gain = eta / decay; /* Compute the loss and gradients for the instance. */ gm->set_weights(gm, w, decay); gm->set_instance(gm, inst); gm->objective_and_gradients(gm, &loss, w, gain); sum_loss += loss; ++t; } /* Terminate when the loss is abnormal (NaN, -Inf, +Inf). */ if (!isfinite(loss)) { logging(lg, "ERROR: overflow loss\n"); ret = CRFSUITEERR_OVERFLOW; sum_loss = loss; goto error_exit; } /* Scale the feature weights. */ vecscale(w, decay, K); decay = 1.; /* Include the L2 norm of feature weights to the objective. */ /* The factor N is necessary because lambda = 2 * C / N. */ norm2 = vecdot(w, w, K); sum_loss += 0.5 * lambda * norm2 * N; /* One epoch finished. */ if (!calibration) { /* Check if the current epoch is the best. */ if (sum_loss < best_sum_loss) { /* Store the feature weights to best_w. */ best_sum_loss = sum_loss; veccopy(best_w, w, K); } /* We don't test the stopping criterion while period < epoch. */ if (period < epoch) { improvement = (pf[(epoch-1) % period] - sum_loss) / sum_loss; } else { improvement = epsilon; } /* Store the current value of the objective function. */ pf[(epoch-1) % period] = sum_loss; logging(lg, "Loss: %f\n", sum_loss); if (period < epoch) { logging(lg, "Improvement ratio: %f\n", improvement); } logging(lg, "Feature L2-norm: %f\n", sqrt(norm2)); logging(lg, "Learning rate (eta): %f\n", eta); logging(lg, "Total number of feature updates: %.0f\n", t); logging(lg, "Seconds required for this iteration: %.3f\n", (clock() - clk_prev) / (double)CLOCKS_PER_SEC); /* Holdout evaluation if necessary. */ if (testset != NULL) { holdout_evaluation(gm, testset, w, lg); } logging(lg, "\n"); /* Check for the stopping criterion. */ if (improvement < epsilon) { ret = 0; break; } } } /* Output the optimization result. */ if (!calibration) { if (ret == 0) { if (epoch < num_epochs) { logging(lg, "SGD terminated with the stopping criteria\n"); } else { logging(lg, "SGD terminated with the maximum number of iterations\n"); } } else { logging(lg, "SGD terminated with error code (%d)\n", ret); } } /* Restore the best weights. */ if (best_w != NULL) { sum_loss = best_sum_loss; veccopy(w, best_w, K); } error_exit: free(best_w); free(pf); if (ptr_loss != NULL) { *ptr_loss = sum_loss; } return ret; }
static void tsuroReset() { tsuroPlayer* player = &g_game.player; // Set player colour vecset(&player->colour, 1.f, 1.f, 0.f); switch(g_game.mode) { case TSURO_MODE_DEMO: { g_game.cardIndex = 0; player->current.x = 0; player->current.y = 3; player->current.path = 7; tsuroShuffleDeck(); // Place int n=0; for (int j=0; j<tsuroNumCells; j++) { for (int i=0; i<tsuroNumCells; i++) { if (n<tsuroNumBaseCards) { tsuroNode* node = &g_game.nodes[i][j]; node->used = 1; node->card = g_game.deck[n]; n++; } } } break; } case TSURO_MODE_SINGLE_PLAYER: { player->current.x = 0; player->current.y = 3; player->current.path = 7; g_game.cardIndex = 0; for (int i=0; i<3; i++) { tsuroDealCard(i); } for (int j=0; j<tsuroNumCells; j++) { for (int i=0; i<tsuroNumCells; i++) { g_game.nodes[i][j].used = 0; } } break; } } player->animFraction = 0.f; player->totalLength = 0.f; player->start = player->current; player->target = player->current; g_game.state = TSURO_STATE_PLAYING; tsuroSnapShot(&g_game.last); tsuroShuffleDeck(); tsuroMouseButton(MOUSEUP,1000,1000); g_use = 0; g_used = 0; g_using = 0; g_select = 1; g_dragging = 0; g_rotate = 0; g_select = -1; }
/*--------------------------------------------------------------------- c generate the test problem for benchmark 6 c makea generates a sparse matrix with a c prescribed sparsity distribution c c parameter type usage c c input c c n i number of cols/rows of matrix c nz i nonzeros as declared array size c rcond r*8 condition number c shift r*8 main diagonal shift c c output c c a r*8 array for nonzeros c colidx i col indices c rowstr i row pointers c c workspace c c iv, arow, acol i c v, aelt r*8 c---------------------------------------------------------------------*/ static void makea( int n, int nz, double a[], /* a[1:nz] */ int colidx[], /* colidx[1:nz] */ int rowstr[], /* rowstr[1:n+1] */ int nonzer, int firstrow, int lastrow, int firstcol, int lastcol, double rcond, int arow[], /* arow[1:nz] */ int acol[], /* acol[1:nz] */ double aelt[], /* aelt[1:nz] */ double v[], /* v[1:n+1] */ int iv[], /* iv[1:2*n+1] */ double shift ) { int i, nnza, iouter, ivelt, ivelt1, irow, nzv; /*-------------------------------------------------------------------- c nonzer is approximately (int(sqrt(nnza /n))); c-------------------------------------------------------------------*/ double size, ratio, scale; int jcol; size = 1.0; ratio = pow(rcond, (1.0 / (double)n)); nnza = 0; /*--------------------------------------------------------------------- c Initialize colidx(n+1 .. 2n) to zero. c Used by sprnvc to mark nonzero positions c---------------------------------------------------------------------*/ #pragma omp parallel for for (i = 1; i <= n; i++) { colidx[n+i] = 0; } for (iouter = 1; iouter <= n; iouter++) { nzv = nonzer; sprnvc(n, nzv, v, iv, &(colidx[0]), &(colidx[n])); vecset(n, v, iv, &nzv, iouter, 0.5); for (ivelt = 1; ivelt <= nzv; ivelt++) { jcol = iv[ivelt]; if (jcol >= firstcol && jcol <= lastcol) { scale = size * v[ivelt]; for (ivelt1 = 1; ivelt1 <= nzv; ivelt1++) { irow = iv[ivelt1]; if (irow >= firstrow && irow <= lastrow) { nnza = nnza + 1; if (nnza > nz) { printf("Space for matrix elements exceeded in" " makea\n"); printf("nnza, nzmax = %d, %d\n", nnza, nz); printf("iouter = %d\n", iouter); exit(1); } acol[nnza] = jcol; arow[nnza] = irow; aelt[nnza] = v[ivelt1] * scale; } } } } size = size * ratio; } /*--------------------------------------------------------------------- c ... add the identity * rcond to the generated matrix to bound c the smallest eigenvalue from below by rcond c---------------------------------------------------------------------*/ for (i = firstrow; i <= lastrow; i++) { if (i >= firstcol && i <= lastcol) { iouter = n + i; nnza = nnza + 1; if (nnza > nz) { printf("Space for matrix elements exceeded in makea\n"); printf("nnza, nzmax = %d, %d\n", nnza, nz); printf("iouter = %d\n", iouter); exit(1); } acol[nnza] = i; arow[nnza] = i; aelt[nnza] = rcond - shift; } } /*--------------------------------------------------------------------- c ... make the sparse matrix from list of elements with duplicates c (v and iv are used as workspace) c---------------------------------------------------------------------*/ sparse(a, colidx, rowstr, n, arow, acol, aelt, firstrow, lastrow, v, &(iv[0]), &(iv[n]), nnza); }