k-dot-p 0.1

kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d Class Reference

#include <continuous_alloy_optimizer_1d.h++>

Collaboration diagram for kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d:

List of all members.

Public Types

enum  goal { MAXIMIZE, MAXIMIZE_ABS, MINIMIZE, MAKE_ZERO }
typedef void(* set_point_callback )(unsigned X, const kdotp::libmodelxx::structure::material *mat, void *privdat)

Public Member Functions

 continuous_alloy_optimizer_1d (kdotp::libmodelxx::structure::material *marker_material, const char *_mat1, const char *_mat0, kdotp::libmodelxx::structure::generic_structure *s, set_point_callback _set_point, unsigned _blocksize, enum goal _goal, GError **err, void *privdat, struct::kdotp::libmodelxx::structure::matdb *_mdb, double _minx=0.0, double _maxx=1.0, long unsigned int rng_seed=0, double starting_stepsize=0.1, unsigned _N_blocks=0, GList *assignments=NULL, struct::kdotp::libmodelxx::postprocessing::function_exp *assignment_expr=NULL, GHashTable *global_symbols=NULL)
void reset_optimum_array ()
void randomize_array (double *array, unsigned int N)
bool is_improved (double value)
unsigned get_random_unoptimized_block (double *sign)
bool step_block (unsigned block, void *privdat)
double get_x (unsigned block)
void set_block (unsigned block, void *privdat, double x)
void set_block (unsigned block, void *privdat)
void set_block_as_optimum (unsigned block)
void set_material_from_current_assignments (void *privdat)
int do_first_step (double value, void *privdat)
int do_second_step (double value, void *privdat)
int do_subsequent_step (double value, void *privdat)
int do_generic_step (double value, void *privdat)
GError * do_optimization (double(*get_value)(void *privdat, unsigned assignment, long unsigned iteration), void *get_value_privdat, void *set_point_privdat, double step_fraction=0.33, double min_stepsize=0.001)
void initialize (void *privdat)
void initialize (const short unsigned *state, void *privdat)
 ~continuous_alloy_optimizer_1d ()

Public Attributes

char * mat1
char * mat0
double * current_assignments
short unsigned * material_is_optimum
kdotp::libmodelxx::structure::generic_structuregenstruct
set_point_callback call_set_point_callback
unsigned start_X
unsigned stop_X
unsigned blocksize
unsigned * points_in_block
unsigned * point_start
unsigned N_blocks
unsigned N_optimum
double previous_value
unsigned * current_block
enum goal goal
gsl_rng * rng
unsigned long int seed
double stepsize
double prev_x
struct
kdotp::libmodelxx::structure::matdb
mdb
double minx
double maxx

Private Member Functions

 continuous_alloy_optimizer_1d ()
bool operator== (const continuous_alloy_optimizer_1d &o)
continuous_alloy_optimizer_1d operator= (const continuous_alloy_optimizer_1d &o)
 continuous_alloy_optimizer_1d (const continuous_alloy_optimizer_1d &o)

Detailed Description

Definition at line 43 of file continuous_alloy_optimizer_1d.h++.


Member Typedef Documentation


Member Enumeration Documentation

Enumerator:
MAXIMIZE 
MAXIMIZE_ABS 
MINIMIZE 
MAKE_ZERO 

Definition at line 46 of file continuous_alloy_optimizer_1d.h++.


Constructor & Destructor Documentation

kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::continuous_alloy_optimizer_1d ( kdotp::libmodelxx::structure::material marker_material,
const char *  _mat1,
const char *  _mat0,
kdotp::libmodelxx::structure::generic_structure s,
set_point_callback  _set_point,
unsigned  _blocksize,
enum goal  _goal,
GError **  err,
void *  privdat,
struct::kdotp::libmodelxx::structure::matdb *  _mdb,
double  _minx = 0.0,
double  _maxx = 1.0,
long unsigned int  rng_seed = 0,
double  starting_stepsize = 0.1,
unsigned  _N_blocks = 0,
GList *  assignments = NULL,
struct::kdotp::libmodelxx::postprocessing::function_exp *  assignment_expr = NULL,
GHashTable *  global_symbols = NULL 
)

constructor and initializer in one

Parameters:
marker_materialmaterial used to mark the region to be optimized
amaterial to be used if the block is zero
bmaterial to be used if the block is one
sgeneric_structure to be optimized
set_pointcallback to use when setting a point, or NULL if you don't want to do anything
blocksizenumber of points per block (ignored if _N_blocks > 0)
errGError information if something failed. Untouched if successful.
minxminimum x to be allowed
maxxmaximum x to be allowed
_N_blocksNumber of blocks to allocate, or <=0 to use the specified blocksize.
assignementslist of initial material assignments for each block (or for all blocks)
assignement_exprfunction to evaluate to get the initial material assignments.
Note:
Starting and stopping points are fetched from the generic structure.
that the assignments are done on the block level, i.e. in blocks of points. This is to improve efficiency and accuracy, since structures can only be grown so finely, but having a grid spacing which is a multiple of the minimum feature size is desirable for high-accuracy calculations.
that any incommensurate number of points will be split amongst the points as evenly as possible.
that this modifies the generic_structure, removing the marker material and replacing it with materials from the optimizer state.
that this constructor calls _set_point, since it has to initialize the material.
kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::~continuous_alloy_optimizer_1d ( )

Definition at line 201 of file continuous_alloy_optimizer_1d.c++.

                                                                    {
  if(mat1 != NULL) g_free(mat1);
  if(mat0 != NULL) g_free(mat0);
  if(current_assignments != NULL) delete[] current_assignments;
  if(material_is_optimum != NULL) delete[] material_is_optimum;
  if(points_in_block != NULL) delete[] points_in_block;
  if(current_block != NULL) delete current_block;
      }
kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::continuous_alloy_optimizer_1d ( ) [inline, private]

Definition at line 321 of file continuous_alloy_optimizer_1d.h++.

{}
kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::continuous_alloy_optimizer_1d ( const continuous_alloy_optimizer_1d o) [inline, private]

Definition at line 324 of file continuous_alloy_optimizer_1d.h++.

{}

Member Function Documentation

int kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::do_first_step ( double  value,
void *  privdat 
)

Does the first step in the algorithm.

Parameters:
valuethe value that we're tracking
privdatpointer to data for the set_point callback.
Returns:
0 if we've not converged, or 1 if we've converged.

Definition at line 215 of file continuous_alloy_optimizer_1d.c++.

                                                                                  {
  /*There's no way we can yet be converged.  We don't even know if this
   *  point is optimal.  This is our first point.*/
  previous_value=value;
  current_block = new unsigned;
  double sign;
  *current_block = get_random_unoptimized_block(&sign);
  /*Save this block's current composition.*/
  prev_x = get_x(*current_block);
  step_block(*current_block, privdat);
  return 0;
      }
int kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::do_generic_step ( double  value,
void *  privdat 
)

Does any step in the algorithm, but checks to see if we've taken the first step yet.

Parameters:
valuethe value that we're tracking
privdatpointer to data for the set_point callback.
Returns:
0 if we've not converged, or 1 if we've converged.

Does any step in the algorithm, but checks to see if we've taken the first step yet

Parameters:
valuethe value that we're tracking
privdatpointer to data for the set_point callback.
Returns:
0 if we've not converged, or 1 if we've converged.

Definition at line 297 of file continuous_alloy_optimizer_1d.c++.

                                                                                    {
  if(current_block == NULL) {
    return do_first_step(value, privdat);
  }
  return do_subsequent_step(value, privdat);
      }
GError * kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::do_optimization ( double(*)(void *privdat, unsigned assignment, long unsigned iteration)  get_value,
void *  get_value_privdat,
void *  set_point_privdat,
double  step_fraction = 0.33,
double  min_stepsize = 0.001 
)

Definition at line 314 of file continuous_alloy_optimizer_1d.c++.

References CONTINUOUS_ALLOY_OPTIMIZER_1D_GERROR_DOMAIN, and unlikely.

Referenced by main().

                                                                                                                                                                                                                                          {
  long unsigned iteration=0;
  do {
    double value = get_value(get_value_privdat, 0, iteration++);
    if(unlikely(isnan(value))) {
      return g_error_new(CONTINUOUS_ALLOY_OPTIMIZER_1D_GERROR_DOMAIN, 101, "kdotp::libkdotp::optimizer::continuous_alloy_optimizer_1d.c::do_optimization: Got NaN (%g) from get_value callback in the %lu%s iteration.  Iteration has been halted.\n", value, iteration, (iteration%10==1&&iteration!=11)?"st":(iteration%10==2&&iteration!=12)?"nd":(iteration%10==3&&iteration!=13)?"rd":"th");
    }else if(unlikely(isinf(value))) {
      return g_error_new(CONTINUOUS_ALLOY_OPTIMIZER_1D_GERROR_DOMAIN, 101, "kdotp::libkdotp::optimizer::continuous_alloy_optimizer_1d.c::do_optimization: Got inf (%g) from get_value callback in the %lu%s iteration.  Iteration has been halted.\n", value, iteration, (iteration%10==1&&iteration!=11)?"st":(iteration%10==2&&iteration!=12)?"nd":(iteration%10==3&&iteration!=13)?"rd":"th");
    }
    do_first_step(value, set_point_privdat);
    do {
      value = get_value(get_value_privdat, 0, iteration++);
      if(unlikely(isnan(value))) {
        return g_error_new(CONTINUOUS_ALLOY_OPTIMIZER_1D_GERROR_DOMAIN, 101, "kdotp::libkdotp::optimizer::continuous_alloy_optimizer_1d.c::do_optimization: Got NaN (%g) from get_value callback in the %lu%s iteration.  Iteration has been halted.\n", value, iteration, (iteration%10==1&&iteration!=11)?"st":(iteration%10==2&&iteration!=12)?"nd":(iteration%10==3&&iteration!=13)?"rd":"th");
      }else if(unlikely(isinf(value))) {
        return g_error_new(CONTINUOUS_ALLOY_OPTIMIZER_1D_GERROR_DOMAIN, 101, "kdotp::libkdotp::optimizer::continuous_alloy_optimizer_1d.c::do_optimization: Got inf (%g) from get_value callback in the %lu%s iteration.  Iteration has been halted.\n", value, iteration, (iteration%10==1&&iteration!=11)?"st":(iteration%10==2&&iteration!=12)?"nd":(iteration%10==3&&iteration!=13)?"rd":"th");
      }
    }while(!(do_subsequent_step(value, set_point_privdat)));
    /*We can't optimize anymore at this step size.*/
    stepsize *= step_fraction;
    /*Free the current_block so it'll be reallocated after first iteration
     *Then reset our optimized steps..
     */
    delete current_block;
    reset_optimum_array();
  }while(stepsize >= min_stepsize);
  return NULL;
      }

Here is the caller graph for this function:

int kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::do_second_step ( double  value,
void *  privdat 
)

Does the later steps in the algorithm

Parameters:
valuethe value that we're tracking
privdatpointer to data for the set_point callback.
Returns:
0 if we've not converged, or 1 if we've converged.
int kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::do_subsequent_step ( double  value,
void *  privdat 
)

Does the later steps in the algorithm

Parameters:
valuethe value that we're tracking
privdatpointer to data for the set_point callback.
Returns:
0 if we've not converged, or 1 if we've converged.

We know that the block that just got tested was already optimal.

Note:
that we don't know whether the other direction (+ or -) is optimized!

Definition at line 232 of file continuous_alloy_optimizer_1d.c++.

                                                                                       {
  if(is_improved(value)) {
    /*This is an improved point!*/
    /*We don't know right now if any other points are optimized.*/
    reset_optimum_array();
    /*This direction (+ or -) succeeded, so the *other* direction will fail and therefore is
     *  already optimized.
     *So set that the *other* direction is optimized.
     */
    set_block_as_optimum((*current_block + N_blocks)%(2*N_blocks));
    previous_value = value;
    fprintf(stderr, "RESET ARRAY: previous_value=%g (value=%g)\n", previous_value, value);
  }else{
    /*Choosing this point worsened the structure.  Go back, set this as optimized, and choose a new point.*/
    /*Algorithm to get sign without a branch:
     *1) Get the real block
     *2) Get the offset between the signed and real block.  This will either be 0 or N_blocks
     *3) Divide by N_blocks/2 (maps to range [0, 2])
     *4) Subtract 1 (maps to range [-1, 1])
     *\note that we need -1 times this, to undo the previous step
     *\note that we now reset the block to its previous x instead of adding and then subtracting
     */
    set_block(*current_block, privdat, prev_x);
    fprintf(stderr, "REVERT BLOCK %u: previous_value=%g (value=%g)\n", *current_block, previous_value, value);
    /**We know that the block that just got tested was already optimal.
     *\note that we don't know whether the other direction (+ or -) is optimized!  */
    set_block_as_optimum(*current_block);
    /*Note that we don't set the previous value to this current value;
     *it's not an improvement!
     */
  }
  /*Check to see if they're all optimized.*/
  if(N_optimum == 2*N_blocks) {
    fprintf(stderr, "ALPHA IS LOCAL OPTIMUM: N_optimum=%u 2*N_blocks=%u\n", N_optimum, 2*N_blocks);
    return 1;
  }else{
    fprintf(stderr, "ALPHA IS (NOT) LOCAL OPTIMUM: N_optimum=%u 2*N_blocks=%u\n", N_optimum, 2*N_blocks);
  }
  /*Get the randomly-selected new block.*/
  double sign;
  bool success;
  do {
    *current_block = get_random_unoptimized_block(&sign);
    prev_x = get_x(*current_block);
    /*Set the block.*/
    success = step_block(*current_block, privdat);
    if(!success) {
      /*If we failed, this block is optimum!*/
      set_block_as_optimum(*current_block);
      /*We may have finished optimizing!*/
      if(N_optimum == 2*N_blocks) {
        fprintf(stderr, "stepping block %u failed, and ALPHA IS LOCAL OPTIMUM: N_optimum=%u 2*N_blocks=%u\n",  *current_block, N_optimum, 2*N_blocks);
        return 1;
      }else{
        fprintf(stderr, "stepping block %u failed, but ALPHA IS (NOT) LOCAL OPTIMUM: N_optimum=%u 2*N_blocks=%u\n", *current_block, N_optimum, 2*N_blocks);
      }
    }
  }while(!success);
  return 0;
      }
unsigned kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::get_random_unoptimized_block ( double *  sign) [inline]

gets a random unoptimized block.

Parameters:
signpointer to a double to store either + or -1, depending on the block.
Note:
that double pointed to by sign will be either +1 or -1; mul by stepsize to get the amount.
Returns:
the block which is returned is [0, 2*N_blocks-1]

Note:
we first walk through the first half, then the second.

we're now in the negative blocks.

Definition at line 193 of file continuous_alloy_optimizer_1d.h++.

References material_is_optimum, N_blocks, N_optimum, and rng.

                                                             {
    unsigned check = (unsigned)floor(gsl_rng_uniform(rng)*(2*N_blocks - N_optimum-1));
    unsigned j=0;
    /*we need to find the check-th un-optimized block.
     *algorithm: walk through the list incrementing j when we have an unoptimized block
     *  return the block where j becomes check.
     */
    /**\note we first walk through the first half, then the second.*/
    *sign = 1.0;
    for(unsigned block=0; block<N_blocks; block++) {
      if(!material_is_optimum[block]) {
        if(j++ == check) {
    return block;
        }
      }
    }
    /**we're now in the negative blocks.*/
    *sign = -1.0;
    for(unsigned block=0; block<N_blocks; block++) {
      if(!material_is_optimum[block]) {
        if(j++ == check) {
    return N_blocks+block;
        }
      }
    }
    return 2*N_blocks;
  }
double kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::get_x ( unsigned  block) [inline]

Gets the x value of a block

Parameters:
signedblock

Definition at line 239 of file continuous_alloy_optimizer_1d.h++.

References current_assignments, and N_blocks.

                                      {
    return current_assignments[block%N_blocks];
  }
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::initialize ( void *  privdat)

Resets the state to get ready to do an optimization.

Note:
that this randomizes the current assignments array, so this is what you'll want to call to start from another initial condition

Definition at line 181 of file continuous_alloy_optimizer_1d.c++.

void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::initialize ( const short unsigned *  state,
void *  privdat 
)

Initializes to a known state.

Parameters:
statethe state to initialize to (array of N_blocks short unsigned ints)

Definition at line 191 of file continuous_alloy_optimizer_1d.c++.

                                                                                               {
  if(current_block != NULL) {
    delete current_block;
    current_block = NULL;
  }
  reset_optimum_array();
  for(unsigned i=0; i<N_blocks; i++) current_assignments[i] = state[i];
  set_material_from_current_assignments(privdat);
      }
bool kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::is_improved ( double  value) [inline]

Definition at line 170 of file continuous_alloy_optimizer_1d.h++.

References MAKE_ZERO, MAXIMIZE, MAXIMIZE_ABS, MINIMIZE, and previous_value.

                                        {
    switch(goal) {
    case MAXIMIZE:
      return (value > previous_value);
      break;
    case MAXIMIZE_ABS:
      fprintf(stderr, "maximize_abs: value=%g previous_value=%g\n", value, previous_value);
      return (fabs(value) > fabs(previous_value));
      break;
    case MINIMIZE:
      return (value < previous_value);
      break;
    case MAKE_ZERO:
      return (fabs(value) < fabs(previous_value));
      break;
    }
    return false;
  }
continuous_alloy_optimizer_1d kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::operator= ( const continuous_alloy_optimizer_1d o) [inline, private]

Definition at line 323 of file continuous_alloy_optimizer_1d.h++.

{return *this;}
bool kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::operator== ( const continuous_alloy_optimizer_1d o) [inline, private]

Definition at line 322 of file continuous_alloy_optimizer_1d.h++.

{return false;}
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::randomize_array ( double *  array,
unsigned int  N 
) [inline]

Note:
that gsl_rng_uniform returns [0,1) but that's good enough.

Definition at line 163 of file continuous_alloy_optimizer_1d.h++.

References maxx, minx, and rng.

                                                             {
    for(unsigned i=0; i<N; i++) {
      /**\note that gsl_rng_uniform returns [0,1) but that's good enough.
       */
      array[i]=minx+gsl_rng_uniform(rng)*(maxx-minx);
    }
  }
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::reset_optimum_array ( ) [inline]

Definition at line 159 of file continuous_alloy_optimizer_1d.h++.

References material_is_optimum, N_blocks, and N_optimum.

                                    {
    for(unsigned i=0; i<2*N_blocks; i++) material_is_optimum[i]=0;
    N_optimum=0;
  }
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::set_block ( unsigned  block,
void *  privdat 
) [inline]
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::set_block ( unsigned  block,
void *  privdat,
double  x 
) [inline]

Sets a block to the specified x value

Parameters:
signedblock

Definition at line 245 of file continuous_alloy_optimizer_1d.h++.

References current_assignments, and N_blocks.

Referenced by set_material_from_current_assignments(), and step_block().

                                                                 {
    current_assignments[block%N_blocks] = x;
    set_block(block%N_blocks, privdat);
  }

Here is the caller graph for this function:

void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::set_block_as_optimum ( unsigned  block) [inline]

Definition at line 265 of file continuous_alloy_optimizer_1d.h++.

References material_is_optimum, and N_optimum.

                                                   {
    material_is_optimum[block]=1;
    N_optimum++;
  }
void kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::set_material_from_current_assignments ( void *  privdat) [inline]

Definition at line 270 of file continuous_alloy_optimizer_1d.h++.

References N_blocks, and set_block().

                                                                   {
    for(unsigned block=0; block<N_blocks; block++) {
      set_block(block, privdat);
    }
  }

Here is the call graph for this function:

bool kdotp::libmetacalc::optimizer::continuous_alloy_optimizer_1d::step_block ( unsigned  block,
void *  privdat 
) [inline]

Steps a block by the specified step

Parameters:
signedblock
privdatprivate data for the set_point callback.
Returns:
true if stepping succeeded; false if stepping failed.

Definition at line 225 of file continuous_alloy_optimizer_1d.h++.

References current_assignments, maxx, minx, N_blocks, set_block(), and stepsize.

                                                        {
    double sign = -1;
    if(block < N_blocks) sign=1;
    if(current_assignments[block%N_blocks]+sign*stepsize > maxx) return false;
    if(current_assignments[block%N_blocks]+sign*stepsize < minx) return false;
    fprintf(stderr, "STEPPING: step_block: sign=%g stepsize=%g block[%u->%u]=%g->", sign, stepsize, block, block%N_blocks, current_assignments[block%N_blocks]);
    current_assignments[block%N_blocks] += sign*stepsize;
    fprintf(stderr, "%g\n", current_assignments[block%N_blocks]);
    set_block(block%N_blocks, privdat);
    return true;
  }

Here is the call graph for this function:


Member Data Documentation

Which block was last changed.

Note:
this is a *pointer*; if it's NULL, the previous calculation was the first one, so all of the blocks had been changed.
this is the block which was just tested
that this is in the range [0, 2*N_blocks-1] to preserve the sign information.

Definition at line 144 of file continuous_alloy_optimizer_1d.h++.

This could be gleaned by counting material_is_optimum, but is faster.

Definition at line 134 of file continuous_alloy_optimizer_1d.h++.

Referenced by get_random_unoptimized_block(), reset_optimum_array(), and set_block_as_optimum().

previous block x, so we can reset the block to its previous state

Definition at line 152 of file continuous_alloy_optimizer_1d.h++.

Previous value we're to track, to compare how we're changing.

Note:
this is a *pointer*; if it's NULL, the previous calculation was the first one, so all of the blocks had been changed.

Definition at line 138 of file continuous_alloy_optimizer_1d.h++.

Referenced by is_improved().

The GSL random number generator information.

Definition at line 147 of file continuous_alloy_optimizer_1d.h++.

Referenced by get_random_unoptimized_block(), and randomize_array().

This is our current stepsize.

Definition at line 150 of file continuous_alloy_optimizer_1d.h++.

Referenced by step_block().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines