using System; using System.Windows.Forms; namespace aiNnFingers { public class BAM { public BAM() { } public RichTextBox richTextBox; //#include //#include //typedef int BOOL; //typedef char CHAR; //typedef int INT; //#define FALSE 0 //#define TRUE 1 //#define NOT ! //#define AND && //#define OR || //#define LO -1 const int LO = -1; //#define HI +1 const int HI = +1; //#define BINARY(x) ((x)==LO ? false : true) //public bool BINARY(int x) //{ // if(x == LO) // return false; // else // return true; //} public int BINARY(int x) { if(x == LO) return 0; else return 1; } //#define BIPOLAR(x) ((x)==false ? LO : HI) public int BIPOLAR(bool x) { if(x == false) return LO; else return HI; } public int BIPOLAR(int x) { if(x == 0) return LO; else return HI; } public struct LAYER { /* A LAYER OF A NET: */ public int Units; /* - number of units in this layer */ public int[] Output; /* - output of ith unit */ public int[][] Weight; /* - connection weights to ith unit */ } public struct NET { /* A NET: */ public LAYER X; /* - X layer */ public LAYER Y; /* - Y layer */ } /****************************************************************************** R A N D O M S D R A W N F R O M D I S T R I B U T I O N S ******************************************************************************/ Random r; const int RAND_MAX = Int32.MaxValue; void InitializeRandoms() { //srand(4711); r = new Random(4711); } bool RandomEqualBOOL() { //return rand() % 2; int retVal = r.Next() % 2; if(retVal == 0) return false; else return true; //return Boolean.Parse(retVal.ToString()); } /****************************************************************************** A P P L I C A T I O N - S P E C I F I C C O D E ******************************************************************************/ //#define NUM_DATA 3 const int NUM_DATA = 3; //#define IN_CHARS 5 const int IN_CHARS = 5; //#define OUT_CHARS 7 const int OUT_CHARS = 7; //#define BITS_PER_CHAR 6 const int BITS_PER_CHAR = 6; //#define FIRST_CHAR ' ' const char FIRST_CHAR = ' '; //#define N (IN_CHARS * BITS_PER_CHAR) const int N = IN_CHARS * BITS_PER_CHAR; //#define M (OUT_CHARS * BITS_PER_CHAR) const int M = OUT_CHARS * BITS_PER_CHAR; char [][] Names = new char[NUM_DATA][] //IN_CHARS { "TINA ".ToCharArray(), "ANTJE".ToCharArray(), "LISA ".ToCharArray() }; char [][] Names_ = new char[NUM_DATA][] //IN_CHARS { "TINE ".ToCharArray(), "ANNJE".ToCharArray(), "RITA ".ToCharArray() }; char [][] Phones = new char[NUM_DATA][] //OUT_CHARS { "6843726".ToCharArray(), "8034673".ToCharArray(), "7260915".ToCharArray() }; int [][] Input = new int[NUM_DATA][]; //N int [][] Input_ = new int[NUM_DATA][]; //N int [][] Output = new int[NUM_DATA][]; //M //FILE* f; void InitializeApplication(ref NET Net) { int n,i,j,a,a_; for (n=0; n 0) Out = HI; if (Out != To.Output[i]) { Stable = false; To.Output[i] = Out; } } } return Stable; } //void PropagateNet(LAYER* From, LAYER* To) void PropagateNet(ref LAYER From, ref LAYER To) { bool Stable1, Stable2; do { Stable1 = PropagateLayer(ref From, ref To); Stable2 = PropagateLayer(ref To, ref From); } while (!(Stable1 && Stable2)); } /****************************************************************************** S I M U L A T I N G T H E N E T ******************************************************************************/ //void SimulateNet(LAYER* From, LAYER* To, int* Pattern, int* Input, int* Output) void SimulateNet(ref LAYER From, ref LAYER To, int[] Pattern, int[] Input, int[] Output) { SetInput(ref From, Pattern); richTextBox.AppendText(" . "); //fprintf(f, " . "); SetRandom(ref To); richTextBox.AppendText(" | "); //fprintf(f, " | "); PropagateNet(ref From, ref To); GetOutput(ref From, Input); richTextBox.AppendText(" . "); //fprintf(f, " . "); GetOutput(ref To, Output); richTextBox.AppendText("\n\n"); //fprintf(f, "\n\n"); } /****************************************************************************** M A I N ******************************************************************************/ public void main() { NET Net = new NET(); int n; int [] I = new int[N]; int [] O = new int[M]; InitializeRandoms(); GenerateNetwork(ref Net); InitializeApplication(ref Net); CalculateWeights(ref Net); for (n=0; n