added svg grapher for output

This commit is contained in:
Friese 2018-05-04 16:30:12 +02:00
parent 9b2999f4e7
commit 6072f3459c
1 changed files with 149 additions and 50 deletions

View File

@ -13,23 +13,34 @@
#include <string.h>
#include <float.h> // DBL_MAX
#define M 1000
#define M 100
#define tracking 40 //Count of weights
#define learnrate 1.0
#define PURE_WEIGHTS 0
#define USED_WEIGHTS 1
#define RESULTS 2
#define DIRECT_PREDECESSOR 3
#define GRAPH 4
#define RESULTS 3
#define DIRECT_PREDECESSOR 2
#define LOCAL_MEAN 4
double x[] = {0};
double _x[M] = {0};
double w [M][M]={{0},{0}};
/* graph building */
typedef struct {
double xVal[7];
double yVal[7];
}point_t;
point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean
/* *file handling* */
char * mkFileName( char* buffer, size_t max_len, int suffixId );
char *fileSuffix( int id );
void myLogger( FILE* fp, int myVar);
void myLogger( FILE* fp, point_t points[]);
size_t getline( char **lineptr, size_t *n, FILE *stream ); //redundant under POSIX supporting OS
void mkSvgGraph( point_t points[]);
/* *rand seed* */
double r2( void );
@ -41,6 +52,7 @@ void directPredecessor( void );
void localMean( void );
int main(int argc, char **argv ) {
char fileName[50];
int i;
@ -49,16 +61,16 @@ int main(int argc, char **argv ) {
for (i = 0; i < M; i++) {
_x[i] += ((255.0 / M) * i); // Init test values
for (int k = 1; k < M; k++){
for (int k = 0; k < M; k++){
w[k][i] = rndm(); // Init weights
}
}
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++ ){
for (i = 0; i <= tracking; i++){
for ( int k = 0; k < tracking; k++ ){
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
}
}
@ -66,16 +78,15 @@ int main(int argc, char **argv ) {
// math magic
directPredecessor(); // TODO: needs some love!
//localMean();
localMean();
directPredecessor(); // TODO: used_weights.txt has gone missing!
// save test_array after math magic happened
// 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++) {
for (int k = 0; k < tracking; k++) {
fprintf(fp1, "[%d][%d] %lf\n", k,i, w[k][i]);
}
@ -105,7 +116,9 @@ void localMean( void ) {
memset(xError, 0, M);// initialize xError-array with Zero
int xCount = 0; // runtime var
int i;
mkFileName(fileName, sizeof(fileName), LOCAL_MEAN );
FILE* fp4 = fopen(fileName, "w");
fprintf(fp4, "\n\n\n\n*********************LocalMean*********************\n");
for (xCount = 1; xCount < M; xCount++){
@ -122,7 +135,8 @@ void localMean( void ) {
xPredicted += xMean;
xError [xCount] = xActual - xPredicted;
points[xCount].xVal[2] = xCount;
points[xCount].yVal[2] = xPredicted;
double xSquared = 0.0;
for ( i = 1; i < xCount; i++ ){ //get x squared
@ -132,8 +146,9 @@ void localMean( void ) {
for ( i - 1; i < xCount; i++ ){ //update weights
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared);
}
}
fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
}
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
double mean = sum_array(xError, xErrorLength) / M;
double deviation = 0.0;
@ -147,10 +162,10 @@ void localMean( void ) {
// write in file
mkFileName( fileName, sizeof(fileName), RESULTS );
FILE *fp2 = fopen(fileName, "w+");
FILE *fp2 = fopen(fileName, "w");
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
fclose(fp2);
fclose(fp4);
}
@ -175,39 +190,44 @@ void directPredecessor( void ) {
// File handling
mkFileName( fileName, sizeof(fileName), DIRECT_PREDECESSOR);
FILE *fp3 = fopen(fileName, "w");
fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\n");
for ( xCount = 1; xCount < M+1; xCount++ ){
xActual = _x[xCount+1];
double xPredicted = 0.0;
for ( i = 1; i < xCount; i++ ){
xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1] ));
}
}
xPredicted += _x[xCount-1];
xError[xCount] = xActual - xPredicted;
fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
points[xCount].xVal[0] = xCount;
points[xCount].yVal[0] = xActual;
points[xCount].xVal[1] = xCount;
points[xCount].yVal[1] = xPredicted;
double xSquared = 0.0;
//get x squared
double xSquared = 0.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 ); //TODO: double val out of bounds
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i] - _x[xCount - i - 1] ) / xSquared ); //TODO: double val out of bounds
}
}
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
double mean = sum_array(xError, xErrorLength) / xErrorLength;
double deviation = 0.0;
for ( i = 0; i < xErrorLength -1; i++ ){
deviation += pow( xError[i] - mean, 2);
deviation += pow( xError[i] - mean, 2);
}
mkSvgGraph( points);
fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
fclose(fp3);
}
@ -252,7 +272,7 @@ char *mkFileName( char* buffer, size_t max_len, int suffixId) {
*/
char * fileSuffix( int id ) {
char * suffix[] = {"_weights_pure.txt", "_weights_used.txt", "direct_predecessor.txt", "ergebnisse.txt", "_graph.svg"};
char * suffix[] = {"_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt"};
return suffix[id];
}
@ -280,16 +300,25 @@ void Graph ( ) {
myLogger
Pipes to stdout or files by using a filepointer for logging purposes
Logs on filepointer, used for svg graphing
==========================================================================
*/
void myLogger ( FILE* fp, int myVar ){
fprintf( fp, "Logging: %d\n", myVar);
}
void myLogger ( FILE* fp, point_t points[] ){
int i;
for( i = 0; i <= M; i++ ){
fprintf( fp, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]);
}
fprintf(fp, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.8px\"/>\n<path d=\"M0 0\n");
for( i = 0; i < M-1; i++ ) {
fprintf( fp, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
}
fprintf(fp, "\" fill=\"none\" stroke=\"green\" stroke-width=\"0.8px\"/>\n<path d=\"M0 0\n");
for( i = 0; i <= M; i++ ) {
fprintf(fp, "L %f %f\n", points[i].xVal[2], points[i].yVal[2]);
}
}
@ -351,38 +380,108 @@ double rndm( void ) {
/*
==========================================================================
parser
getline
Parses files, used for template.svg mainly
This code is public domain -- Will Hartung 4/9/09
Microsoft Windows is not POSIX conform and does not support getline.
What the Heck?
=========================================================================
*/
size_t getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*p++ = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr - 1;
}
/*
==========================================================================
mkSvgGraph
parses template.svg and writes results in said template
==========================================================================
*/
char * parser( char buffer) {
FILE *fp = fopen ( "template.svg", "wr" );
void mkSvgGraph( point_t points[] ) {
FILE *input = fopen ( "template.svg", "r" );
FILE *target = fopen ( "output.svg", "w" );
char *line = NULL;
char *ptr;
// char *ptr;
size_t len = 0;
ssize_t read;
char string[] = "HECK!!!";
char values[64];
char firstGraph[15] = {"<path d=\"M0 0"}; /* There is a space behind the last zero,
but windows does not recognize it in strtsr() ?!
so no output will return*/
if( fp == NULL ) {
if( input == NULL ) {
exit(EXIT_FAILURE);
}
while ( ( read = getline(&line, &len, fp) ) != -1) {
// printf("Retrieved line of length %zu :\n", read);
//puts(line);
if( strstr(line, "<path d=\"M0 0 ") != NULL ) {
ptr = line;
printf("%s", *ptr);
fprintf(fp, "%s\n", string);
while ( ( read = getline(&line, &len, input) ) != -1) {
//printf("Retrieved line of length %zu :\n", read);
//puts(line); // debug purpose
fprintf(target, "%s",line);
if( strstr(line, firstGraph) != NULL ) {
//fprintf(target,"HECK!!!\n");
myLogger( target, points );
}
}
free(line);
exit(EXIT_SUCCESS);
// free(target);
// free(input);
//exit(EXIT_SUCCESS);
}