Beispiel #1
0
int IPFP::runGEMA( ){
	
	//Apply GEMA extensions of Iterative Proportional Fitting Procedure
	int iter = 0;
	targets_modified = false;
	IPFP ipfp_copy(*this);
	double diff = computeBeliefs();
	std::vector<Message> desired_marginals, actual_marginals;
	std::vector<Message> gema_marginals(cfg->dv_spectrum_depths.size());
    computeMarginals( actual_marginals );
	setDesiredMarginals( desired_marginals, actual_marginals );
	while( !checkConvergence(false) && iter < MAX_IPFP_ITERATIONS ){
		iter++;

		//Re-initialise the GEMA target marginals	
		for( unsigned int idx = 0; idx < cfg->dv_spectrum_depths.size(); idx++ ) 
			gema_marginals[idx].reset(actual_marginals[idx].size());
		
		//Update the GEMA target marginals		
		for( unsigned int idx = 0; idx < cfg->dv_spectrum_depths.size(); idx++ ){
			ipfp_copy = *this;	//Create a copy of the IPFP instance so we don't change this one
			std::vector<Message> tmp_marginals( actual_marginals );
			ipfp_copy.applyIPFPstep( tmp_marginals, desired_marginals, idx );
			for( unsigned int j = 0; j < cfg->dv_spectrum_depths.size(); j++ ) 
				gema_marginals[j].addWeightedMessage(tmp_marginals[j], cfg->dv_spectrum_weights[idx]);
		}

		//Run standard IPFP for number of spectra iterations
		for( unsigned int idx = 0; idx < cfg->dv_spectrum_depths.size(); idx++ )
			applyIPFPstep( actual_marginals, gema_marginals, idx );

	}
	return iter;
}
Beispiel #2
0
        void solve(Operator& opA, Vector& x, Vector& b, Comm& comm) const
        {
            Dune::InverseOperatorResult result;
            // Parallel version is deactivated until we figure out how to do it properly.
#if HAVE_MPI
            if (parallelInformation_.type() == typeid(ParallelISTLInformation))
            {
                const size_t size = opA.getmat().N();
                const ParallelISTLInformation& info =
                    boost::any_cast<const ParallelISTLInformation&>( parallelInformation_);

                // As we use a dune-istl with block size np the number of components
                // per parallel is only one.
                info.copyValuesTo(comm.indexSet(), comm.remoteIndices(),
                                  size, 1);
                // Construct operator, scalar product and vectors needed.
                constructPreconditionerAndSolve<Dune::SolverCategory::overlapping>(opA, x, b, comm, result);
            }
            else
#endif
            {
                OPM_THROW(std::logic_error,"this method if for parallel solve only");
            }

            checkConvergence( result );
        }
Beispiel #3
0
 void solve(Operator& opA, Vector& x, Vector& b ) const
 {
     Dune::InverseOperatorResult result;
     // Construct operator, scalar product and vectors needed.
     Dune::Amg::SequentialInformation info;
     constructPreconditionerAndSolve(opA, x, b, info, result);
     checkConvergence( result );
 }
Beispiel #4
0
void dd::ExpMax::maximization(const int &n_epoch, const int &n_sample_per_epoch, const double &stepsize,
const double &decay, const double reg_param, const double reg1_param, const bool is_quiet) {
//const double &decay, const double reg_param, const double reg1_param, const std::string meta_file, const bool is_quiet) {
    //this->gibbs->learn(n_epoch, n_sample_per_epoch, stepsize,decay, reg_param, reg1_param, meta_file, is_quiet);
    this->gibbs->learn(n_epoch, n_sample_per_epoch, stepsize,decay, reg_param, reg1_param, is_quiet);
    resetEvidence();
    iterationCount++;
    if (check_convergence)
        checkConvergence();
}
Beispiel #5
0
/// Ops to be performed after
/// an iteration
void IterativeSolverJacobi::postIterate()
{
  // now, our current guess is updated to be
  // what the iterating over rows above produced
  *xGuess = *xNext ;

  // Check if the solution
  // is converging or diverging
  checkConvergence() ;

  if( solutionState == IsDiverging )
    warning( "Solution actually diverging, iteration=%d, norm2=%f", iteration, norm2 ) ;
}
Beispiel #6
0
int IPFP::runIPFP( ){
	
	//Apply Iterative Proportional Fitting Procedure
	int iter = 0;
	targets_modified = false;
	double diff = computeBeliefs();
	std::vector<Message> desired_marginals, actual_marginals;
    computeMarginals( actual_marginals );
	setDesiredMarginals( desired_marginals, actual_marginals );
	while( !checkConvergence(false) && iter < MAX_IPFP_ITERATIONS ){
		iter++;
		for( unsigned int idx = 0; idx < cfg->dv_spectrum_depths.size(); idx++ )
			applyIPFPstep( actual_marginals, desired_marginals, idx );
	}
	return iter;
}
/* ************************************************************************* */
void NonlinearOptimizer::defaultOptimize() {

  const NonlinearOptimizerParams& params = this->_params();
  double currentError = this->error();

  // check if we're already close enough
  if(currentError <= params.errorTol) {
    if (params.verbosity >= NonlinearOptimizerParams::ERROR)
      cout << "Exiting, as error = " << currentError << " < " << params.errorTol << endl;
    return;
  }

  // Maybe show output
  if (params.verbosity >= NonlinearOptimizerParams::VALUES) this->values().print("Initial values");
  if (params.verbosity >= NonlinearOptimizerParams::ERROR) cout << "Initial error: " << currentError << endl;

  // Return if we already have too many iterations
  if(this->iterations() >= params.maxIterations)
    return;

  // Iterative loop
  do {
    // Do next iteration
    currentError = this->error();
    this->iterate();

    // Maybe show output
    if(params.verbosity >= NonlinearOptimizerParams::VALUES) this->values().print("newValues");
    if(params.verbosity >= NonlinearOptimizerParams::ERROR) cout << "newError: " << this->error() << endl;
  } while(this->iterations() < params.maxIterations &&
      !checkConvergence(params.relativeErrorTol, params.absoluteErrorTol,
            params.errorTol, currentError, this->error(), params.verbosity));

  // Printing if verbose
  if (params.verbosity >= NonlinearOptimizerParams::ERROR &&
      this->iterations() >= params.maxIterations)
    cout << "Terminating because reached maximum iterations" << endl;
}
Beispiel #8
0
// Coordinate descent for binomial models
SEXP cdfit_binomial(SEXP X_, SEXP y_, SEXP penalty_, SEXP lambda, SEXP eps_, SEXP max_iter_, SEXP gamma_, SEXP multiplier, SEXP alpha_, SEXP dfmax_, SEXP user_, SEXP warn_) {

  // Declarations
  int n = length(y_);
  int p = length(X_)/n;
  int L = length(lambda);
  SEXP res, beta0, beta, Dev, iter;
  PROTECT(beta0 = allocVector(REALSXP, L));
  double *b0 = REAL(beta0);
  for (int i=0; i<L; i++) b0[i] = 0;
  PROTECT(beta = allocVector(REALSXP, L*p));
  double *b = REAL(beta);
  for (int j=0; j<(L*p); j++) b[j] = 0;
  PROTECT(Dev = allocVector(REALSXP, L));
  PROTECT(iter = allocVector(INTSXP, L));
  for (int i=0; i<L; i++) INTEGER(iter)[i] = 0;
  double *a = Calloc(p, double);    // Beta from previous iteration
  for (int j=0; j<p; j++) a[j] = 0;
  double a0 = 0;                    // Beta0 from previous iteration
  double *X = REAL(X_);
  double *y = REAL(y_);
  const char *penalty = CHAR(STRING_ELT(penalty_, 0));
  double *lam = REAL(lambda);
  double eps = REAL(eps_)[0];
  int max_iter = INTEGER(max_iter_)[0];
  double gamma = REAL(gamma_)[0];
  double *m = REAL(multiplier);
  double alpha = REAL(alpha_)[0];
  int dfmax = INTEGER(dfmax_)[0];
  int user = INTEGER(user_)[0];
  int warn = INTEGER(warn_)[0];
  double *r = Calloc(n, double);
  double *w = Calloc(n, double);
  double *s = Calloc(n, double);
  double *z = Calloc(p, double);
  double *eta = Calloc(n, double);
  int *e1 = Calloc(p, int);
  for (int j=0; j<p; j++) e1[j] = 0;
  int *e2 = Calloc(p, int);
  for (int j=0; j<p; j++) e2[j] = 0;
  double xwr, xwx, pi, u, v, cutoff, l1, l2, shift, si;
  int converged, lstart;

  // Initialization
  double ybar = sum(y, n)/n;
  a0 = b0[0] = log(ybar/(1-ybar));
  double nullDev = 0;
  for (int i=0;i<n;i++) nullDev = nullDev - y[i]*log(ybar) - (1-y[i])*log(1-ybar);
  for (int i=0; i<n; i++) s[i] = y[i] - ybar;
  for (int i=0; i<n; i++) eta[i] = a0;
  for (int j=0; j<p; j++) z[j] = crossprod(X, s, n, j)/n;

  // If lam[0]=lam_max, skip lam[0] -- closed form sol'n available
  if (user) {
    lstart = 0;
  } else {
    lstart = 1;
    REAL(Dev)[0] = nullDev;
  }

  // Path
  for (int l=lstart; l<L; l++) {
    if (l != 0) {
      // Assign a, a0
      a0 = b0[l-1];
      for (int j=0; j<p; j++) a[j] = b[(l-1)*p+j];

      // Check dfmax
      int nv = 0;
      for (int j=0; j<p; j++) {
	if (a[j] != 0) nv++;
      }
      if (nv > dfmax) {
	for (int ll=l; ll<L; ll++) INTEGER(iter)[ll] = NA_INTEGER;
	res = cleanupB(s, w, a, r, e1, e2, z, eta, beta0, beta, Dev, iter);
	return(res);
      }

      // Determine eligible set
      if (strcmp(penalty, "lasso")==0) cutoff = 2*lam[l] - lam[l-1];
      if (strcmp(penalty, "MCP")==0) cutoff = lam[l] + gamma/(gamma-1)*(lam[l] - lam[l-1]);
      if (strcmp(penalty, "SCAD")==0) cutoff = lam[l] + gamma/(gamma-2)*(lam[l] - lam[l-1]);
      for (int j=0; j<p; j++) if (fabs(z[j]) > (cutoff * alpha * m[j])) e2[j] = 1;
    } else {

      // Determine eligible set
      double lmax = 0;
      for (int j=0; j<p; j++) if (fabs(z[j]) > lmax) lmax = fabs(z[j]);
      if (strcmp(penalty, "lasso")==0) cutoff = 2*lam[l] - lmax;
      if (strcmp(penalty, "MCP")==0) cutoff = lam[l] + gamma/(gamma-1)*(lam[l] - lmax);
      if (strcmp(penalty, "SCAD")==0) cutoff = lam[l] + gamma/(gamma-2)*(lam[l] - lmax);
      for (int j=0; j<p; j++) if (fabs(z[j]) > (cutoff * alpha * m[j])) e2[j] = 1;
    }

    while (INTEGER(iter)[l] < max_iter) {
      while (INTEGER(iter)[l] < max_iter) {
	while (INTEGER(iter)[l] < max_iter) {
	  INTEGER(iter)[l]++;
	  REAL(Dev)[l] = 0;
	  for (int i=0;i<n;i++) {
	    if (eta[i] > 10) {
	      pi = 1;
	      w[i] = .0001;
	    } else if (eta[i] < -10) {
	      pi = 0;
	      w[i] = .0001;
	    } else {
	      pi = exp(eta[i])/(1+exp(eta[i]));
	      w[i] = pi*(1-pi);
	    }
	    s[i] = y[i] - pi;
	    r[i] = s[i]/w[i];
	    if (y[i]==1) REAL(Dev)[l] = REAL(Dev)[l] - log(pi);
	    if (y[i]==0) REAL(Dev)[l] = REAL(Dev)[l] - log(1-pi);
	  }
	  if (REAL(Dev)[l]/nullDev < .01) {
	    if (warn) warning("Model saturated; exiting...");
	    for (int ll=l; ll<L; ll++) INTEGER(iter)[ll] = NA_INTEGER;
	    res = cleanupB(s, w, a, r, e1, e2, z, eta, beta0, beta, Dev, iter);
	    return(res);
	  }

	  // Intercept
	  xwr = crossprod(w, r, n, 0);
	  xwx = sum(w, n);
	  b0[l] = xwr/xwx + a0;
	  for (int i=0; i<n; i++) {
	    si = b0[l] - a0;
	    r[i] -= si;
	    eta[i] += si;
	  }

	  // Covariates
	  for (int j=0; j<p; j++) {
	    if (e1[j]) {

	      // Calculate u, v
	      xwr = wcrossprod(X, r, w, n, j);
	      xwx = wsqsum(X, w, n, j);
	      u = xwr/n + (xwx/n)*a[j];
	      v = xwx/n;

	      // Update b_j
	      l1 = lam[l] * m[j] * alpha;
	      l2 = lam[l] * m[j] * (1-alpha);
	      if (strcmp(penalty,"MCP")==0) b[l*p+j] = MCP(u, l1, l2, gamma, v);
	      if (strcmp(penalty,"SCAD")==0) b[l*p+j] = SCAD(u, l1, l2, gamma, v);
	      if (strcmp(penalty,"lasso")==0) b[l*p+j] = lasso(u, l1, l2, v);

	      // Update r
	      shift = b[l*p+j] - a[j];
	      if (shift !=0) {
		/* for (int i=0;i<n;i++) r[i] -= shift*X[j*n+i]; */
		/* for (int i=0;i<n;i++) eta[i] += shift*X[j*n+i]; */
		for (int i=0;i<n;i++) {
		  si = shift*X[j*n+i];
		  r[i] -= si;
		  eta[i] += si;
		}
	      }
	    }
	  }

	  // Check for convergence
	  converged = checkConvergence(b, a, eps, l, p);
	  a0 = b0[l];
	  for (int j=0; j<p; j++) a[j] = b[l*p+j];
	  if (converged) break;
	}

	// Scan for violations in strong set
	int violations = 0;
	for (int j=0; j<p; j++) {
	  if (e1[j]==0 & e2[j]==1) {
	    z[j] = crossprod(X, s, n, j)/n;
	    l1 = lam[l] * m[j] * alpha;
	    if (fabs(z[j]) > l1) {
	      e1[j] = e2[j] = 1;
	      violations++;
	    }
	  }
	}
	if (violations==0) break;
      }

      // Scan for violations in rest
      int violations = 0;
      for (int j=0; j<p; j++) {
	if (e2[j]==0) {
	  z[j] = crossprod(X, s, n, j)/n;
	  l1 = lam[l] * m[j] * alpha;
	  if (fabs(z[j]) > l1) {
	    e1[j] = e2[j] = 1;
	    violations++;
	  }
	}
      }
      if (violations==0) break;
    }
  }
  res = cleanupB(s, w, a, r, e1, e2, z, eta, beta0, beta, Dev, iter);
  return(res);
}
Beispiel #9
0
int nasolver<nr_type_t>::solve_nonlinear (void)
{
    qucs::exception * e;
    int convergence, run = 0, MaxIterations, error = 0;

    // fetch simulation properties
    MaxIterations = getPropertyInteger ("MaxIter");
    reltol = getPropertyDouble ("reltol");
    abstol = getPropertyDouble ("abstol");
    vntol = getPropertyDouble ("vntol");
    updateMatrix = 1;

    if (convHelper == CONV_GMinStepping)
    {
        // use the alternative non-linear solver solve_nonlinear_continuation_gMin
        // instead of the basic solver provided by this function
        iterations = 0;
        error = solve_nonlinear_continuation_gMin ();
        return error;
    }
    else if (convHelper == CONV_SourceStepping)
    {
        // use the alternative non-linear solver solve_nonlinear_continuation_Source
        // instead of the basic solver provided by this function
        iterations = 0;
        error = solve_nonlinear_continuation_Source ();
        return error;
    }

    // run solving loop until convergence is reached
    do
    {
        error = solve_once ();
        if (!error)
        {
            // convergence check
            convergence = (run > 0) ? checkConvergence () : 0;
            savePreviousIteration ();
            run++;
            // control fixpoint iterations
            if (fixpoint)
            {
                if (convergence && !updateMatrix)
                {
                    updateMatrix = 1;
                    convergence = 0;
                }
                else
                {
                    updateMatrix = 0;
                }
            }
        }
        else
        {
            break;
        }
    }
    while (!convergence &&
            run < MaxIterations * (1 + convHelper ? 1 : 0));

    if (run >= MaxIterations || error)
    {
        e = new qucs::exception (EXCEPTION_NO_CONVERGENCE);
        e->setText ("no convergence in %s analysis after %d iterations",
                    desc.c_str(), run);
        throw_exception (e);
        error++;
    }

    iterations = run;
    return error;
}
Beispiel #10
0
int nasolver<nr_type_t>::solve_nonlinear_continuation_Source (void)
{
    qucs::exception * e;
    int convergence, run = 0, MaxIterations, error = 0;
    nr_double_t sStep, sPrev;

    // fetch simulation properties
    MaxIterations = getPropertyInteger ("MaxIter") / 4 + 1;
    updateMatrix = 1;
    fixpoint = 0;

    // initialize the stepper
    sPrev = srcFactor = 0;
    sStep = 0.01;
    srcFactor += sStep;

    do
    {
        // run solving loop until convergence is reached
        run = 0;
        do
        {
            subnet->setSrcFactor (srcFactor);
            error = solve_once ();
            if (!error)
            {
                // convergence check
                convergence = (run > 0) ? checkConvergence () : 0;
                savePreviousIteration ();
                run++;
            }
            else break;
        }
        while (!convergence && run < MaxIterations);
        iterations += run;

        // not yet converged, so decreased the source-step
        if (run >= MaxIterations || error)
        {
            if (error)
                sStep *= 0.1;
            else
                sStep *= 0.5;
            restorePreviousIteration ();
            saveSolution ();
            // here the absolute minimum step checker
            if (sStep < std::numeric_limits<nr_double_t>::epsilon())
            {
                error = 1;
                e = new qucs::exception (EXCEPTION_NO_CONVERGENCE);
                e->setText ("no convergence in %s analysis after %d sourceStepping "
                            "iterations", desc.c_str(), iterations);
                throw_exception (e);
                break;
            }
            srcFactor = std::min (sPrev + sStep, 1.0);
        }
        // converged, increased the source-step
        else if (run < MaxIterations / 4)
        {
            sPrev = srcFactor;
            srcFactor = std::min (srcFactor + sStep, 1.0);
            sStep *= 1.5;
        }
        else
        {
            srcFactor = std::min (srcFactor + sStep, 1.0);
        }
    }
    // continue until no source factor is necessary
    while (sPrev < 1);

    subnet->setSrcFactor (1);
    return error;
}
Beispiel #11
0
int nasolver<nr_type_t>::solve_nonlinear_continuation_gMin (void)
{
    qucs::exception * e;
    int convergence, run = 0, MaxIterations, error = 0;
    nr_double_t gStep, gPrev;

    // fetch simulation properties
    MaxIterations = getPropertyInteger ("MaxIter") / 4 + 1;
    updateMatrix = 1;
    fixpoint = 0;

    // initialize the stepper
    gPrev = gMin = 0.01;
    gStep = gMin / 100;
    gMin -= gStep;

    do
    {
        // run solving loop until convergence is reached
        run = 0;
        do
        {
            error = solve_once ();
            if (!error)
            {
                // convergence check
                convergence = (run > 0) ? checkConvergence () : 0;
                savePreviousIteration ();
                run++;
            }
            else break;
        }
        while (!convergence && run < MaxIterations);
        iterations += run;

        // not yet converged, so decreased the gMin-step
        if (run >= MaxIterations || error)
        {
            gStep /= 2;
            // here the absolute minimum step checker
            if (gStep < std::numeric_limits<nr_double_t>::epsilon())
            {
                error = 1;
                e = new qucs::exception (EXCEPTION_NO_CONVERGENCE);
                e->setText ("no convergence in %s analysis after %d gMinStepping "
                            "iterations", desc.c_str(), iterations);
                throw_exception (e);
                break;
            }
            gMin = MAX (gPrev - gStep, 0);
        }
        // converged, increased the gMin-step
        else
        {
            gPrev = gMin;
            gMin = MAX (gMin - gStep, 0);
            gStep *= 2;
        }
    }
    // continue until no additional resistances is necessary
    while (gPrev > 0);

    return error;
}
Beispiel #12
0
SEXP gdfit_gaussian(SEXP X_, SEXP y_, SEXP penalty_, SEXP K1_, SEXP K0_, SEXP lambda, SEXP alpha_, SEXP eps_, SEXP max_iter_, SEXP gamma_, SEXP group_multiplier, SEXP dfmax_, SEXP gmax_, SEXP user_) {

  // Lengths/dimensions
  int n = length(y_);
  int L = length(lambda);
  int J = length(K1_) - 1;
  int p = length(X_)/n;

  // Pointers
  double *X = REAL(X_);
  double *y = REAL(y_);
  const char *penalty = CHAR(STRING_ELT(penalty_, 0));
  int *K1 = INTEGER(K1_);
  int K0 = INTEGER(K0_)[0];
  double *lam = REAL(lambda);
  double alpha = REAL(alpha_)[0];
  double eps = REAL(eps_)[0];
  int max_iter = INTEGER(max_iter_)[0];
  double gamma = REAL(gamma_)[0];
  double *m = REAL(group_multiplier);
  int dfmax = INTEGER(dfmax_)[0];
  int gmax = INTEGER(gmax_)[0];
  int user = INTEGER(user_)[0];

  // Outcome
  SEXP res, beta, iter, df, loss;
  PROTECT(beta = allocVector(REALSXP, L*p));
  for (int j=0; j<(L*p); j++) REAL(beta)[j] = 0;
  PROTECT(iter = allocVector(INTSXP, L));
  for (int i=0; i<L; i++) INTEGER(iter)[i] = 0;
  PROTECT(df = allocVector(REALSXP, L));
  for (int i=0; i<L; i++) REAL(df)[i] = 0;
  PROTECT(loss = allocVector(REALSXP, L));
  for (int i=0; i<L; i++) REAL(loss)[i] = 0;
  double *b = REAL(beta);

  // Intermediate quantities
  double *r = Calloc(n, double);
  for (int i=0; i<n; i++) r[i] = y[i];
  double *a = Calloc(p, double);
  for (int j=0; j<p; j++) a[j] = 0;
  int *e = Calloc(J, int);
  for (int g=0; g<J; g++) e[g] = 0;
  int converged, lstart, ng, nv, violations;
  double shift, l1, l2;

  // If lam[0]=lam_max, skip lam[0] -- closed form sol'n available
  if (user) {
    lstart = 0;
  } else {
    REAL(loss)[0] = gLoss(r,n);
    lstart = 1;
  }

  // Path
  for (int l=lstart; l<L; l++) {
    R_CheckUserInterrupt();
    if (l != 0) {
      for (int j=0; j<p; j++) a[j] = b[(l-1)*p+j];

      // Check dfmax, gmax
      ng = 0;
      nv = 0;
      for (int g=0; g<J; g++) {
	if (a[K1[g]] != 0) {
	  ng++;
	  nv = nv + (K1[g+1]-K1[g]);
	}
      }
      if (ng > gmax | nv > dfmax) {
	for (int ll=l; ll<L; ll++) INTEGER(iter)[ll] = NA_INTEGER;
	res = cleanupG(a, r, e, beta, iter, df, loss);
	return(res);
      }
    }

    while (INTEGER(iter)[l] < max_iter) {
      while (INTEGER(iter)[l] < max_iter) {
	converged = 0;
	INTEGER(iter)[l]++;
	REAL(df)[l] = 0;

	// Update unpenalized covariates
	for (int j=0; j<K0; j++) {
	  shift = crossprod(X, r, n, j)/n;
	  b[l*p+j] = shift + a[j];
	  for (int i=0; i<n; i++) r[i] -= shift * X[n*j+i];
	  REAL(df)[l] += 1;
	}

	// Update penalized groups
	for (int g=0; g<J; g++) {
	  l1 = lam[l] * m[g] * alpha;
	  l2 = lam[l] * m[g] * (1-alpha);
	  if (e[g]) gd_gaussian(b, X, r, g, K1, n, l, p, penalty, l1, l2, gamma, df, a);
	}

	// Check convergence
	if (checkConvergence(b, a, eps, l, p)) {
	  converged  = 1;
	  REAL(loss)[l] = gLoss(r,n);
	  break;
	}
	for (int j=0; j<p; j++) a[j] = b[l*p+j];
      }

      // Scan for violations
      violations = 0;
      for (int g=0; g<J; g++) {
	if (e[g]==0) {
	  l1 = lam[l] * m[g] * alpha;
	  l2 = lam[l] * m[g] * (1-alpha);
	  gd_gaussian(b, X, r, g, K1, n, l, p, penalty, l1, l2, gamma, df, a);
	  if (b[l*p+K1[g]] != 0) {
	    e[g] = 1;
	    violations++;
	  }
	}
      }

      if (violations==0) {
	REAL(loss)[l] = gLoss(r, n);
	break;
      }
      for (int j=0; j<p; j++) a[j] = b[l*p+j];
    }
  }
  res = cleanupG(a, r, e, beta, iter, df, loss);
  return(res);
}
Beispiel #13
0
/**
 * main function
 * divided to two brances for master & slave processors respectively
 * @param argc commandline argument count
 * @param argv array of commandline arguments
 * @return 0 if success
 */
int main(int argc, char* argv[])
{
    	int rank;
	int size;
    	int num_clusters;
    	int num_points;
	int dex;
	int job_size;
	int job_done=0;
	
	Point* centroids;
	Point* points;
	Point* received_points;
	int  * slave_clusters;
	int  * former_clusters;
	int  * latter_clusters;
    	
	MPI_Init(&argc, &argv);
	
	MPI_Status status;

    	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    	MPI_Comm_size(MPI_COMM_WORLD, &size);
	
	//creation of derived MPI structure
	MPI_Datatype MPI_POINT;
	MPI_Datatype type=MPI_DOUBLE;
	int blocklen=2;
	MPI_Aint disp=0;
	MPI_Type_create_struct(1,&blocklen,&disp,&type,&MPI_POINT);
	MPI_Type_commit(&MPI_POINT);

/******** MASTER PROCESSOR WORKS HERE******************************************************/ 
      
   	if(rank==MASTER)
  	{
		//inputting from file
		FILE *input;
    		input=fopen(argv[1],"r");
		readHeaders(input,&num_clusters,&num_points);
    		points=(Point*)malloc(sizeof(Point)*num_points);
		readPoints(input,points,num_points);
		fclose(input);

		//other needed memory locations
		former_clusters=(int*)malloc(sizeof(int)*num_points);
		latter_clusters=(int*)malloc(sizeof(int)*num_points);
		job_size=num_points/(size-1);
		centroids=malloc(sizeof(Point)*num_clusters);
		
		//reseting and initializing to default behaviour		
		initialize(centroids,num_clusters);
		resetData(former_clusters,num_points);
		resetData(latter_clusters,num_points);
		
		//Sending the essential data to slave processors
		for(dex=1;dex<size;dex++)
		{
			printf("Sending to [%d]\n",dex);
			MPI_Send(&job_size              ,1           , MPI_INT        ,dex,0,MPI_COMM_WORLD);
			MPI_Send(&num_clusters          ,1           , MPI_INT        ,dex,0,MPI_COMM_WORLD);
			MPI_Send(centroids              ,num_clusters, MPI_POINT      ,dex,0,MPI_COMM_WORLD);
			MPI_Send(points+(dex-1)*job_size,job_size    , MPI_POINT      ,dex,0,MPI_COMM_WORLD);
		}
    		printf("Sent!\n");

		MPI_Barrier(MPI_COMM_WORLD);

		//Main job of master processor is done here		
		while(1)
		{	
			MPI_Barrier(MPI_COMM_WORLD);
			
			printf("Master Receiving\n");
			for(dex=1;dex<size;dex++)
				MPI_Recv(latter_clusters+(job_size*(dex-1)),job_size,MPI_INT,dex,0,MPI_COMM_WORLD,&status);
			
			printf("Master Received\n");
			
			calculateNewCentroids(points,latter_clusters,centroids,num_clusters,num_points);
			printf("New Centroids are done!\n");
			if(checkConvergence(latter_clusters,former_clusters,num_points)==0)
			{
				printf("Converged!\n");
				job_done=1;
			}
			else    
			{
				printf("Not converged!\n");
				for(dex=0;dex<num_points;dex++)
					former_clusters[dex]=latter_clusters[dex];
			}
			
			//Informing slaves that no more job to be done
			for(dex=1;dex<size;dex++)
				MPI_Send(&job_done,1, MPI_INT,dex,0,MPI_COMM_WORLD);

			MPI_Barrier(MPI_COMM_WORLD);
			if(job_done==1)
				break;
	
			//Sending the recently created centroids			
			for(dex=1;dex<size;dex++)
				MPI_Send(centroids,num_clusters, MPI_POINT,dex,0, MPI_COMM_WORLD);

			MPI_Barrier(MPI_COMM_WORLD);
		}
		
		//Outputting to the output file		
		FILE* output=fopen(argv[2],"w");
		fprintf(output,"%d\n",num_clusters);
		fprintf(output,"%d\n",num_points);
		for(dex=0;dex<num_clusters;dex++)
			fprintf(output,"%lf,%lf\n",centroids[dex]._x,centroids[dex]._y);
		for(dex=0;dex<num_points;dex++)
			fprintf(output,"%lf,%lf,%d\n",points[dex]._x,points[dex]._y,latter_clusters[dex]+1);
		fclose(output);
	}
/*************END OF MASTER PROCESSOR'S BRANCH -- SLAVE PROCESSORS' JOB IS TO FOLLOW ************************/
	else
	{
		//Receiving the essential data
		printf("Receiving\n");
		MPI_Recv(&job_size    ,1           ,MPI_INT  ,MASTER,0,MPI_COMM_WORLD,&status);
		MPI_Recv(&num_clusters,1           ,MPI_INT  ,MASTER,0,MPI_COMM_WORLD,&status);
		centroids=malloc(sizeof(Point)*num_clusters);
		MPI_Recv(centroids    ,num_clusters,MPI_POINT,MASTER,0,MPI_COMM_WORLD,&status);
		printf("part_size =%d\n",job_size);
		received_points=(Point*)malloc(sizeof(Point)*job_size);
		slave_clusters=(int*)malloc(sizeof(int)*job_size);
		MPI_Recv(received_points,job_size,MPI_POINT      ,MASTER,0,MPI_COMM_WORLD,&status);
		printf("Received [%d]\n",rank);

		MPI_Barrier(MPI_COMM_WORLD);
		
		while(1)
		{
			printf("Calculation of new clusters [%d]\n",rank);
			for(dex=0;dex<job_size;dex++)
			{
				slave_clusters[dex]=whoIsYourDaddy(received_points[dex],centroids,num_clusters);
			}
			
			printf("sending to master [%d]\n",rank);
			MPI_Send(slave_clusters,job_size, MPI_INT,MASTER, 0, MPI_COMM_WORLD);
			MPI_Barrier(MPI_COMM_WORLD);
			MPI_Barrier(MPI_COMM_WORLD);
			MPI_Recv(&job_done,1, MPI_INT,MASTER,0,MPI_COMM_WORLD,&status);
					
			if(job_done==1) //No more work to be done
				break;
			
			//Receiving recently created centroids from master
			MPI_Recv(centroids,num_clusters,MPI_POINT,MASTER,0, MPI_COMM_WORLD,&status);

			MPI_Barrier(MPI_COMM_WORLD);
		}
	}
	//End of all	
	MPI_Finalize();
    	return 0;
}
Beispiel #14
0
int IPFP::runIPFPwithOscAdjust( ){
	
	//Apply Iterative Proportional Fitting Procedure - detecting and adjusting for oscillatory convergence
	int iter = 0;
	int reverse = 0;
	targets_modified = false;

	//Store a copy of the initial msgs
	std::vector<Message> init_down_msgs = down_msgs;
	std::vector<Message> init_up_msgs = up_msgs;
	
	//Initialise the target marginals
	std::vector<Message> desired_marginals, actual_marginals, reverse_marginals;	
	initialiseFactorProbsAndBeliefs();
	double diff = computeBeliefs();
	computeMarginals( actual_marginals );
	setDesiredMarginals( desired_marginals, actual_marginals );	

	int retries = 0;
	while( (retries == 0 || status == OSC_CONVERGE) && retries <= MAX_IPFP_OSC_RETRIES ){

		//Run IPFP to convergence (oscillatory or otherwise) in the forward direction
		iter = 0;
		while( !checkConvergence(true) && iter < MAX_IPFP_ITERATIONS ){
			iter++;
			//for( int idx = cfg->dv_spectrum_depths.size()-1; idx >= 0; idx-- ){
			for( unsigned int idx = 0; idx < cfg->dv_spectrum_depths.size(); idx++ )
				applyIPFPstep( actual_marginals, desired_marginals, idx );
		}
		if( status != OSC_CONVERGE ) break;

		//Oscillatory Convergence detected:
		// std::cout << "Oscillatory convergence - modifying targets for retry after " << iter << " iterations ("  << retries << " retry)" << std::endl;

		//Re-Initialise msgs and factor transition/persistence probabilities
		down_msgs = init_down_msgs;
		up_msgs = init_up_msgs;
		initialiseFactorProbsAndBeliefs();
		diff = computeBeliefs();
		computeMarginals( reverse_marginals );
		for( unsigned int i = 0; i < diff_buf.size(); i++){
			diff_buf[i] = 100.0;
			prev_diff_buf[i] = 1000.0;
		}

		//Run IPFP to convergence (oscillatory or otherwise) in the reverse direction
		iter = 0;
		while( !checkConvergence(true) && iter < MAX_IPFP_ITERATIONS ){
			iter++;
			for( int idx = cfg->dv_spectrum_depths.size()-1; idx >= 0; idx-- )
				applyIPFPstep( reverse_marginals, desired_marginals, idx );
		}
		
		//Set the target marginals to the average of the forward and reverse marginals
		desired_marginals = actual_marginals;
		for( unsigned int j = 0; j < cfg->dv_spectrum_depths.size(); j++ ) 
			desired_marginals[j].addWeightedMessage(reverse_marginals[j], 1.0);	
		targets_modified = true;
		
		//Re-Initialise everything ready to go again
		down_msgs = init_down_msgs;
		up_msgs = init_up_msgs;
		initialiseFactorProbsAndBeliefs();
		diff = computeBeliefs();
		computeMarginals( actual_marginals );
		for( unsigned int i = 0; i < diff_buf.size(); i++){
			diff_buf[i] = 100.0;
			prev_diff_buf[i] = 1000.0;
		}

		retries++;

	}
	return iter;
}
Beispiel #15
0
static void cdfit(double *x, double *y, double *omega, double *init, double lambda,
                  double kappa, double theta, char *penalty, double eps,
                  int max, int inmax, int n, int p, int *iter)
{
  double *beta_old, *beta, *w, *ww, *re;
  double  z, r = 0, s = 0;
  int i, j, itetime = 0, flag = 1, itetime2 = 0, flag2 = 1;
  beta = vector(p);
  beta_old = vector(p);
  w = vector(p); /* p dimensional vector for X'WX. */
  re = vector(n);
  ww = vector(n); /* n dimensional weight vector for diag(W). */
  for (i = 0; i < p; i++)
  {
    beta_old[i] = init[i];
    beta[i] = init[i];
  }
  for (i = 0; i < n; i++)
  {
    s = 0;
    for (j = 0; j < p; j++)
      s += x[i + j * n] * beta[j];
    re[i] = y[i] - s; /* re is the r vector. */
  }

  /* This is the MM layer. */
  while (flag && itetime <= max)
  {
    if (itetime > 0) for (i = 0; i < p; i++) beta_old[i] = beta[i];
    fww(beta, theta, x, y, omega, n, p, ww);
    for (j = 0; j < p; j++)
    {
      s = 0;
      for (i = 0; i < n; i++)
        s += x[i + j * n] * ww[i] * x[i + j * n];
      w[j] = s;
    }
    /* This is one coordinate descent layer. */
    itetime2 = 0;
    flag2 = 1;
    while (flag2 && itetime2 < inmax)
    {
      for (j = 0; j < p; j++)
      {
        r = 0;
        for (i = 0; i < n; i++) r += x[i + j * n] * ww[i] * re[i];
        if (strcmp(penalty, "MCP") == 0) z = fmcp(lambda, kappa, beta[j], w[j], r);
        if (strcmp(penalty, "LASSO") == 0) z = flasso(lambda, beta[j], w[j], r);
        if (fabs(beta[j] - z) > eps) {
          for (i = 0; i < n; i++) re[i] += x[i + j * n] * (beta[j] - z);}
        beta[j] = z;
      }
      itetime2++;
      flag2 = !checkConvergence(beta, beta_old, eps, p);
    }
    itetime++;
    flag = !checkConvergence(beta, beta_old, eps, p);
  }
  for (i = 0; i < p; i++) init[i] = beta[i];
  iter[0] = itetime;
  free_vector(beta);
  free_vector(beta_old);
  free_vector(w);
  free_vector(ww);
  free_vector(re);
}
bool NOX::LineSearch::Polynomial::compute(Abstract::Group& newGrp,
                      double& step,
                      const Abstract::Vector& dir,
                      const Solver::Generic& s)
{
  printOpeningRemarks();

  int nNonlinearIters = s.getNumIterations();

  if (useCounter)
    counter.incrementNumLineSearches();

  // Get the linear solve tolerance if doing ared/pred for conv criteria
  std::string direction = const_cast<Teuchos::ParameterList&>(s.getList()).
    sublist("Direction").get("Method", "Newton");
  double eta = (suffDecrCond == AredPred) ?
    const_cast<Teuchos::ParameterList&>(s.getList()).
    sublist("Direction").sublist(direction).sublist("Linear Solver").
    get("Tolerance", -1.0) : 0.0;

  // Computations with old group
  const Abstract::Group& oldGrp = s.getPreviousSolutionGroup();
  double oldPhi = meritFuncPtr->computef(oldGrp);    // \phi(0)
  double oldValue = computeValue(oldGrp, oldPhi);
  double oldSlope = meritFuncPtr->computeSlope(dir, oldGrp);

  // Computations with new group
  step = defaultStep;
  updateGrp(newGrp, oldGrp, dir, step);
  double newPhi = meritFuncPtr->computef(newGrp);
  double newValue = computeValue(newGrp, newPhi);

  bool isConverged = false;
  bool isFailed = false;
  int nIters = 1;

  if (oldSlope >= 0.0)
  {
    printBadSlopeWarning(oldSlope);
    isFailed = true;
  }
  else
    isConverged = checkConvergence(newValue, oldValue, oldSlope, step,
                   eta, nIters, nNonlinearIters);

  // Increment the number of newton steps requiring a line search
  if ((useCounter) && (!isConverged))
    counter.incrementNumNonTrivialLineSearches();

  double prevPhi = 0.0;        // \phi(\lambda_{k-1})
  double prevPrevPhi = 0.0;    // \phi(\lambda_{k-2})
  double prevStep = 0.0;    // \lambda_{k-1}
  double prevPrevStep = 0.0;    // \lambda_{k-2}

  while ((!isConverged) && (!isFailed))
  {
    print.printStep(nIters, step, oldValue, newValue,
            "", (suffDecrCond != AredPred));

    if (nIters > maxIters)
    {
      isFailed = true;
      break;
    }

    if (interpolationType == Quadratic3)
    {
      /* 3-Point Quadratic Interpolation */

      prevPrevPhi = prevPhi;
      prevPhi = newPhi;
      prevPrevStep = prevStep;
      prevStep = step;

      if (nIters == 1)
      {
    step = 0.5 * step;
      }
      else
      {
    double c1 = prevStep * prevStep * (prevPrevPhi - oldPhi) -
      prevPrevStep * prevPrevStep * (prevPhi - oldPhi);
    double c2 = prevPrevStep * (prevPhi - oldPhi) -
      prevStep * (prevPrevPhi - oldPhi);

    if (c1 < 0)
      step = -0.5 * c1 / c2;
      }
    }

    else if ((nIters == 1) || (interpolationType == Quadratic))
    {
      /* Quadratic Interpolation */

      prevPhi = newPhi;
      prevStep = step;

      step = - (oldSlope * prevStep * prevStep) /
    (2.0 * (prevPhi - oldPhi - prevStep * oldSlope)) ;

    }

    else
    {
      /*   Cubic Interpolation */

      prevPrevPhi = prevPhi;
      prevPhi = newPhi;
      prevPrevStep = prevStep;
      prevStep = step;

      double term1 = prevPhi - oldPhi - prevStep * oldSlope ;
      double term2 = prevPrevPhi - oldPhi - prevPrevStep * oldSlope ;

      double a = 1.0 / (prevStep - prevPrevStep) *
    (term1 / (prevStep * prevStep) - term2 /
     (prevPrevStep * prevPrevStep)) ;

      double b = 1.0 / (prevStep - prevPrevStep) *
    (-1.0 * term1 * prevPrevStep / (prevStep * prevStep) +
     term2 * prevStep / (prevPrevStep * prevPrevStep)) ;

      double disc = b * b - 3.0 * a * oldSlope;

      if (disc < 0)
      {
    isFailed = true;
    break;
      }

      if (b > 0.0) // Check to prevent round off error (H. Walker)
      {
    step = -oldSlope / (b + sqrt(disc));
      }
      else
      {
    if (fabs(a) < 1.e-12) // check for when a is small
    {
      step = -oldSlope / (2.0 * b);
    }
    else
    {
      step = (-b + sqrt(disc))/ (3.0 * a);
    }
      }
    }

    // Apply bounds
    if (step < minBoundFactor * prevStep)
      step = minBoundFactor * prevStep;
    else if (step > maxBoundFactor * prevStep)
      step = maxBoundFactor * prevStep;

    // Check that step isn't too small
    if (step < minStep)
    {
      isFailed = true;
      break;
    }

    // Update the new group and compute new measures
    updateGrp(newGrp, oldGrp, dir, step);
    newPhi = meritFuncPtr->computef(newGrp);
    newValue = computeValue(newGrp, newPhi);

    nIters ++;

    if (useCounter)
      counter.incrementNumIterations();

    isConverged = checkConvergence(newValue, oldValue, oldSlope, step,
                   eta, nIters, nNonlinearIters);

  } // End while loop


  if (isFailed)
  {
    if (useCounter)
      counter.incrementNumFailedLineSearches();

    if (recoveryStepType == Constant)
      step = recoveryStep;

    if (step == 0.0)
    {
      newGrp = oldGrp;
      newPhi = oldPhi;
      newValue = oldValue;
    }
    else
    {
      updateGrp(newGrp, oldGrp, dir, step);
      newPhi = meritFuncPtr->computef(newGrp);
      newValue = computeValue(newGrp, newPhi);
    }
  }

  std::string message = (isFailed) ? "(USING RECOVERY STEP!)" : "(STEP ACCEPTED!)";
  print.printStep(nIters, step, oldValue, newValue, message, (suffDecrCond != AredPred));

  paramsPtr->set("Adjusted Tolerance", 1.0 - step * (1.0 - eta));

  if (useCounter)
    counter.setValues(*paramsPtr);

  return (!isFailed);
}
    Mahalanobis InfTheoMetricLearner::learnMetric() {

        computeConstraints();
        int dim = getVectorDim();

        Mahalanobis metric = getMetric();
        metric.setM(M0);

        int simConCount = simConstraints.getConstraintCount();
        int disSimConCount = disSimConstraints.getConstraintCount();

        vec x1;
        vec x2;
        double slack = 0.0;
        double lambda = 0.0;

        InfTheoConstraints* currentConstSet = NULL;
        int currentIdx = 0;
        int currentSimIdx = 0;
        int currentDisSimIdx = 0;

        double p = 0.0;
        double delta = 0.0;
        double alpha = 0.0;
        double beta = 0.0;

        int i = 0;

        for(i = 0; !checkConvergence(metric.getM()) && (simConCount || disSimConCount); ++i) {

            // choose sim constraint
            if(i % 2 && simConCount) {

                currentConstSet = &simConstraints;
                currentIdx = currentSimIdx = (currentSimIdx + 1) % simConCount;

                // line 3.3
                delta = 1;

            }
            // choose dissim constraint
            else if(!(i %2) && disSimConCount) {
                currentConstSet = &disSimConstraints;
                currentIdx = currentDisSimIdx = (currentDisSimIdx + 1) % disSimConCount;

                // line 3.3
                delta = -1;

            }

            // pick some constraint (line 3.1)
            x1 = currentConstSet->getX1(currentIdx);
            x2 = currentConstSet->getX2(currentIdx);
            slack = currentConstSet->getSlack(currentIdx);
            lambda = currentConstSet->getLambda(currentIdx);

            // line 3.2
            p = metric.computeSquaredDistance(x1, x2);

            // ignore pairs with x1 == x2
            if(p > 0) {

                // line 3.4
                alpha = min(lambda, delta / 2.0 * (1.0 / p - gamma / slack));

                // line 3.5
                beta = delta * alpha / (1.0 - delta * alpha * p);

                /*
                // prints debug information
                cout << "p: " << p << endl;
                cout << "alpha: " << alpha << endl;
                cout << "1.0 / p: " << (1.0 / p) << endl;
                cout << "gamma / slack: " << (gamma / slack) << endl;
                cout << x1.t() << endl << x2.t() << endl;
                cout << "delta / 2.0 * (1.0 / p - gamma / slack): " << (delta / 2.0 * (1.0 / p - gamma / slack)) << endl;
                cout << "beta: " << beta << endl;
                */

                // line 3.6
                double nextSlack = gamma * slack / (gamma + delta * alpha * slack);
                currentConstSet->setSlack(currentIdx, nextSlack);

                // line 3.7
                double nextLambda = lambda - alpha;
                currentConstSet->setLambda(currentIdx, nextLambda);

                // line 3.8
                mat currentM = metric.getM();
                currentM = currentM + beta * currentM * (x1 - x2) * (x1 - x2).t() * currentM;
                metric.setM(currentM);

            }

        }

        cout << "(InfTheoMetricLearner) metric learning converged in " << i << " iterations" << endl;

        return metric;

    }