Esempio n. 1
0
int add_solution(char board[8][8], solution **head) {
  if (*head) {
    solution *n;
    solution *tmp = *head;

    while (tmp) {
      if (compare_boards(board, tmp->solution))
	return 0;
      tmp = tmp->next;
    }
    if (!(n = malloc(sizeof(*n)))) {
      return 0;
    }
    tmp = *head;
    while (tmp->next)
      tmp = tmp->next;
    tmp-> next = n;
    n->next = 0;
    copy_solution(n, board);
    return 1;
  }
  if (!(*head = malloc(sizeof(solution))))
    return 0;
  (*head)->next = 0;
  copy_solution(*head, board);
  return 1;
}
Esempio n. 2
0
static FuncResult validate_hyperbolic_structure(
    Triangulation   *manifold)
{
    int i;

    /*
     *  First make sure some sort of solution is in place.
     */
    if (manifold->solution_type[complete] == not_attempted)
        find_complete_hyperbolic_structure(manifold);

    /*
     *  If the solution is something other than geometric_solution
     *  or nongeometric_solution, we're out of luck.
     */
    if (manifold->solution_type[complete] != geometric_solution
     && manifold->solution_type[complete] != nongeometric_solution)
        return func_failed;

    /*
     *  Overwrite the filled structure with the complete structure
     *  to keep the low-level retriangulation routines happy.
     *  (See the technical note at the top of this file for a
     *  more complete explanation.)
     */
    copy_solution(manifold, complete, filled);

    /*
     *  If all Tetrahedra are positively oriented, we're golden.
     */
    if (manifold->solution_type[complete] == geometric_solution)
        return func_OK;

    /*
     *  Try to find a geometric_solution by randomizing the Triangulation.
     *  If we can't find one within MAX_RETRIANGULATIONS randomizations,
     *  give up and return func_failed.
     */

    for (i = 0; i < MAX_RETRIANGULATIONS; i++)
    {
        randomize_triangulation(manifold);
        if (manifold->solution_type[complete] == geometric_solution)
            return func_OK;
    }

    /*
     *  Before we go, we'd better restore the filled solution.
     */
    polish_hyperbolic_structures(manifold);

    return func_failed;
}
/**
 * Loop principal del algoritmo Simulated Annealing.
 *
 * @instance  :: Instancia del BACP
 * @iter      :: Numero maximo de iteraciones para una temperatura t_current
 * @t_current :: Temperatura inicial
 * @t_min     :: Temperatura minima (eps)
 * @alpha     :: Velocidad de decaimiento de la temperatura
 */
void run(struct bacp_instance *instance, int iter, float t_current, float t_min, float alpha)
{
  unsigned short int i;
  unsigned short int new_cost;
  unsigned short int *s_c      = instance->period;
  unsigned short int old_cost  = cost(instance);
  unsigned short int best_cost = old_cost;
  unsigned short int *s_best   = copy_solution(instance->period, instance->n_courses);

  instance->period = NULL;
  while (t_current > t_min) {
    i = 0;
    while (++i <= iter) {
      if (instance->period) free(instance->period);

      instance->period = s_c;
      instance->period = neighbour(instance);
      new_cost         = cost(instance);

      if (new_cost < old_cost || (exp(-abs(new_cost - old_cost)/t_current) > aceptar())) {
        free(s_c);
        s_c              = instance->period;
        old_cost         = new_cost;
        instance->period = NULL;
      }

      if ( best_cost > new_cost ) {
        if (s_best) free(s_best);
        s_best    = copy_solution(s_c, instance->n_courses);
        best_cost = new_cost;
      }
    }
    t_current = t_current * alpha;
  }

  if (instance->period) free(instance->period);
  if (s_c) free(s_c);

  instance->period = s_best;
}
Esempio n. 4
0
enum isl_lp_result isl_pip_solve_lp(struct isl_basic_map *bmap, int maximize,
				      isl_int *f, isl_int denom, isl_int *opt,
				      isl_int *opt_denom,
				      struct isl_vec **vec)
{
	enum isl_lp_result res = isl_lp_ok;
	PipMatrix	*domain = NULL;
	PipOptions	*options;
	PipQuast   	*sol;
	unsigned	 total;

	total = isl_basic_map_total_dim(bmap);
	domain = isl_basic_map_to_pip(bmap, 0, 1, 0);
	if (!domain)
		goto error;
	entier_set_si(domain->p[0][1], -1);
	isl_int_set(domain->p[0][domain->NbColumns - 1], f[0]);
	isl_seq_cpy_to_pip(domain->p[0]+2, f+1, total);

	options = pip_options_init();
	if (!options)
		goto error;
	options->Urs_unknowns = -1;
	options->Maximize = maximize;
	options->Nq = 0;
	sol = pip_solve(domain, NULL, -1, options);
	pip_options_free(options);
	if (!sol)
		goto error;

	if (vec) {
		isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
		*vec = isl_vec_alloc(ctx, 1 + total);
	}
	if (vec && !*vec)
		res = isl_lp_error;
	else if (!sol->list)
		res = isl_lp_empty;
	else if (entier_zero_p(sol->list->vector->the_deno[0]))
		res = isl_lp_unbounded;
	else
		copy_solution(*vec, maximize, opt, opt_denom, sol);
	pip_matrix_free(domain);
	pip_quast_free(sol);
	return res;
error:
	if (domain)
		pip_matrix_free(domain);
	return isl_lp_error;
}
Esempio n. 5
0
void perm_sub(int n,int lst[]){
  int i;
  if(n==LENGTH){
    double dist = total_dist(lst);
    if(dist<sum){
      sum = dist;
      copy_solution(lst);
    }
  }else{
    for(i=1;i<LENGTH;i++){
      if(!(apper(lst,i,num))){
	if(n!=2 || lst[0]>i){
	  append(lst,i,num);
	  num++;
	  perm_sub(n+1,lst);
	  num--;
	}
      }
    }
  }
}
/**
 * Genera una nueva solucion vecina, cambiando un curso al azar a
 * un periodo al azar
 *
 * @instance :: Instancia del BACP
 *
 * @return   :: nueva solucion (arreglo de unsigned short int)
 */
unsigned short int *neighbour(struct bacp_instance *instance)
{
  unsigned short int course;
  unsigned short int new_period;
  unsigned short int *new_solution;

  /* 0. Duplicar solucion actual */
  new_solution = copy_solution(instance->period, instance->n_courses);

  /* 1. Tomar un curso al azar */
  course = rand()%instance->n_courses;

  /* 2. Asignarlo a un periodo al azar,
   *    si es el mismo que el actual
   *    sumarle 1 % n_periodos */
  new_period = rand()%instance->n_periods;
  if (new_period == instance->period[course])
    new_period = (new_period + 1)%instance->n_periods;

  new_solution[course] = new_period;

  return new_solution;
}
Esempio n. 7
0
/*It solves the problem. If we are deleteting duplicates, it checks that the
 solution is valid.*/
int solve(Satellite *sats, int *combination, int *solution) {
  int i, j, k;
  long long int combs;
  int n;
  int valid;
  float r, num;

  combs = number_of_combinations(sats);
  //  printf("Combinations: %ld\n", combs);
  get_golden_index_max(sats);
  for (k = 0; k < nsats; k++) {
    combination[k] = 0; // We start from the combination 1 1 .. 1
    for (j = 0; j < sats[k].golden_index - 1; j++) {
      // delete_duplicates(sats, j, k);
    }
  }
  // print_F_matrix(sats);
  for (i = 1; i < combs; i++) {
    printf("%d ", i);
    next_combination(sats, combination);
    valid = check_solution(sats, combination);
    if (!valid)
      continue;
    //  print_array("comb", combination, nsats);
    n = total_occurrences(sats, combination);
    //  printf("tic: %.2f\n", tic);
    r = total_reward(sats, combination);
    num = r / n;
    printf("%.2f\n", num);
    if (num > max) {
      max = num;
      copy_solution(combination, solution);
    }
  }
  return 0;
}