File: parity.c

package info (click to toggle)
libfann 2.1.0~beta%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,648 kB
  • ctags: 924
  • sloc: sh: 8,753; ansic: 5,994; cpp: 2,351; makefile: 507; perl: 243; python: 131; sed: 7
file content (115 lines) | stat: -rwxr-xr-x 2,738 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*       Parity Benchmark Data Set Generator
	 
	 v1.0
	 Matt White  (mwhite+@cmu.edu)
	 9/19/93

	 QUESTIONS/COMMENTS: neural-bench@cs.cmu.edu

	 Usage: a.out <# of inputs>
           # of inputs defaults to 2, or XOR.
	   Care should be taken not to input a number greater than the number
	   of bits, usually 32, of the machine being used, minus one.  Thus,
           for most computers, the maximum input is 31.
  
	 Description
	 ~~~~~~~~~~~
	   This program generates parity benchmark data in the CMU Neural 
         Network Benchmark format. 
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


/*      Function Prototypes      */

void getCommandLine(int *, int, char **);
void printHeader(int);
void printSet(int);


int main(int argc, char **argv)
{
	int inputs;					/* Number of inputs into the neural network */

	getCommandLine(&inputs, argc, argv);
	printHeader(inputs);
	printSet(inputs);
}


/*      getCommandLine -  This function reads the values from the command line
	and interprets them.  The only argument should be the number of inputs
        to generate, which defaults to 2.  Extra arguments are ignored.
        Invalid arguments are flagged and an error is produced.
*/

void getCommandLine(int *inputs, int argc, char **argv)
{
	if(argc < 2)
		*inputs = 2;
	else if((**(argv + 1) >= '0') && (**(argv + 1) <= '9'))
		*inputs = atoi(*(argv + 1));
	else
	{
		fprintf(stderr, "Invalid argument: %s\n", *(argv + 1));
		fprintf(stderr, "Value should be an integer.\n");
		exit(1);
	}
}


/*      printHeader -  This function prints out the header information for the
	data set.  This includes generation time (local) and number of inputs.
	It also prints the $SETUP segment for the data set.
*/

void printHeader(int inputs)
{
	unsigned int num_points;

	num_points = pow(2, inputs);

	printf("%d %d 1\n", num_points, inputs);
}


/*      printSet -  Generates and prints out a data set having the specified
	number of inputs.
*/

void printSet(int inputs)
{
	unsigned int num_points,	/* Number of points of data to generate */
	  num_pos,					/* Number of positive bits in this number */
	  this_point,				/* Number being analyzed */
	  i,						/* General indexing variables */
	  j;

	num_points = pow(2, inputs);	/* Figure out how many points to generate */

	for(i = 0; i < num_points; i++)
	{
		num_pos = 0;
		this_point = i;
		for(j = 0; j < inputs; j++)
		{						/* Analyze the number */
			if((this_point & 1) == 1)
			{					/* Get a bit and use it */
				printf("1 ");
				num_pos++;
			}
			else
				printf("-1 ");
			this_point >>= 1;	/* Shift to the next bit */
		}

		if((num_pos % 2) == 1)	/* Print the expected output */
			printf("1\n");
		else
			printf("0\n");
	}
}