Ejemplo n.º 1
0
void DynamicProgrammingQ2(double *Q1, double *T1, double *Q2, double *T2, int m1, int n1, int n2,
double *tv1, double *tv2, int n1v, int n2v, double *G, double *T, double *size, double lam1){
  int *idxv1 = 0;
  int *idxv2 = 0;
  double *E = 0; /* E[ntv1*j+i] = cost of best path to (tv1[i],tv2[j]) */
  int *P = 0; /* P[ntv1*j+i] = predecessor of (tv1[i],tv2[j]) along best path */

  idxv1=(int*)malloc((n1v)*sizeof(int));
  idxv2=(int*)malloc((n2v)*sizeof(int));
  E=(double*)malloc((n1v)*(n2v)*sizeof(double));
  P=(int*)calloc((n1v)*(n2v),sizeof(int));

  /* dp_costs() needs indexes for gridpoints precomputed */
  dp_all_indexes( T1, n1, tv1, n1v, idxv1 );
  dp_all_indexes( T2, n2, tv2, n2v, idxv2 );

  /* Compute cost of best path from (0,0) to every other grid point */
  dp_costs( Q1, T1, n1, Q2, T2, n2, 
    m1, tv1, idxv1, n1v, tv2, idxv2, n2v, E, P, lam1 );

  /* Reconstruct best path from (0,0) to (1,1) */
  *size = dp_build_gamma( P, tv1, n1v, tv2, n2v, G, T );
  
  // free allocated memory
  free(idxv1); free(idxv2); free(E); free(P);
}
Ejemplo n.º 2
0
static void test_dp_edge_weight_basic2()
{
  /* Q1 and Q2 are column-major arrays! */
  double Q1[2*3] = 
  {
    1,1,
    1,1,
    1,1
  };
  double T1[]={0,1.0/3.0,2.0/3.0,1};
  double Q2[2*4] = 
  {
    1,-1,
    -1,1,
    1,-1,
    -1,1
  };
  double T2[]={0,.25,.5,.75,1};
  double tv1[]={0,1.0/3.0,2.0/3.0,1};
  double tv2[]={0,0.5,1};
  int idxv1[4];
  int idxv2[3];
  double W[144]; /* edge weight matrix */
  double E[12];  /* DP cost matrix */
  int P[12];     /* DP predecessor matrix */
  double G[4];   /* gamma function values */
  double T[4];   /* gamma parameter values */
  double retval;

  double Eexp[] = { 
    0.0, 1e9, 1e9, 1e9, 
    1e9, 5.0/3.0, 7.0/3.0, 3, 
    1e9, 8.0/3.0, 10.0/3.0, 4.0
  };

  int nsamps1 = sizeof(T1) / sizeof(double);
  int nsamps2 = sizeof(T2) / sizeof(double);
  int ntv1 = sizeof(tv1) / sizeof(double);
  int ntv2 = sizeof(tv2) / sizeof(double);
  int dim = 2;
  int Gnsamps;
  int i;

  dp_all_indexes(T1,nsamps1,tv1,ntv1,idxv1);
  dp_all_indexes(T2,nsamps2,tv2,ntv2,idxv2);

  retval = dp_costs( Q1, T1, nsamps1, Q2, T2, nsamps2,
    dim, tv1, idxv1, ntv1, tv2, idxv2, ntv2, E, P );
  CU_ASSERT_DOUBLE_EQUAL( retval, E[ntv1*ntv2-1], 1e-3 );

  for ( i=0; i<ntv1*ntv2; ++i )
  {
    CU_ASSERT_DOUBLE_EQUAL( E[i], Eexp[i], 1e-3 );
  }

  dp_all_edge_weights (
    Q1, T1, nsamps1, Q2, T2, nsamps2, 
    dim, tv1, idxv1, ntv1, tv2, idxv2, ntv2, W );

  Gnsamps = dp_build_gamma(P,tv1,ntv1,tv2,ntv2,G,T);
  printf( "Gamma: " );
  for ( i=0; i<Gnsamps; ++i )
  {
    printf( "(%0.3f,%0.3f)", T[i], G[i] );
  }
  printf( "\n" );
}
Ejemplo n.º 3
0
static void test_dp_edge_weight_basic1()
{
  double T1[] = { 0.0, 1.0/6.0, 0.5, 4.0/6.0, 5.0/6.0, 1.0 };
  double Q1[] = { 1.0, -1.0, 1.0, -1.0, 1.0 };
  double T2[] = { 0.0, 2.0/6.0, 5.0/6.0, 1.0 };
  double Q2[] = { -1.0, 1.0, -1.0 };
  double tv1[] = { 0.0, 0.25, 0.5, 0.75, 1.0 };
  double tv2[] = { 0.0, 0.25, 0.5, 0.75, 1.0 };
  int nsamps1 = sizeof(T1) / sizeof(double);
  int nsamps2 = sizeof(T2) / sizeof(double);
  int ntv1 = sizeof(tv1) / sizeof(double);
  int ntv2 = sizeof(tv2) / sizeof(double);
  int dim = 1;

  double W[ntv2*ntv1*ntv2*ntv1]; /* edge weight matrix */
  double E[ntv2*ntv1];  /* DP cost matrix */
  int P[ntv2*ntv1];     /* DP predecessor matrix */
  int idxv1b[ntv1];
  int idxv2b[ntv2];
  double G[ntv1];   /* gamma function values */
  double T[ntv1];   /* gamma parameter values */
  int npts;

  double a[] = { 0.25, 0.75, 0.0, 0.0, 0.25, 1.0/6.0 };
  double b[] = { 0.75, 1.0, 0.75, 0.25, 0.5, 0.5 };
  double c[] = { 0.25, 0.25, 0.0, 0.0, 0.0, 1.0/6.0 };
  double d[] = { 0.5, 1.0, 0.25, 0.25, 0.25, 5.0/6.0 };
  double expected[] = { 0.51430, 0.90377, 0.90377, 0.66667, 0.0, 1.4714 };
  int ncases = sizeof(expected) / sizeof(double);
  int idxv1a, idxv2a;

  double actual;
  int i;

  for ( i=0; i<ncases; ++i )
  {
    /* Can't use dp_all_indexes() for this because a and c are 
     * not sorted in ascending order */
    idxv1a = dp_lookup(T1,nsamps1,a[i]);
    idxv2a = dp_lookup(T2,nsamps2,c[i]);

    actual = dp_edge_weight ( 
      Q1, T1, nsamps1, 
      Q2, T2, nsamps2, 
      dim, a[i], b[i], c[i], d[i], idxv1a, idxv2a );
    CU_ASSERT_DOUBLE_EQUAL( actual, expected[i], 0.0001 );
    if ( fabs(actual - expected[i]) > 0.0001 )
    {
      printf( "\nCase %d: expected %0.5f but got %0.5f\n", i, expected[i], actual );
    }
  }

  dp_all_indexes(T1,nsamps1,tv1,ntv1,idxv1b);
  dp_all_indexes(T2,nsamps2,tv2,ntv2,idxv2b);

  dp_all_edge_weights(Q1,T1,6,Q2,T2,4,1,tv1,idxv1b,ntv1,tv2,idxv2b,ntv2,W);
  dp_costs(Q1,T1,6,Q2,T2,4,1,tv1,idxv1b,ntv1,tv2,idxv2b,ntv2,E,P);
  npts = dp_build_gamma( P, tv1, ntv1, tv2, ntv2, G, T );

  printf( "gamma:\n" );
  for ( i=0; i<npts; ++i )
  {
    printf( "(%0.2f,%0.2f)\n", T[i], G[i] );
  }
}