runnable version, xSquared kicks everything out of bounds in directPredecessor

This commit is contained in:
Friese 2018-05-03 18:30:42 +02:00
parent 1a9b1191b9
commit d71470afc1
1 changed files with 142 additions and 102 deletions

View File

@ -11,95 +11,106 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <float.h> // DBL_MAX
#define M 1000
#define tracking 40 //Count of weights
#define learnrate 1.0
#define WGHTS 1
#define WGHTSFTR 2
#define RES 3
#define DRCTPRD 4
#define PURE_WEIGHTS 0
#define USED_WEIGHTS 1
#define RESULTS 2
#define DIRECT_PREDECESSOR 3
double x[] = {0};
double _x[M] = {0};
double w [M][M]={{0},{0}};
const char *fileName(int id);
/* *file handling* */
char * mkFileName( char* buffer, size_t max_len, int suffixId );
char *fileSuffix( int id );
void myLogger( FILE* fp, int myVar);
/* *rand seed functions* */
double r2( void );
double rnd(void);
double rndm( void );
/* *math functions * */
double sum_array( double x[], int length );
void direkterVorgaenger(void);
void lokalerMittelWert(void);
void directPredecessor( void );
void localMean( void );
int main(int argc, char **argv ) {
//init test_array, fill in weights by random
char fileName[50];
int i;
// srand(NULL);
srand( (unsigned int) time(NULL) );
for (i = 0; i < M; i++) {
_x[i] += ((255.0 / M) * i);
for (int k = 1; k < M; k++)
{
w[k][i] = rnd();
_x[i] += ((255.0 / M) * i); // Init test values
for (int k = 1; k < M; k++){
w[k][i] = rndm(); // Init weights
}
}
// save plain test_array before math magic happened
FILE *fp0 = fopen(fileName(WGHTS),"wb+");
mkFileName( fileName, sizeof(fileName), PURE_WEIGHTS);
// save plain test_array before math magic happens
FILE *fp0 = fopen(fileName,"w");
for (i = 0; i < tracking; i++){
for ( int k = 1; k < tracking; k++ ){
fprintf(fp0, "[%f][%f] %.2f\n", k, i, w[k][i]);
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
}
}
fclose(fp0);
// math magic
direkterVorgaenger();
//directPredecessor(); // TODO: needs some love!
localMean();
// save test_array after math magic happened
FILE *fp1 = fopen(fileName(WGHTSFTR),"wb+");
// memset( fileName, '\0', sizeof(fileName) );
mkFileName( fileName, sizeof(fileName), USED_WEIGHTS);
FILE *fp1 = fopen(fileName,"w");
for (i = 0; i < tracking; i++) {
for (int k = 1; k < tracking; k++) {
fprintf(fp1, "[%f][%f] %.2f\n", k,i, w[k][i]);
fprintf(fp1, "[%d][%d] %lf\n", k,i, w[k][i]);
}
}
fclose(fp1);
getchar();
// getchar();
printf("DONE!");
}
/*
=======================================================================================
===================================
lokalerMittelwert()
localMean
Variant (1/3),
substract local mean
Variant (1/3), substract local mean.
===================================
=======================================================================================
*/
void lokalerMittelWert() {
void localMean( void ) {
char fileName[50];
double xError[M]; // includes e(n)
memset(xError, 0, M);// initialize xError-array with Zero
int xCount = 0; // runtime var
int i;
for (xCount = 1; xCount < M; xCount++){ // x_cout can not be zero
for (xCount = 1; xCount < M; xCount++){
//double xPartArray[xCount]; //includes all values at the size of runtime var
double xMean = (xCount > 0) ? ( sum_array(_x, xCount) / xCount) : 0;
double xMean = ( xCount > 0 ) ? ( sum_array(_x, xCount) / xCount ) : 0;// xCount can not be zero
double xPredicted = 0.0;
double xActual = _x[xCount + 1];
@ -134,7 +145,8 @@ void lokalerMittelWert() {
// write in file
FILE *fp2 = fopen(fileName(RES), "wb+");
mkFileName( fileName, sizeof(fileName), RESULTS );
FILE *fp2 = fopen(fileName, "w+");
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
fclose(fp2);
@ -142,10 +154,9 @@ void lokalerMittelWert() {
/*
===================================
direkterVorgaenger()
directPredecessor
Variant (2/3),
@ -154,18 +165,19 @@ void lokalerMittelWert() {
===================================
*/
void direkterVorgaenger() {
void directPredecessor( void ) {
char fileName[50];
double xError [M];
int xCount = 0, i;
double xActual;
// File handling
FILE *fp3 = fopen(fileName(DRCTPRD), "wb+");
mkFileName( fileName, sizeof(fileName), DIRECT_PREDECESSOR);
FILE *fp3 = fopen(fileName, "w");
for ( xCount = 1; xCount < M; xCount++ ){
for ( xCount = 1; xCount < M+1; xCount++ ){
xActual = _x[xCount+1];
double xPredicted = 0.0;
double xActual = _x[xCount+1];
for ( i = 1; i < xCount; i++ ){
xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1] ));
}
@ -175,15 +187,14 @@ void direkterVorgaenger() {
fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
double xSquared = 0.0;
//get x squared
double xSquared = 0;
for ( i = 1; i < xCount; i++ ){
xSquared += pow( _x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor
}
for ( i = 1; i < xCount; i++){
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i - 1] ) / xSquared );
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i - 1] ) / xSquared ); //TODO: double val out of bounds
}
}
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
@ -200,50 +211,83 @@ void direkterVorgaenger() {
}
/*
=========================================================================
===================================
fileName()
mkFileName
generates filename with date for
logging purposes
Writes the current date plus the suffix with index suffixId
into the given buffer. If[M ?K the total length is longer than max_len,
only max_len characters will be written.
===================================
=========================================================================
*/
const char *fileName(int id){
char *mkFileName( char* buffer, size_t max_len, int suffixId) {
const char * format_str = "%Y-%m-%d_%H_%M_%S";
size_t date_len;
const char * suffix = fileSuffix( suffixId );
time_t now = time(NULL);
static char* suffix[] = {"_weights.txt","_weights_after.txt", "_results.txt", "direct_predecessor.txt"};
char *date;
time_t now;
now = time(NULL);
strftime(date, 20, "%Y-%m-%d_%H_%M_%S", localtime(&now));
strcat(suffix[id], date);
strftime( buffer, max_len, format_str, localtime(&now) );
date_len = strlen(buffer);
strncat( buffer, suffix, max_len - date_len );
}
/*
=========================================================================
fileSuffix
Contains and returns every suffix for all existing filenames
==========================================================================
*/
char * fileSuffix( int id ) {
char * suffix[] = {"_weights_pure.txt", "_weights_used.txt", "direct_predecessor.txt", "ergebnisse.txt"};
return suffix[id];
}
/*
===================================
/*
==========================================================================
myLogger
Pipes to stdout or files by using a filepointer for logging purposes
==========================================================================
*/
void myLogger ( FILE* fp, int myVar ){
fprintf( fp, "Logging: %d\n", myVar);
}
/*
=========================================================================
sum_array
sum of all elements in x
within a defined length
===================================
Sum of all elements in x within a defined length
=========================================================================
*/
double sum_array( double x[], int length ){
//int length = 0;
int i = 0;
double sum = 0.0;
//length = sizeof(x)/sizeof(x[0]);
for( i=0; i< length; i++ ) {
sum += x[i];
}
@ -252,36 +296,32 @@ double sum_array(double x[], int length){
/*
==========================================================================
===================================
r2
r2()
returns a double value between
0 and 1
===================================
returns a random double value between 0 and 1
==========================================================================
*/
double r2()
{
double r2( void ) {
return((rand() % 10000) / 10000.0);
}
/*
==========================================================================
===================================
rndm
int rnd()
fills a double variable with
random value and returns it
===================================
fills a double variable with random value and returns it
=========================================================================
*/
double rnd()
{
double rndm( void ) {
double rndmval= r2();
return rndmval;
}