Пример #1
0
HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
{
    double lowValue = low();
    double highValue = high();
    double theValue = value();
    double optimumValue = optimum();

    if (optimumValue < lowValue) {
        // The optimum range stays under low
        if (theValue <= lowValue)
            return GaugeRegionOptimum;
        if (theValue <= highValue)
            return GaugeRegionSuboptimal;
        return GaugeRegionEvenLessGood;
    }
    
    if (highValue < optimumValue) {
        // The optimum range stays over high
        if (highValue <= theValue)
            return GaugeRegionOptimum;
        if (lowValue <= theValue)
            return GaugeRegionSuboptimal;
        return GaugeRegionEvenLessGood;
    }

    // The optimum range stays between high and low.
    // According to the standard, <meter> never show GaugeRegionEvenLessGood in this case
    // because the value is never less or greater than min or max.
    if (lowValue <= theValue && theValue <= highValue)
        return GaugeRegionOptimum;
    return GaugeRegionSuboptimal;
}
HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
{
    double lowValue = low();
    double highValue = high();
    double theValue = value();
    double optimumValue = optimum();

    if (optimumValue <= lowValue) {
        // The optimum range stays under low
        if (theValue <= lowValue)
            return GaugeRegionOptimum;
        if (theValue <= highValue)
            return GaugeRegionSuboptimal;
        return GaugeRegionEvenLessGood;
    }
    
    if (highValue <= optimumValue) {
        // The optimum range stays over high
        if (highValue <= theValue)
            return GaugeRegionOptimum;
        if (lowValue <= theValue)
            return GaugeRegionSuboptimal;
        return GaugeRegionEvenLessGood;
    }

    // The optimum range stays between high and low
    if (lowValue < theValue && theValue < highValue)
        return GaugeRegionOptimum;
    if (theValue == min() || max() == theValue)
        return GaugeRegionEvenLessGood;
    return GaugeRegionSuboptimal;
}
Пример #3
0
SEXP Expect_matrix(SEXP S1, SEXP S, SEXP lambda, SEXP time, SEXP theta0, SEXP theta1, SEXP matdiag){
    
    int nvar, npoints, nprotect=0;
    
    nvar = GET_LENGTH(lambda);
    npoints = GET_LENGTH(time);
    
    PROTECT(time = coerceVector(time,REALSXP)); nprotect++;
    PROTECT(theta0 = coerceVector(theta0,REALSXP)); nprotect++;
    PROTECT(theta1 = coerceVector(theta1,REALSXP)); nprotect++;
    // results
    SEXP expectation = PROTECT(allocVector(REALSXP,nvar*npoints)); nprotect++;
    
    if(!isComplex(lambda)){
    // eigenvectors
    PROTECT(S1 = coerceVector(S1,REALSXP)); nprotect++;
    PROTECT(S = coerceVector(S,REALSXP)); nprotect++;
    // matrix exponential
    SEXP matexp = PROTECT(allocVector(REALSXP,nvar*nvar*npoints)); nprotect++;

    // Compute the exponential matrix
    multi_exp_matrix (&nvar, &npoints, REAL(time), REAL(lambda), REAL(S), REAL(S1), REAL(matexp));
    
    // Compute the expectations
    optimum (&nvar, &npoints, REAL(time), REAL(theta0), REAL(theta1), REAL(expectation), REAL(matexp), REAL(matdiag));
    // Done.
        
    }else{
    
    double complex *matexp;
    // complex eigenvalues & eigenvectors
    PROTECT(S1 = coerceVector(S1,CPLXSXP)); nprotect++;
    PROTECT(S = coerceVector(S,CPLXSXP)); nprotect++;
        
    // alloc a complex vector in C rather than R structure...
    matexp = Calloc(nvar*nvar*npoints,double complex);
        
    // Compute the exponential matrix
    multi_exp_matrix_complex (&nvar, &npoints, REAL(time), COMPLEX(lambda), COMPLEX(S), COMPLEX(S1), matexp);
        
    // Compute the expectations
    optimum_complex(&nvar, &npoints, REAL(time), REAL(theta0), REAL(theta1), REAL(expectation), matexp, REAL(matdiag));
    // Done.
    // Free the memory
    Free(matexp);
    }


    UNPROTECT(nprotect);
    return expectation;
    
}
Пример #4
0
int Simplex::simplex() {
    if (SIMPLEX_DEBUG) fprintf(stderr, "Simplex step:\n");
    if (SIMPLEX_DEBUG) printObjective();
    if (SIMPLEX_DEBUG) printTableau();

//	fprintf(stderr, "Objective = %.6f\n", optimum());

//	printTableau(true);

//	checkObjective2();

    if (!findPivotRow()) return SIMPLEX_OPTIMAL;

//	fprintf(stderr, "pivot row = %d\n", pivot_row);

    regeneratePivotRow();

//	checkObjective2();

//	if (!findPivotCol()) {
    if (!findPivotCol2()) {
        mip->unboundedFailure();
        return SIMPLEX_UNBOUNDED;
    }


//	fprintf(stderr, "pivot col = %d\n", pivot_col);

    pivot();

    simplexs++;

//	printB();

//	printTableau(true);

//	checkBasis();

//	printObjective();

//	if (SIMPLEX_DEBUG) printTableau();

//	exit(0);

    // If bound is already good enough to cause failure, return
    if (EARLY_TERM && (int) ceil(optimum()) > mip->objVarBound()) return SIMPLEX_GOOD_ENOUGH;

    return SIMPLEX_IN_PROGRESS;
}
Пример #5
0
void LMA::reset()
{
  // Read out results
  alglib::minlmreport report;
  alglib::minlmresults(state, xIn, report);

  // Set optimum
  optimum.resize(n);
  for(unsigned i = 0; i < n; i++)
    optimum(i) = xIn[i];
  opt->setParameters(optimum);

  // Log result
  OPENANN_DEBUG << "Terminated:";
  OPENANN_DEBUG << report.iterationscount << " iterations";
  OPENANN_DEBUG << report.nfunc << " function evaluations";
  OPENANN_DEBUG << report.njac << " Jacobi evaluations";
  OPENANN_DEBUG << "Error = " << opt->error();
  OPENANN_DEBUG << "Reason:";
  switch(report.terminationtype)
  {
  case 1:
    OPENANN_DEBUG << "Relative function improvement is below threshold.";
    break;
  case 2:
    OPENANN_DEBUG << "Relative step is below threshold.";
    break;
  case 4:
    OPENANN_DEBUG << "Gradient is below threshold.";
    break;
  case 5:
    OPENANN_DEBUG << "MaxIts steps was taken";
    break;
  case 7:
    OPENANN_DEBUG << "Stopping conditions are too stringent, "
                  << "further improvement is impossible.";
    break;
  default:
    OPENANN_DEBUG << "Unknown.";
  }

  iteration = -1;
}
Пример #6
0
void CG::reset()
{
  alglib_impl::ae_state_clear(&envState);

  alglib::mincgreport report;
  alglib::mincgresults(state, xIn, report);
  optimum.resize(n, 1);
  for(unsigned i = 0; i < n; i++)
    optimum(i) = xIn[i];

  OPENANN_DEBUG << "CG terminated";
  OPENANN_DEBUG << report.iterationscount << " iterations";
  OPENANN_DEBUG << report.nfev << " function evaluations";
  OPENANN_DEBUG << "reason: ";
  switch(report.terminationtype)
  {
  case 1:
    OPENANN_DEBUG << "Relative function improvement is no more than EpsF.";
    break;
  case 2:
    OPENANN_DEBUG << "Relative step is no more than EpsX.";
    break;
  case 4:
    OPENANN_DEBUG << "Gradient norm is no more than EpsG.";
    break;
  case 5:
    OPENANN_DEBUG << "MaxIts steps was taken.";
    break;
  case 7:
    OPENANN_DEBUG << "Stopping conditions are too stringent, further"
                  << " improvement is impossible, we return the best "
                  << "X found so far.";
    break;
  case 8:
    OPENANN_DEBUG << "Terminated by user.";
    break;
  default:
    OPENANN_DEBUG << "Unknown.";
  }

  iteration = -1;
}
/**
 * Genera una solucion inicial, utilizando una heuristica greedy:
 * si no se viola la condicion de maximo de creditos y cursos:
 *   - asignar el curso j al periodo i.
 *   - arreglar colisiones de prerequisitos moviendo al siguiente periodo
 *     el curso que no cumple.
 * si no
 *   - avanzar el periodo i al i + 1
 *
 * repetir hasta haber asignado todos los prerequisitosk
 *
 * @bacp_instance :: instancia del problema bacp
 */
void initial_solution(struct bacp_instance *instance)
{
  unsigned short int j;
  unsigned short int i = 0;
  unsigned short int cpp;
  unsigned short int cred;
  unsigned short int optimal_load = optimum(instance) + 1;

  for (j = 0; j < instance->n_courses; j++) {
    cpp  = courses_per_period(instance, i) + 1;
    cred = credits(instance, i) + instance->credits[j];

    /* 1. Nos pasamos de los cursos maximos al agregar este? */
    /* 2. Nos pasamos de los creditos maximos al agregar este? */
    if ( cpp > instance->max_courses || cred > optimal_load)
      i = (i + 1) % instance->n_periods;

    instance->period[j] = i;
    fix_collitions(instance, i);
  }
}
/**
* Actual CTMDPI bounded reachability algorithm.
*
* @param unsigned num_states number of states of CTMDPI
* @param fg Poisson values by Fox-Glynn algorithm
* @param left_end use Poisson probabilities up to left limit
* @param min true for minimal probability, false for maximal one
* @param row_starts see CTMDPI documentation
* @param choice_starts see CTMDPI documentation
* @param non_zeros see CTMDPI documentation
* @param q current probability vector
* @param q_primed next probabilities vector
* @param cols see CTMDPI documentation
* @param P_s_alpha_B P(s,alpha,B) (see paper)
* @param B target states
*/
static double *ctmdpi_iter( const unsigned num_states, const FoxGlynn *fg, const unsigned left_end,
                                const BOOL min, const int * row_starts,
                                const int * choice_starts,
				const double *non_zeros, double *q, double *q_primed, const unsigned *cols,
				const double *P_s_alpha_B, const bitset *B) {
       /* in the algorithm the main iteration is in lines 3-12. For each
	* jump probability k starting from the right bound in the Fox-Glynn
	* algorithm down to 1 the loop body is to be executed once. In the
	* implementation here we split this loop into two parts: One for
	* the non-negligible Poisson probabilities between left and right
	* bound of the Fox-Glynn algorithm and one for the jump
	* probabilities below left bound, if any.
	*/
	unsigned i;

        state_index row;
        bitset * not_B;

	/* first part - use poisson probabilities */

	/* line 3 in paper */
	for (i = fg->right; i >= left_end; i--) {
		unsigned ps_index = 0;
		double psi = fg->weights[i-fg->left] / fg->total_weight;
		/* lines 4-12 in paper */
                for ( row = 0 ; (unsigned) row < num_states ; row++ ) {
			BITSET_BLOCK_TYPE s_in_B;
			s_in_B = get_bit_val(B, row);
			if ( s_in_B == BIT_OFF ) {
				/* get maximizing/minimizing decision */
				/* lines 4-10 in paper (non-target state) */
                                double m = min ? 2.0 : -1.0;
				unsigned l1 = row_starts[row];
				unsigned h1 = row_starts[row+1];
				unsigned j;
				for (j = l1; j < h1; j++) {
					double m_primed = get_choice_probability_psi
					(j, choice_starts, non_zeros, cols,q, P_s_alpha_B, ps_index, psi);
					m = optimum(min, m, m_primed);
					ps_index++;
				}
				/* in case some non-target state did
				 * not have any possible decisions we
				 * set m to zero afterwards */
				m = ((-1.0 == m) || (2.0 == m)) ? 0.0 : m;
				/* line 9 in paper*/
				q_primed[row] = m;
			} else {
				/* line 11 in paper (target state) */
				q_primed[row] = psi + q[row];
			}
		}
		/* swapping is done instead using a new q vector for
		 * each k */
		swap(&q, &q_primed);
	}

	/* we have to set the q_primed values for target states once
	 * more now; they won't change afterwards, as psi =
	 * 0.0. We do here what would be done in the first iteration
	 * of the next loop. As the values for q_primed do not
	 * influence the first iteration of the next loop, we can do
	 * this beforehand to avoid having to do this in each loop. */
        row = state_index_NONE;
        while ( (row = get_idx_next_non_zero(B, row)) != state_index_NONE )
        { /* target states only */
	    q_primed[row] = q[row];
	}

        not_B = not(B);
	/* now do part where we are below left bound of poisson probabilities */
	for (; i > 0; i--) {
		unsigned ps_index = 0;
		/* lines 4-12 in paper */
                row = state_index_NONE;
                while ( (row = get_idx_next_non_zero(not_B, row))
                                        != state_index_NONE )
                { /* non-target state? */
				/* get maximizing/minimizing decision */
				/* lines 4-10 in paper (non-target state) */
				double m = min ? 2 : -1.0;
				unsigned l1 = row_starts[row];
				unsigned h1 = row_starts[row+1];
				unsigned j;
				for (j = l1; j < h1; j++) {
					double m_primed = get_choice_probability
					(j, choice_starts, non_zeros, cols, q);
					m = optimum(min, m, m_primed);
					ps_index++;
				}
				/* in case some non-target state did
				 * not have any possible decisions we
				 * set m to zero afterwards */
				m = ((-1.0 == m) || (2.0 == m)) ? 0.0 : m;
				/* line 9 in paper*/
				q_primed[row] = m;
		}
		/* swapping is done instead using a new q vector for
		 * each k */
		swap(&q, &q_primed);
	}

        free_bitset(not_B);

	return q;
}
/**
 * Compute transient reachability probabilities in nonuniform CTMDPs.
 *
 * @param ctmdp CTMDP to compute probabilities for
 * @param rates vector of different rates occuring in CTMDP
 * @param num_rates size of rates vector
 * @param choices_to_rates maps choice numbers to corresponding rate index
 * @param k_bound maximal cardinality to consider
 * @
 */
static double *ctmdpi_iterate
(const NDSparseMatrix *ctmdp, const double *rates, const unsigned num_rates,
 const unsigned *choices_to_rates, const unsigned k_bound, const bitset *B,
 const double time_bound, const BOOL min)
{
	double *probs_k;
	double *probs_km1;
	unsigned state_nr;
	double li;
	unsigned prob_nr;
	unsigned vec_nr;
	unsigned num_k_vecs;
	unsigned num_km1_vecs;
	unsigned *rates_quant;
	unsigned *all_k_rates_quant;
	unsigned *all_km1_rates_quant;
	unsigned k;
	const unsigned num_states = (unsigned) ctmdp->n;
	const unsigned max_entries = multiset_coeff(num_rates, k_bound);
	unsigned succ_nr;
	unsigned choice_nr;
	double choice_prob;
	double succ_prob;
	unsigned succ_vec_nr;
	const unsigned *row_starts = (unsigned *) ctmdp->row_counts;
	const unsigned *choice_starts = (unsigned *) ctmdp->choice_counts;
	unsigned rate_nr;
	double *non_zeros = ctmdp->non_zeros;
	hypoexp_t *hypoexp;
	unsigned *rate_to_succ_vec_nr;

	probs_k = malloc(max_entries * num_states * sizeof(double));
	probs_km1 = malloc(max_entries * num_states * sizeof(double));
	all_k_rates_quant = malloc(max_entries * num_rates * sizeof(unsigned));
	all_km1_rates_quant = malloc(max_entries * num_rates * sizeof(unsigned));
	enumerate_k_vectors(num_rates, k_bound, all_k_rates_quant, &num_k_vecs);
	hypoexp = init_hypoexp(k_bound, rates, num_rates, time_bound);
	rate_to_succ_vec_nr = malloc(num_rates * sizeof(unsigned));

	compute_k_bound_probs(all_k_rates_quant, num_k_vecs, num_rates,
			      hypoexp, k_bound, num_states, B, probs_k);

	for (k = k_bound; k > 0; k--) {
		enumerate_k_vectors(num_rates, k - 1, all_km1_rates_quant, &num_km1_vecs);
		prob_nr = 0;
		for (vec_nr = 0; vec_nr < num_km1_vecs; vec_nr++) {
			rates_quant = get_ith_vec(all_km1_rates_quant, vec_nr, num_rates);
			li = hypoexp_cumulate(rates_quant, k - 1, hypoexp);
			for (rate_nr = 0; rate_nr < num_rates; rate_nr++) {
				rates_quant[rate_nr] += 1;
				succ_vec_nr = vector_to_number
						(rates_quant, all_k_rates_quant, num_rates, 0, num_k_vecs-1);
				rates_quant[rate_nr] -= 1;
				rate_to_succ_vec_nr[rate_nr] = succ_vec_nr;
			}

			for (state_nr = 0; state_nr < num_states; state_nr++) {
				if (get_bit_val(B, state_nr)) {
					probs_km1[prob_nr] = li;
				} else {
					unsigned state_start = row_starts[state_nr];
					unsigned state_end = row_starts[state_nr + 1];
					double opt_choice_prob = min ? 2.0 : -1.0;
					for (choice_nr = state_start; choice_nr < state_end; choice_nr++) {
						unsigned choice_start = choice_starts[choice_nr];
						unsigned choice_end = choice_starts[choice_nr + 1];
						choice_prob = 0.0;
						rate_nr = choices_to_rates[choice_nr];
						succ_vec_nr = rate_to_succ_vec_nr[rate_nr];
						for (succ_nr = choice_start; succ_nr < choice_end; succ_nr++) {
							succ_prob = probs_k[succ_vec_nr * num_states + (ctmdp->cols)[succ_nr]];
							choice_prob += non_zeros[succ_nr] * succ_prob;
						}
						opt_choice_prob = optimum(min, choice_prob, opt_choice_prob);
					}
					if (state_start == state_end) {
						opt_choice_prob = 0.0;
					}
					probs_km1[prob_nr] = opt_choice_prob;
				}
				prob_nr++;
			}
		}
		swap_double(&probs_k, &probs_km1);
		swap_unsigned(&all_k_rates_quant, &all_km1_rates_quant);
		num_k_vecs = num_km1_vecs;
	}

	free_hypoexp(hypoexp);
	free(probs_km1);
	free(all_k_rates_quant);
	free(all_km1_rates_quant);
	free(rate_to_succ_vec_nr);

	return probs_k;
}