head	1.2;
access;
symbols;
locks; strict;
comment	@//@;


1.2
date	94.11.17.06.08.45;	author rjj;	state Exp;
branches;
next	1.1;

1.1
date	94.11.17.03.25.40;	author rjj;	state Exp;
branches;
next	;


desc
@Test file for the Single Layer Perceptron algorithm.
@


1.2
log
@Modified to not use Bit valued continuous images.
@
text
@// test-slp.c           Section: 13.5 Single Layer Perceptron (SLP)
//      Copyright 1994, Center for Computer Vision and Visualization,
//      University of Florida.  All rights reserved.
//
// Test the SLP algorithm.


#include "slp.h"
#include <fstream.h>



// read_pattern()  from cin
IA_Point<double>
read_pattern( const unsigned int m_value ) {

    IA_Point<double> p = extend_to_point( 0.0, m_value );

    for( int i=0; i<m_value; i++ ) {
	cin >> p[i];
    }

    return p;
}


int main( int argc, char * argv[] ) {

    // Get the arguments
    if (argc != 4 ) {

	cout << "Usage:" << endl;
	cout << "\t" << argv[0] << "  "
	     << "learning-rate  m-value  training-set-filename" << endl;
	cout << endl
	     << "The training set format should be:" << endl
	     << "classification(0 or 1)"
	     << "\tpattern-1st-value patern-2nd-value .. pattern-mth-value "
	     << endl << endl;
	cout << "Each pattern should be on a separate line" << endl;
	exit( 1 );
    }

    double learning_rate = atof( argv[1] );
    unsigned int m_value = atoi( argv[2] );

    ifstream training_stream( argv[3] );
    if ( !training_stream ) {
	cerr << "can not open file: " << argv[3] << endl;
    }

    SLP slp( learning_rate, m_value );

    cerr << "Initial weight is: " << slp.weight() << endl
	 << "Learning rate is : " << slp.learning_rate() << endl;

    int how_long = slp.train( training_stream, 10 );

    cerr << "Final weight is: " << slp.weight() << endl
	 << "Training loops : " << how_long << endl << endl;

    // Here is a little loop that allows the user to input patterns
    //   and have the SLP classify them.

    IA_Point<double>
	pattern = extend_to_point( 0.0, m_value );
    char menu;
    cout << "Choices: [pP] => read pattern, [qQ] => quit ? " ;
    cout.flush();
    while( (cin >> menu).good() && (menu != 'q') && (menu != 'Q') ) {

	switch( menu ) {
	case 'p':
	case 'P':
	    pattern = read_pattern( m_value );
	    cout << "Pattern: " << pattern
		 << " classified as: " << slp( pattern )
		 << endl;
	    break;

	case 'q':
	case 'Q':
	    break;

	default:
	    cerr << "Don't understand choice: " << menu << endl;
	}
	cout << "Choices: [pP] => read pattern, [qQ] => quit ? ";
	cout.flush();
    }

    exit( 0 );
}
@


1.1
log
@Initial revision
@
text
@a11 43
// read_training_set()  reading the and construct a BCI extensive image.
IA_Image<IA_Point<double>, IA_Bit>
read_training_set( ifstream &   training_file,
		   unsigned int m_value ) {

    unsigned int array_size = 10;  // initial size of arrays

    // Allocate parallel arrary for the domain and range.
    IA_Array<IA_Point<double> >  ts_domain( array_size );
    IA_Array<IA_Bit>             ts_values( array_size );

    int    pClass;
    int    pIndex = 0;
    while( ! training_file.eof() ) {
	// Make sure then is enough room in the arrary
	if (array_size == pIndex) {
	    array_size *= 2;
	    ts_domain.lengthen( array_size );
	    ts_values.lengthen( array_size );
	    // Did this work ?
	    if ( ( ts_domain.len() != array_size ) ||
		 ( ts_values.len() != array_size )   ) {
		cerr << "Array lengthing failed!" << endl;
		exit( 1 );
	    }
	}
	// Read and remember the classification of this pattern
	training_file >> pClass;
	ts_values[ pIndex ] = IA_Bit( pClass );

	// Read and remember the pattern.
	for( int m=0; m<m_value; m++ ) {
	    training_file >> ts_domain[ pIndex ][ m ];
	}
    }

    return
	IA_Image<IA_Point<double>, IA_Bit>(
	    ts_domain.data_start(), array_size,
	    ts_values.data_start(), array_size,
	    (1==0) );
}

a51 3
    IA_Image<IA_Point<double>, IA_Bit>
	ts = read_training_set( training_stream, m_value );

d57 1
a57 1
    int how_long = slp.train( ts, 10 );  // don\'t try more than 10 times
d60 1
a60 1
	 << "Training loops : " << how_long;
d68 2
a69 1
    cout << "Choices: [pP] => read pattern, [qQ] => quit" << endl;
d77 2
a78 2
		 << "classified as: " << slp( pattern )
		 << end;
d88 2
a89 1
	cout << "Choices: [pP] => read pattern, [qQ] => quit" << endl;
@
