Beispiel #1
0
local void add_entry_to_result
(
  wn_sparse_matrix_entry entry,
  wn_sparse_matrix cost_mat
)
{
  double flow;
  perif_data i_perif,j_perif;
  int i,j;

  i = entry->i;
  j = entry->j;

  i_perif = i_perif_array[i];
  j_perif = j_perif_array[j];

  flow = wn_min(i_perif->remaining_capacity,j_perif->remaining_capacity);

  if(flow > 0.0)
  {
    wn_insert_sparse_matrix_value(first_result,flow,i,j);
  }

  --number_of_basics_still_needed;

  i_perif->remaining_capacity -= flow;
  j_perif->remaining_capacity -= flow;

  if(i_perif->remaining_capacity == 0.0)
  {
    if(j_perif->remaining_capacity == 0.0)
    {
      if(i_perif->remaining_entry_count < j_perif->remaining_entry_count)
      {
	i_perif_is_saturated(i_perif,cost_mat);
      }
      else
      {
	j_perif_is_saturated(j_perif,cost_mat);
      }
    }
    else
    {
      wn_assert(j_perif->remaining_capacity > 0.0);

      i_perif_is_saturated(i_perif,cost_mat);
    }
  }
  else
  {
    wn_assert(i_perif->remaining_capacity > 0.0);

    if(j_perif->remaining_capacity == 0.0)
    {
      j_perif_is_saturated(j_perif,cost_mat);
    }
    else
    {
      wn_assert_notreached();
    }
  }
}
Beispiel #2
0
int main(int argc, char *argv[])
{
  int i1,i2;
  char override_buffer[1000];
  int override_line_num;
  enum { lo_a, lo_b } tst_enum;
  int sts;

  i1 = 1;
  i2 = 2;

  if (4 == argc  &&  !strcmp("-override_code", argv[1]))
  {
    sts = sscanf(argv[3], "%d", &override_line_num);
    if (1 != sts)
    {
      fprintf(stderr,
      /**/	"Usage: %s -override_code <filename> <line#>\n", argv[0]);

      return 0;
    }
    wn_get_assert_override_string(override_buffer, argv[2], override_line_num);
    printf("Override string == \"%s\"\n", override_buffer);

    return 0;
  } else if (2 == argc  &&  !strcmp("-sizeofptr", argv[1])) {
    printf("%d\n", sizeof(ptr));

    return 0;
  } else if (2 == argc  &&  !strcmp("-os", argv[1])) {
#   if defined(WN_WINDOWS)
      printf("OS = Windows\n");
#   endif
#   if defined(WN_VMS)
      printf("OS = VMS\n");
#   endif
#   if defined(WN_UNIX)
      printf("OS = Unix\n");
#   endif
  }


  printf("i1=%d,i2=%d\n",i1,i2);
  printf("swapping\n");
  wn_swap(i1,i2,int);
  printf("i1=%d,i2=%d\n",i1,i2);

# define LOC_PRINT_SIZEOF(t) \
  printf("sizeof(" #t ") == %d\n", sizeof(t))

  LOC_PRINT_SIZEOF(char);
  LOC_PRINT_SIZEOF(bool);
  LOC_PRINT_SIZEOF(short);
  LOC_PRINT_SIZEOF(tst_enum);
  LOC_PRINT_SIZEOF(int);
  LOC_PRINT_SIZEOF(long);
  LOC_PRINT_SIZEOF(float);
  LOC_PRINT_SIZEOF(char *);
  LOC_PRINT_SIZEOF(ptr);
  LOC_PRINT_SIZEOF(double);

  printf("first, warn assert....\n");
  wn_assert_warn(1 == 2);
  printf("next, warn notreached....\n");
  wn_assert_warn_notreached();
  if (2 == argc  &&  !strcmp("-notreached", argv[1]))
  {
    printf("now, crash notreached....\n");
    wn_assert_notreached();
  }
  else
  {
    printf("now, crash assert....\n");
    wn_assert(1 == 2);
  }

  return(0);
} /* main */
Beispiel #3
0
local void line_minimize
(
  wn_cdn_context_type c,
  wn_cdn_srchdir_type search_direction,
  double *px_width_sum
)
{
  double *search_direction_vect;
  double poly[3],poly_total_noise[3];
  double xopt,fopt,dfopt,fopt_noise,dfopt_noise;
  double x_width;
  double x0,x1,x2,f0,f1,f2,df0,df1,df2,
         f0_noise,f1_noise,f2_noise,df0_noise,df1_noise,df2_noise;
  int code;
  double a;
  double diff_noise;

  printf("line minimize: coord = %d\n",
         search_direction->coord);
  printf("  x_width = %lg\n",search_direction->x_width);
  x_width = search_direction->x_width;

  find_taylor_poly(c,poly,poly_total_noise,search_direction,0.0);

  if(poly_total_noise[1] == 0.0)
  {
    search_direction->ratio_df1_noise = WN_FHUGE;
  }
  else
  {
    search_direction->ratio_df1_noise = wn_abs(poly[1])/poly_total_noise[1];
  }

  printf("  x_width = %lg\n",search_direction->x_width);

  printf("poly=\n");
  wn_print_vect(poly,3);
  printf("poly_total_noise=\n");
  wn_print_vect(poly_total_noise,3);

/* try to find min using slope and curvature - this should work 
   most of the time */
  if(poly[2] >= 2.0*poly_total_noise[2])
  {
    xopt = -0.5*poly[1]/poly[2];

    printf("  xopt = %lg\n",xopt);

    if((-x_width < xopt)&&(xopt < x_width))
    {
      printf("slope-curvature method successful!!!!!!\n");
      fopt = wn_eval_poly(xopt,poly,3);

      goto finish;
    }
    else
    {
      printf("slope-curvature method failed because xopt too far out\n");
      x0 = xopt;
    }
  }
  else
  {
    printf("slope-curvature method failed because non-positive curvature\n");
    x0 = search_direction->x0;
    if(x0 == 0.0)
    {
      x0 = search_direction->x_width;
    }

    if(poly[1] < 0.0)
    {
      x0 = wn_abs(x0);
    }
    else if(poly[1] > 0.0)
    {
      x0 = -wn_abs(x0);
    }
  }
  
  if((-search_direction->x_width < x0)&&(x0 <  search_direction->x_width))
  {
    if(x0 < 0.0)
    {
      x0 = -search_direction->x_width;
    }
    else
    {
      wn_assert(x0 >= 0.0);

      x0 = search_direction->x_width;
    }
  }

/* try secant method using slopes */
secant: ;
  printf("trying secant method....\n");
  x1 = 0.0;
  f1 = poly[0];
  df1 = poly[1];
  f1_noise = poly_total_noise[0];
  df1_noise = poly_total_noise[1];

  find_taylor_poly(c,poly,poly_total_noise,search_direction,x0);
  f0 = poly[0];
  df0 = poly[1];
  f0_noise = poly_total_noise[0];
  df0_noise = poly_total_noise[1];

  printf("x0 = %lg\n",x0);
  printf("poly=\n");
  wn_print_vect(poly,3);
  printf("poly_total_noise=\n");
  wn_print_vect(poly_total_noise,3);

  wn_fit_parabola_2d(&code,&a,&xopt,x0,df0,x1,df1);

  printf("code = %d,a = %lg, xopt = %lg\n",code,a,xopt);

  if((code != WN_SUCCESS)||(!(a > 0.0))||
     (!(wn_abs(xopt)<MAX_EXPAND*wn_abs(x0))))
  {
    printf("secant method failed because parabola fit failed.\n");
    goto full_linesearch;
  }   

  find_taylor_poly(c,poly,poly_total_noise,search_direction,xopt);
  fopt = poly[0];
  dfopt = poly[1];
  fopt_noise = poly_total_noise[0];
  dfopt_noise = poly_total_noise[1];

  printf("xopt = %lg\n",xopt);
  printf("poly=\n");
  wn_print_vect(poly,3);
  printf("poly_total_noise=\n");
  wn_print_vect(poly_total_noise,3);

  diff_noise = sqrt(wn_square(f1_noise)+wn_square(fopt_noise));
  if((fopt-f1) > 2.0*diff_noise)
  {
    printf("secant method failed because f increased too much\n");
    goto full_linesearch;
  }

  if(!(wn_abs(dfopt) <= wn_abs(df1)))
  {
    printf("secant method failed because df increased\n");
    goto full_linesearch;
  }

  printf("secant method successful!!!!!!\n");
  goto finish;

/* full linesearch:  search out, or in by 2x steps*/
full_linesearch: ;
  if(wn_abs(df1) <= 2.0*df1_noise)
  {
    xopt = x1;
    fopt = f1;
    dfopt = df1;
    printf("full linesearch failed because slope indistinguishable from 0\n");
    goto finish;
  }
  
  if(df1 < 0.0)
  {
    wn_assert(x0 > 0.0);
  }
  else if(df1 > 0.0)
  {
    wn_assert(x0 < 0.0);
  }
  else
  {
    wn_assert_notreached();
  }

  printf("df1 = %lg,df0 = %lg\n",df1,df0);
  if(wn_sign(df1) == wn_sign(df0))
  {
    printf("searching outward....\n");

    /* search outward until slopes differ */
    while(wn_sign(df1) == wn_sign(df0))
    { 
      xopt = x0;
      fopt = f0;
      dfopt = df0;

      x0 *= 2.0;

      find_taylor_poly(c,poly,poly_total_noise,search_direction,x0);
      f0 = poly[0];
      df0 = poly[1];

      printf("xopt=%lg,fopt=%lg,dfopt=%lg,x0=%lg,f0=%lg,df0=%lg,x1=%lg,f1=%lg,df1=%lg\n",
             xopt,fopt,dfopt,x0,f0,df0,x1,f1,df1);
    }
  }
  else if(wn_sign(df1) == -wn_sign(df0))
  {
    xopt = x0;
    fopt = f0;
    dfopt = df0;

    printf("searching inward....\n");

    while(wn_sign(dfopt) == -wn_sign(df1))
    {
      xopt *= 0.5;

      find_taylor_poly(c,poly,poly_total_noise,search_direction,xopt);
      fopt = poly[0];
      dfopt = poly[1];

      printf("xopt=%lg,fopt=%lg,dfopt=%lg,x1=%lg,f1=%lg,df1=%lg\n",
             xopt,fopt,dfopt,x1,f1,df1);
    }
  }
  else
  {
    wn_assert_notreached();
  }

finish: ;
  compute_search_direction_vect(c,&search_direction_vect,search_direction);
  wn_cdn_compute_func_call_vect(c,search_direction_vect,xopt);
  wn_copy_vect(c->current_vect,c->func_call_vect,c->num_vars);
  retire_search_direction_vect(c,&search_direction_vect,search_direction);

  if(xopt != 0.0)
  {
    (*px_width_sum) += wn_abs(xopt)/search_direction->x_width;
  }

  c->ob = fopt;

  printf("finish:  xopt = %lg, fopt = %lg\n",xopt,fopt);
}
Beispiel #4
0
/*[\RJL]*/
void lo_update_level_and_balance(
  wn_mbhandle *ploop_handle,
  wn_mbtree tree
)
{
  int diff;
  wn_mbhandle handle, left_child, right_child, parent, n4, n5, n6, n7;

  handle = *ploop_handle;
  left_child = handle->left_child;
  right_child = handle->right_child;

  diff = lo_handle_tree_level(left_child)-lo_handle_tree_level(right_child);

  switch(diff)
  {
    default:    /* wn_abs(diff) > 2 */
      wn_assert_notreached();  /* should never happen */

    case(-1):   /* not unbalanced enough to remedy */
    case(0):
    case(1):
    {
      lo_calculate_handle_count_and_level_from_children(handle);
    } break;
    case(-2):
    {
      n4 = right_child->right_child;
      n5 = right_child->left_child;
      parent = handle->parent;

      if(lo_handle_tree_level(n4) >= lo_handle_tree_level(n5))
      {
        right_child->left_child = handle;
        handle->parent = right_child;

        *ploop_handle = right_child;
        right_child->parent = parent;

        handle->left_child = left_child;
        if(left_child != NULL)
        {
          left_child->parent = handle;
        }

        right_child->right_child = n4;
        n4->parent = right_child;

        handle->right_child = n5;
        if(n5 != NULL)
        {
          n5->parent = handle;
        }

        lo_calculate_handle_count_and_level_from_children(handle);
        lo_calculate_handle_count_and_level_from_children(right_child);
      }
      else /* lo_handle_tree_level(n4) < lo_handle_tree_level(n5) */
      {
        n6 = n5->right_child;
        n7 = n5->left_child;

        n5->left_child = handle;
        handle->parent = n5;

        n5->right_child = right_child;
        right_child->parent = n5;

        handle->left_child = left_child;
        if(left_child != NULL)
        {
          left_child->parent = handle;
        }

        right_child->right_child = n4;
        if(n4 != NULL)
        {
          n4->parent = right_child;
        }

        *ploop_handle = n5;
        n5->parent = parent;

        right_child->left_child = n6;
        if(n6 != NULL)
        {
          n6->parent = right_child;
        }

        handle->right_child = n7;
        if(n7 != NULL)
        {
          n7->parent = handle;
        }

        lo_calculate_handle_count_and_level_from_children(right_child);
        lo_calculate_handle_count_and_level_from_children(handle);
        lo_calculate_handle_count_and_level_from_children(n5);
      }

      lo_update_parent(*ploop_handle,handle,parent,tree);
    } break;
    case(2):
    {
      n4 = left_child->left_child;
      n5 = left_child->right_child;
      parent = handle->parent;

      if(lo_handle_tree_level(n4) >= lo_handle_tree_level(n5))
      {
        left_child->right_child = handle;
        handle->parent = left_child; 

        *ploop_handle = left_child;
        left_child->parent = parent;

        handle->right_child = right_child;
        if(right_child != NULL)
        {
          right_child->parent = handle; 
        }

        left_child->left_child = n4;
        n4->parent = left_child;  

        handle->left_child = n5;
        if(n5 != NULL)
        {
          n5->parent = handle;
        }

        lo_calculate_handle_count_and_level_from_children(handle);
        lo_calculate_handle_count_and_level_from_children(left_child);
      }
      else /* lo_handle_tree_level(n4) < lo_handle_tree_level(n5) */
      {
        n6 = n5->left_child;
        n7 = n5->right_child;

        n5->right_child = handle;
        handle->parent = n5;

        n5->left_child = left_child;
        left_child->parent = n5;

        handle->right_child = right_child;
        if(right_child != NULL)
        {
          right_child->parent = handle;
        }

        left_child->left_child = n4;
        if(n4 != NULL)
        {
          n4->parent = left_child; 
        }

        *ploop_handle = n5;
        n5->parent = parent;

        left_child->right_child = n6;
        if(n6 != NULL)
        {
          n6->parent = left_child;  
        }

        handle->left_child = n7;
        if(n7 != NULL)
        {
          n7->parent = handle; 
        }

        lo_calculate_handle_count_and_level_from_children(left_child);
        lo_calculate_handle_count_and_level_from_children(handle);
        lo_calculate_handle_count_and_level_from_children(n5);
      }

      lo_update_parent(*ploop_handle,handle,parent,tree);
    } break;
  }
}