示例#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);
}
示例#2
0
static void test_dp_edge_weight_basic3()
{
  /* Q1 and Q2 are column-major arrays! */
  double Q1[2*7] = 
  {
    -0.12093, -3.29569,
    -3.99025,  1.89471,
     0.41784, -5.22274,
    -3.41510, -3.55044,
    -4.53925, -2.88102,
    -3.91785, -3.61769,
    -2.95956, -4.41621
  };
  double T1[8] =
  {
    0.00000, 0.14286, 0.28571, 0.42857, 
    0.57143, 0.71429, 0.85714, 1.00000
  };
  double Q2[2*9] = 
  {
    -3.94476, -4.47761,
    -5.49738, -1.25924,
    -6.12657,  0.84389,
    -6.08871, -0.48200,
    -5.89130, -1.34482,
    -6.17836,  0.40485,
    -6.01889,  1.04033,
    -6.09084,  0.86223,
    -5.45081, -2.80984
  };
  double T2[10] = 
  {
    0.00000, 0.11111, 0.22222, 0.33333, 0.44444, 
    0.55556, 0.66667, 0.77778, 0.88889, 1.00000
  };
  int nsamps1 = sizeof(T1) / sizeof(double);
  int nsamps2 = sizeof(T2) / sizeof(double);
  int idxv1[nsamps1];
  int idxv2[nsamps2];
  int dim = 2;
  double E[nsamps1*nsamps2];
  int P[nsamps1*nsamps2];
  double retval;

  dp_all_indexes(T1,nsamps1,T1,nsamps1,idxv1);
  dp_all_indexes(T2,nsamps2,T2,nsamps2,idxv2);

  retval = dp_costs( Q1, T1, nsamps1, Q2, T2, nsamps2,
    dim, T1, idxv1, nsamps1, T2, idxv2, nsamps2, E, P );
  CU_ASSERT_DOUBLE_EQUAL( retval, 19.629, 1e-3 );
}
示例#3
0
static void test_dp_edge_weight_timing1()
{
  double *Q1=0, *T1=0;
  double *Q2=0, *T2=0;
  int *idxv1=0;
  int *idxv2=0;
  double *E=0;
  int *P=0;
  int nsamps1 = 200;
  int nsamps2 = 250;
  
  Q1 = (double*)malloc( (nsamps1-1)*sizeof(double) );
  Q2 = (double*)malloc( (nsamps2-1)*sizeof(double) );
  T1 = (double*)malloc( nsamps1*sizeof(double) );
  T2 = (double*)malloc( nsamps2*sizeof(double) );
  idxv1 = (int*)malloc( nsamps1*sizeof(int) );
  idxv2 = (int*)malloc( nsamps2*sizeof(int) );
  E = (double*)malloc( nsamps1*nsamps2*sizeof(double) );
  P = (int*)malloc( nsamps1*nsamps2*sizeof(int) );

  CU_ASSERT_PTR_NOT_NULL_FATAL(Q1);
  CU_ASSERT_PTR_NOT_NULL_FATAL(Q2);
  CU_ASSERT_PTR_NOT_NULL_FATAL(T1);
  CU_ASSERT_PTR_NOT_NULL_FATAL(T2);
  CU_ASSERT_PTR_NOT_NULL_FATAL(E);
  CU_ASSERT_PTR_NOT_NULL_FATAL(P);

  random_partition( T1, nsamps1 );
  random_partition( T2, nsamps2 );

  dp_all_indexes(T1,nsamps1,T1,nsamps1,idxv1);
  dp_all_indexes(T2,nsamps2,T2,nsamps2,idxv2);

  dp_costs( Q1, T1, nsamps1, Q2, T2, nsamps2, 1, 
    T1, idxv1, nsamps1, T2, idxv2, nsamps2, E, P );

  if ( Q1 ) free(Q1);
  if ( Q2 ) free(Q2);
  if ( T1 ) free(T1);
  if ( T2 ) free(T2);
  if ( idxv1) free(idxv1);
  if ( idxv2) free(idxv2);
  if ( E ) free(E);
  if ( P ) free(P);
}
示例#4
0
static void test_dp_all_indexes_basic1()
{
  double p[]={0.0,0.2,0.33333,0.75,1.0};
  double tv[]={0.0,0.1,0.1999,0.2,0.33332,0.33333,0.74,0.999,1.0};
  int exp[]={0,0,0,1,1,2,2,3,3};
  int np=sizeof(p)/sizeof(double);
  int ntv=sizeof(tv)/sizeof(double);
  int idxv[ntv];
  int i;

  dp_all_indexes(p,np,tv,ntv,idxv);
  for ( i=0; i<ntv; ++i )
  {
    CU_ASSERT_EQUAL(idxv[i],exp[i]);
  }
}
示例#5
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" );
}
示例#6
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] );
  }
}