File: makeweights.c

package info (click to toggle)
gnubg 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 30,800 kB
  • ctags: 8,224
  • sloc: ansic: 92,884; xml: 13,338; sh: 9,675; makefile: 425; python: 392; yacc: 295; sql: 237; lex: 220; php: 102; cs: 81; awk: 25; sed: 16
file content (116 lines) | stat: -rw-r--r-- 2,852 bytes parent folder | download
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
116
/*
 * makeweights.c
 *
 * by Gary Wong <gary@cs.arizona.edu>, 2000.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 or later of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: makeweights.c,v 1.20 2008/01/17 22:28:05 Superfly_Jon Exp $
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <locale.h>
#include "eval.h" /* for WEIGHTS_VERSION */

static void 
usage (char *prog) {
  fprintf (stderr, "Usage: %s [[-f] outputfile [inputfile]]\n"
	  "  outputfile: Output to file instead of stdout\n"
	  "  inputfile: Input from file instead of stdin\n",
	   prog);

  exit (1);
}

extern int main( int argc, char *argv[] )
{
    neuralnet nn;
    char szFileVersion[ 16 ];
    static float ar[ 2 ] = { WEIGHTS_MAGIC_BINARY, WEIGHTS_VERSION_BINARY };
    int c;
    FILE *input = stdin, *output = stdout;

	if (argc > 1)
	{
		int arg = 1;
		if (!StrCaseCmp (argv[1], "-f"))
			arg++;	/* Skip */

		if (argc > arg + 2)
			usage (argv[0]);
		if ((output = fopen (argv[arg], "wb")) == 0)
		{
			perror ("Can't open output file");
			exit (1);
		}
		if (argc == arg + 2)
		{
			if ((input = fopen(argv[arg + 1], "r")) == 0)
			{
				perror ("Can't open input file");
				exit (1);
			}
		}
    }

    if (!setlocale (LC_ALL, "C") || 
		!bindtextdomain (PACKAGE, LOCALEDIR) || 
		!textdomain (PACKAGE))
	{
		perror ("seting locale failed");
		exit (1);
	}

    /* generate weights */
    
    if (fscanf(input, "GNU Backgammon %15s\n", szFileVersion ) != 1)
	{
		fprintf( stderr, _("%s: invalid weights file\n"), argv[ 0 ] );
		return EXIT_FAILURE;
    }

    if (StrCaseCmp( szFileVersion, WEIGHTS_VERSION ) )
	{
		fprintf( stderr, _("%s: incorrect weights version\n"
							"(version %s is required, "
							"but these weights are %s)\n" ),
				argv[ 0 ], WEIGHTS_VERSION, szFileVersion );
		return EXIT_FAILURE;
    }
	
    fwrite( ar, sizeof( ar[ 0 ] ), 2, output );

    for( c = 0; !feof(input); c++ )
	{
		if (NeuralNetLoad( &nn, input ) == -1)
		{
			fprintf(stderr, "Failed to load neural net!");
			return EXIT_FAILURE;
		}
		if( NeuralNetSaveBinary( &nn, output ) == -1 )
		{
			fprintf(stderr, "Failed to save neural net!");
			return EXIT_FAILURE;
		}
	}

    fprintf( stderr, _("%d nets converted\n"), c );

    return EXIT_SUCCESS;

}