- Stahuj zápisky z přednášek a ostatní studijní materiály
- Zapisuj si jen kvalitní vyučující (obsáhlá databáze referencí)
- Nastav si své předměty a buď stále v obraze
- Zapoj se svojí aktivitou do soutěže o ceny
- Založ si svůj profil, aby tě tví spolužáci mohli najít
- Najdi své přátele podle místa kde bydlíš nebo školy kterou studuješ
- Diskutuj ve skupinách o tématech, které tě zajímají
Studijní materiály
Vše v jednom
KTE/ZPE - Základy programování pro elektrotechniku
Zjednodušená ukázka:
Stáhnout celý tento materiál].east = outcome->west; /* periodic boundary ! */
next_row_out[ 1 ].northwest = outcome->southeast;
next_row_out[ 0 ].northeast = outcome->southwest;
}
/* Now process the inner nodes */
for( col = 1; col < size; col++ ) {
cur_row_in++;
adr = 0;
if( cur_row_in->northeast != 0 ) adr |= 32;
if( cur_row_in->northwest != 0 ) adr |= 16;
if( cur_row_in->east != 0 ) adr |= 8;
if( cur_row_in->west != 0 ) adr |= 4;
if( cur_row_in->southeast != 0 ) adr |= 2;
if( cur_row_in->southwest != 0 ) adr |= 1;
outcome = rules + adr;
#ifdef SCATTER_ALTERNATELY
switch( adr ) {
case 0x21:
outcome = &scatter[ scatter_dir ];
scatter_dir = 1 - scatter_dir;
break;
case 0x0C:
outcome = &scatter[ scatter_dir + 1 ];
scatter_dir = 1 - scatter_dir;
break;
case 0x12:
outcome = &scatter[ scatter_dir + 2 ];
scatter_dir = 1 - scatter_dir;
break;
}
#endif
if( cur_row_index & 1 )
{ /* odd row */
prev_row_out[ col ].southwest = outcome->northeast;
prev_row_out[ col - 1 ].southeast = outcome->northwest;
cur_row_out[ col + 1 ].west = outcome->east;
cur_row_out[ col - 1 ].east = outcome->west;
next_row_out[ col ].northwest = outcome->southeast;
next_row_out[ col - 1 ].northeast = outcome->southwest;
}
else
{ /* even row - offset in slightly */
prev_row_out[ col + 1 ].southwest = outcome->northeast;
prev_row_out[ col ].southeast = outcome->northwest;
cur_row_out[ col + 1 ].west = outcome->east;
cur_row_out[ col - 1 ].east = outcome->west;
next_row_out[ col + 1 ].northwest = outcome->southeast;
next_row_out[ col ].northeast = outcome->southwest;
}
}
/* Special case the last node in a row */
cur_row_in++;
adr = 0;
if( cur_row_in->northeast != 0 ) adr |= 32;
if( cur_row_in->northwest != 0 ) adr |= 16;
if( cur_row_in->east != 0 ) adr |= 8;
if( cur_row_in->west != 0 ) adr |= 4;
if( cur_row_in->southeast != 0 ) adr |= 2;
if( cur_row_in->southwest != 0 ) adr |= 1;
outcome = rules + adr;
if( cur_row_index & 1 )
{ /* odd row */
prev_row_out[ size ].southwest = outcome->northeast;
prev_row_out[ size - 1 ].southeast = outcome->northwest;
cur_row_out[ 0 ].west = outcome->east; /* periodic boundary ! */
cur_row_out[ size - 1 ].east = outcome->west;
next_row_out[ size ].northwest = outcome->southeast;
next_row_out[ size - 1 ].northeast = outcome->southwest;
}
else
{ /* even row - offset in slightly */
prev_row_out[ 0 ].southwest = outcome->northeast; /* boundary */
prev_row_out[ size ].southeast = outcome->northwest;
cur_row_out[ 0 ].west = outcome->east; /* periodic boundary ! */
cur_row_out[ size - 1 ].east = outcome->west;
next_row_out[ 0 ].northwest = outcome->southeast; /* boundary */
next_row_out[ size ].northeast = outcome->southwest;
}
}
void parse_arguments( int argc, char **argv )
{
int index;
if( argc > 1 )
{
index = 1;
while( index < argc )
{
if( argv[ index ][0] == '-' )
{ /* - option, only -size defined right now */
if( index + 1 >= argc )
{
printf( "%s option requires an argument !\n", argv[ index ] );
exit( -1 );
}
array_size = atoi( argv[ ++index ] );
printf( "Simulating a %d x %d array\n", array_size, array_size );
}
else
{ /* must be array seed - block, rand, or test */
switch( argv[ index ][0] ) {
case 'b':
array_seed = SEED_BLOCK;
break;
case 'r':
array_seed = SEED_BLOCK_AND_RND;
break;
case 't':
array_seed = SEED_TEST;
break;
default:
printf( "I don't understand %s as a seed pattern !\n",
argv[ index ] );
exit( -1 );
}
}
index++;
}
}
srand( SEED_RAND_SEED );
}
void seed_lattice( lattice *l, int seed )
{
int row, col;
int lattice_size = l->size;
lattice_node *n;
/* Now seed the lattice structure. It has already
been cleared to all zeroes. */
if( seed == SEED_BLOCK_AND_RND )
for( row = 0; row < lattice_size; row++ ) {
for( n = l->d[ row ], col = 0; col < lattice_size; col++, n++ ) {
if( rand() > SEED_RAND_THRESHOLD) n->northeast = 1;
if((rand() > SEED_RAND_THRESHOLD) n->northwest = 1;
if((rand() > SEED_RAND_THRESHOLD) n->east = 1;
if((rand() > SEED_RAND_THRESHOLD) n->west = 1;
if((rand() > SEED_RAND_THRESHOLD) n->southeast = 1;
if((rand() > SEED_RAND_THRESHOLD) n->southwest = 1;
}
}
if( (seed == SEED_BLOCK) || (seed == SEED_BLOCK_AND_RND) )
for( row = SEED_BLOCK_START;
row < (SEED_BLOCK_START + SEED_BLOCK_SIZE); row++ ) {
n = l->d[ row ] + SEED_BLOCK_START;
for( col = SEED_BLOCK_START;
col < (SEED_BLOCK_START + SEED_BLOCK_SIZE); col++, n++ ) {
n->northeast = 1;
n->northwest = 1;
n->east = 1;
n->west = 1;
n->southeast = 1;
n->southwest = 1;
}
}
if( seed == SEED_TEST ) {
for( n = l->d[ 0 ], col = 0; col < l->size; col++, n++ ) {
n->east = 1;
n->southwest = 1;
}
for( row = 2; row < (array_size - 1); row++ ) {
n = l->d[ row ] + 4 + (row / 2);
n->northeast = 1;
n->west = 1;
}
}
}
/* alloc_lattice
This lattice is stored as an array of lattice_nodes, where each
row is pointed to independently (although in this case the lines
are actually stored consecutively.)
*/
lattice *alloc_lattice( int lattice_size )
{
int row, col;
long total_array_size;
lattice_node *n;
lattice_node *row_start;
lattice *l;
/* malloc the memory */
total_array_size = (lattice_size * lattice_size * sizeof(lattice_node))
+ sizeof( lattice );
if( (l = (lattice *)malloc( total_array_size )) == NULL ) {
printf( "unable to alloc %d bytes of memory for lattice\n",
total_array_size );
exit( -1 );
}
/* Now init the lattice structure. */
l->size = lattice_size;
row_start = (lattice_node *)((long)l + sizeof( lattice ));
for( row = 0; row < lattice_size; row++ ) {
l->d[ row ] = row_start;
for( n = row_start, col = 0; col < lattice_size; col++, n++ ) {
n->northeast = 0;
n->northwest = 0;
n->east = 0;
n->west = 0;
n->southeast = 0;
n->southwest = 0;
}
row_start += lattice_size;
}
return( l );
}
void display_lattice( window_tag wt, lattice *l )
{
int x, y;
int size = l->size;
int num_particles;
lattice_node *n;
for( y = 0; y < size; y++ )
{
n = l->d[ y ];
for( x = 0; x < size; x++, n++ )
{
num_particles = n->northeast;
num_particles += n->northwest;
num_particles += n->east;
num_particles += n->west;
num_particles += n->southeast;
num_particles += n->southwest;
display_set_pixel( wt, x, y, num_particles );
}
}
display_update( wt );
}
/* ga.c
This is search for minimum energy spin glass configurations,
using a genetic algorithm.
The output is solely text, since I wanted to concentrate on the
algorithm. It does, however, print out the lowest energy and the
mean and variance of the energy distribution among the population
for each step along the way.
usage: ga []
J. Watlington, 11/26/95
*/
#include
#include
#include
#define REPORT_PERIOD 0x01 /* num of time steps between prints */
#define POPULATION_SIZE 10
#define SPIN_GLASS_SIZE 100 /* number of spin glass lattice points */
#define EQUAL_PROB 0.5
#define NUM_BUFS 2 /* a double buffer is required */
/* The following variables represent the system being modeled. */
double glass_spin[ NUM_BUFS ][ POPULATION_SIZE ][ SPIN_GLASS_SIZE ];
double glass_J[ SPIN_GLASS_SIZE ]; /* The external stresses */
long rseed = -1; /* random generator state */
/* Quitting conditions */
double epsilon = 1.0;
#define MAX_STALE_GENERATIONS 12
#define MAX_ITERATIONS 4000
/* The following NR routines are linked in. */
extern float gasdev( long *idnum );
extern float ran1( long *idnum );
/* The following routines are declared locally */
int draw_parent( double *repro_prob, int exclude );
void reproduce( int buf, int adam, int eve, int child, double mutation );
double calc_total_energy( double *spin );
void display_glass( int time, int buf, int pop, int show_J );
void main( int argc, char **argv )
{
int i, member;
int adam, eve;
int best_member;
int time = 0;
int cur_gen = 0;
int next_gen = 1;
int no_progress_ctr = 0;
double total_prob;
double total_E, best_E, mean_E, var_E;
double minimum_E = 0.0;
double prev_best_E = 0.0;
double beta = 0.0;
double mutate = 0.0;
double stored_E[ POPULATION_SIZE ]; /* needed for variance calcs */
double repro_prob[ POPULATION_SIZE ]; /* probability of reproducing */
/* Parse the one or two arguments provided by the user. */
if( argc == 3 )
{
sscanf( argv[2], "%lf", &mutate );
}
else if( argc != 2 )
{
printf( "usage: ga \n" );
exit( -1 );
}
sscanf( argv[1], "%lf", &beta );
/* Initialize the spin glass */
for( i = 0; i < SPIN_GLASS_SIZE; i++ )
{
glass_J[ i ] = gasdev( &rseed );
minimum_E -= fabs( glass_J[ i ] );
}
for( member = 0; member < POPULATION_SIZE; member++ )
for( i = 0; i < SPIN_GLASS_SIZE; i++ )
if( ran1( &rseed ) > EQUAL_PROB )
glass_spin[ cur_gen ][ member ][ i ] = 1.0;
else
glass_spin[ cur_gen ][ member ][ i ] = -1.0;
/* Tell the user about the starting conditions */
printf( "Looking for minimum energy %lf (to within %lf)\n",
minimum_E, epsilon );
/* Now start evolving */
do {
/* Generate the probabilities of each population member
reproducing by calculating their energies. */
total_prob = 0.0;
best_E = 1000.0;
mean_E = 0;
best_member = 0;
for( member = 0; member < POPULATION_SIZE; member++ )
{
total_E = calc_total_energy( &glass_spin[cur_gen][member][0] );
repro_prob[ member ] = exp( -beta * ( total_E - minimum_E ) );
total_prob += repro_prob[ member ]; /* acc. the total prob. */
if( total_E < best_E ) /* and find member with least energy */
{
best_E = total_E;
best_member = member;
}
stored_E[ member ] = total_E; /* save for variance calcs */
mean_E += total_E; /* acc the total energy */
}
/* Calculate some statistics about the population energy levels,
since just looking at the raw numbers is useless ! */
mean_E = mean_E / (double)POPULATION_SIZE;
var_E = 0.0;
for( member = 0; member < POPULATION_SIZE; member++ )
{
total_E = stored_E[ member ] - mean_E;
var_E += total_E * total_E;
}
var_E = var_E / (double)POPULATION_SIZE;
/* Now build the probability array used by draw_parent() */
repro_prob[ 0 ] = repro_prob[ 0 ] / total_prob;
for( member = 1; member < POPULATION_SIZE; member++ )
{
repro_prob[ member ] = (repro_prob[ member ] / total_prob)
+ repro_prob[ member-1 ];
}
repro_prob[ POPULATION_SIZE - 1 ] = 1.0;
/* Tell the user about life... */
if( !(time & REPORT_PERIOD) )
printf( "t: %4d best: %3.1lf mean: %3.1lf var: %4.1lf\n", time,
best_E, mean_E, var_E );
/* For each member of the next generation, draw two parents from
the current generation and combine them into the offspring */
for( member = 0; member < POPULATION_SIZE; member++ )
{
adam = draw_parent( repro_prob, -1 );
eve = draw_parent( repro_prob, adam );
reproduce( cur_gen, adam, eve, member, mutate );
}
/* Check for forward progress ! */
if( prev_best_E == best_E )
no_progress_ctr++;
else
{
no_progress_ctr = 0;
prev_best_E = best_E;
}
/* Increment generations */
cur_gen = next_gen;
next_gen = 1 - next_gen;
} while( ((best_E - minimum_E) > epsilon) &&
(no_progress_ctr < MAX_STALE_GENERATIONS) &&
(time++ < MAX_ITERATIONS));
if( no_progress_ctr >= MAX_STALE_GENERATIONS )
printf( "Glass frozen in non-optimal state !\n" );
printf( "Best energy at end: %lf at time %d\n", best_E, time );
display_glass( time, next_gen, best_member, 1 );
}
/* draw_parent
Given a table which contains the cumulative probabilities
summed, (ie. a, a+b, a+b+c, a+b+c+d, ...), this function
randomly selects a parent. The exclude argument allows a
particular parent to be excluded from the drawing.
*/
int draw_parent( double *repro_prob, int exclude )
{
float uniform_dev;
int lower_limit, upper_limit;
int target;
do {
uniform_dev = ran1( &rseed );
lower_limit = 0;
upper_limit = POPULATION_SIZE - 1;
while( lower_limit != upper_limit )
{
target = lower_limit + ((upper_limit - lower_limit) >> 1);
if( uniform_dev > repro_prob[ target ] )
lower_limit = target + 1;
else
upper_limit = target;
}
} while( lower_limit == exclude );
return( lower_limit );
}
/* reproduce
This function takes two parents and creates a child from
their genetic information by randomly selecting a switchover
point, and copying information up to the switchover from
one parent and after the switchover from the other.
The mutate argument allows a mutation rate for the copying to
be specified.
*/
void reproduce( int buf, int adam, int eve, int child, double mutate )
{
int switchpoint;
int x;
double *parent;
double *baby;
parent = &glass_spin[ buf ][ adam ][ 0 ];
baby = &glass_spin[ 1 - buf ][ child ][ 0 ];
switchpoint = (int)( ran1( &rseed ) * (float)(SPIN_GLASS_SIZE - 1) );
for( x = 0; x < switchpoint; x++ )
*baby++ = *parent++;
parent = &glass_spin[ buf ][ eve ][ switchpoint ];
for( x = switchpoint; x < SPIN_GLASS_SIZE; x++ )
*baby++ = *parent++;
if( mutate > 0.0 )
{
baby = &glass_spin[ 1 - buf ][ child ][ 0 ];
for( x = 0; x < SPIN_GLASS_SIZE; x++ )
if( ran1( &rseed ) size - 1;
int row;
calc_collision_row( in, out, size, 0, 1 );
for( row = 1; row < size; row++ )
calc_collision_row( in, out, row - 1, row, row + 1 );
calc_collision_row( in, out, size - 1, size, 0 );
}
void calc_collision_row( lattice *in, lattice *out,
int prev_row_index, int cur_row_index,
int next_row_index )
{
int adr;
int col;
int size = in->size - 1;
lattice_node *outcome;
lattice_node *cur_row_in = in->d[ cur_row_index ];
lattice_node *prev_row_out = out->d[ prev_row_index ];
lattice_node *cur_row_out = out->d[ cur_row_index ];
lattice_node *next_row_out = out->d[ next_row_index ];
/* Special case the first node in a row */
adr = 0;
if( cur_row_in->north != 0 ) adr |= 8;
if( cur_row_in->east != 0 ) adr |= 4;
if( cur_row_in->west != 0 ) adr |= 2;
if( cur_row_in->south != 0 ) adr |= 1;
outcome = rules + adr;
prev_row_out[ 0 ].south = outcome->north;
cur_row_out[ 1 ].west = outcome->east;
cur_row_out[ size ].east = outcome->west; /* periodic boundary ! */
next_row_out[ 0 ].north = outcome->south;
/* Now process the inner nodes */
for( col = 1; col < size; col++ ) {
cur_row_in++;
adr = 0;
if( cur_row_in->north != 0 ) adr |= 8;
if( cur_row_in->east != 0 ) adr |= 4;
if( cur_row_in->west != 0 ) adr |= 2;
if( cur_row_in->south != 0 ) adr |= 1;
outcome = rules + adr;
prev_row_out[ col ].south = outcome->north;
cur_row_out[ col + 1 ].west = outcome->east;
cur_row_out[ col - 1 ].east = outcome->west;
next_row_out[ col ].north = outcome->south;
}
/* Special case the last node in a row */
cur_row_in++;
adr = 0;
if( cur_row_in->north != 0 ) adr |= 8;
if( cur_row_in->east != 0 ) adr |= 4;
if( cur_row_in->west != 0 ) adr |= 2;
if( cur_row_in->south != 0 ) adr |= 1;
outcome = rules + adr;
prev_row_out[ size ].south = outcome->north;
cur_row_out[ 0 ].west = outcome->east; /* periodic boundary ! */
cur_row_out[ size - 1 ].east = outcome->west;
next_row_out[ size ].north = outcome->south;
}
void parse_arguments( int argc, char **argv )
{
int index;
if( argc > 1 )
{
index = 1;
while( index < argc )
{
if( argv[ index ][0] == '-' )
{ /* - option, only -size defined right now */
if( index + 1 >= argc )
{
printf( "%s option requires an argument !\n", argv[ index ] );
exit( -1 );
}
array_size = atoi( argv[ ++index ] );
printf( "Simulating a %d x %d array\n", array_size, array_size );
}
else
{ /* must be array seed - block, rand, or test */
switch( argv[ index ][0] ) {
case 'b':
array_seed = SEED_BLOCK;
break;
case 'r':
array_seed = SEED_BLOCK_AND_RND;
break;
case 't':
array_seed = SEED_TEST;
break;
default:
printf( "I don't understand %s as a seed pattern !\n",
argv[ index ] );
exit( -1 );
}
}
index++;
}
}
srand( SEED_RAND_SEED );
}
void seed_lattice( lattice *l, int seed )
{
int row, col;
int lattice_size = l->size;
lattice_node *n;
/* Now seed the lattice structure. It has already
been cleared to all zeroes. */
if( seed == SEED_BLOCK_AND_RND )
for( row = 0; row < lattice_size; row++ ) {
for( n = l->d[ row ], col = 0; col < lattice_size; col++, n++ ) {
if((rand() >> SEED_RAND_SCALE_FACTOR) > SEED_RAND_THRESHOLD) n->north = 1;
if((rand() >> SEED_RAND_SCALE_FACTOR) > SEED_RAND_THRESHOLD) n->south = 1;
if((rand() >> SEED_RAND_SCALE_FACTOR) > SEED_RAND_THRESHOLD) n->east = 1;
if((rand() >> SEED_RAND_SCALE_FACTOR) > SEED_RAND_THRESHOLD) n->west = 1;
}
}
if( (seed == SEED_BLOCK) || (seed == SEED_BLOCK_AND_RND) )
for( row = SEED_BLOCK_START;
row < (SEED_BLOCK_START + SEED_BLOCK_SIZE); row++ ) {
n = l->d[ row ] + SEED_BLOCK_START;
for( col = SEED_BLOCK_START;
col < (SEED_BLOCK_START + SEED_BLOCK_SIZE); col++, n++ ) {
n->north = 1;
n->east = 1;
n->we
Vloženo: 3.07.2009
Velikost: 1,95 MB
Komentáře
Tento materiál neobsahuje žádné komentáře.
Mohlo by tě zajímat:
Reference vyučujících předmětu KTE/ZPE - Základy programování pro elektrotechnikuPodobné materiály
Copyright 2023 unium.cz. Abychom mohli web rozvíjet a dále vylepšovat podle preferencí uživatelů, shromažďujeme statistiky o návštěvnosti, a to pomocí Google Analytics a Netmonitor. Tyto systémy pro unium.cz zaznamenávají, které stránky uživatel na webové stránce navštívil, odkud se na stránku dostal, kam z ní odešel, jaké používá zařízení, operační systém či prohlížeč, či jaký má preferenční jazyk. Statistiky jsou anonymní, takže unium.cz nezná identitu návštěvníka a spravuje cookies tak, že neumožňuje identifikovat konkrétní osoby. Používáním webu vyjadřujete souhlas použitím cookies a následujících služeb: