This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron_Test.cpp
210 lines (199 loc) · 5.56 KB
/
Perceptron_Test.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Perceptron_Test.h"
#include "NANA_test.h"
#include "Neuron\\NAFunctions.h"
#include "Neuron\\Neuron.h"
#include "Neuron\\NeuralNet.h"
#include "Neuron\\Perceptron.h"
#include "Perceptron_test.h"
#include "Neuron\\Neuro_Constants.h"
#include <Math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
int MAX_IN,MAX_OUT,DIV1,DIV2;
Perceptron* PT;
/*ONLY FOR TESTING*/
double* CompressInput(double input[],int N,int type,int etta)
{
// THIS IS WRONG!!!
double betta=5.0;
// END OF WRONG!!!
double* new_input = new double[N];
double max,min,measure;
max=input[0];
min=input[0];
for (int i=0;i<N;i++)
{ if (max<input[i]) max=input[i]; if (min>input[i]) min=input[i]; }
//if (abs(max)>abs(min)) measure=max;
//else measure=min;
for (int j=0;j<N;j++)
new_input[j]=input[j]-min;
switch (type)
{
case 5:
{
for (int k=0;k<N;k++)
{
if ((max-min)!=0)
new_input[k]=new_input[k]*(2*betta/(max-min))-betta;
else
new_input=0;
}
}
break;
}
return new_input;
}
double* CompressOutput(double output[],int N,int type,int etta)
{
// THIS IS WRONG!!!
double betta=0.5;
// END OF WRONG!!!
double* new_output = new double[N];
double max,min,measure;
max=output[0];
min=output[0];
for (int i=0;i<N;i++)
{ if (max<output[i]) max=output[i]; if (min>output[i]) min=output[i]; }
//if (abs(max)>abs(min)) measure=max;
//else measure=min;
for (int j=0;j<N;j++)
new_output[j]=output[j]-min;
switch (type)
{
case 5:
{
for (int k=0;k<N;k++)
{
if ((max-min)!=0)
new_output[k]=new_output[k]*(betta/(max-min))+0.5;
else {
if (new_output[k]<1 && new_output[k]>-1)
new_output[k]=abs(new_output[k])+0.5;
else
new_output[k]=betta/new_output[k]+1;
}
}
}
break;
}
return new_output;
}
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
MAX_IN=4;
MAX_OUT=1;
DIV1=5;
DIV2=2;
PT = new Perceptron(MAX_IN,MAX_OUT,DIV1,DIV2,1,5,1);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
double x=Edit1->Text.ToDouble();
double w=Edit2->Text.ToDouble();
double b=Edit3->Text.ToDouble();
double* input = new double[MAX_IN];
double* output_l = new double[MAX_OUT];
double* output = new double [MAX_OUT];
double* input_r = new double[MAX_IN];
double* output_r =new double[MAX_OUT];
PT->Learning_SetAlpha(1);
PT->Learning_SetEpsilon(0.02);
PT->Learning_SetEtta(1);
int j=0;
/*for (int ii=0;ii<100;ii++)
{
double x=Edit1->Text.ToDouble();
double w=Edit2->Text.ToDouble();
double b=Edit3->Text.ToDouble();
randomize();
/*for (int ij=0;ij<10;ij++)
{
w+=0.5-random(10)/100.0;
x+=0.5-random(10)/100.0;
b+=0.5-random(10)/100.0;
for (j=0;j<MAX_IN;j++)
input[j]=(cos((x+j/2.0)*w+b));
int n=0;
for (n=0;n<MAX_OUT;n++)
{
output_l[n]=(cos((x+j/2.0)*w+b));
//ShowMessage(output_l[n]);
j++;
}
input_r=CompressInput(input,MAX_IN,5,1);
output_r=CompressOutput(output_l,MAX_OUT,5,1);
double l_state=(input_r[MAX_IN-1]+input_r[MAX_IN-2])/2;
//ShowMessage(output_r[0]);
output_l[0]=l_state;
PT->Learn(input_r,output_l);
}
for (int k=0;k<MAX_OUT;k++)
{
Memo1->Lines->Add(AnsiString(output[k]));
Memo2->Lines->Add(AnsiString(output_l[k]));
}
}
output=PT->GetResult();
int i=0;
Series1->Clear();
Series2->Clear();
/*for (i=0;i<MAX_IN;i++)
{
//ShowMessage(input_r[i]);
Series1->AddXY(i,input_r[i]);
Series2->AddXY(i,input_r[i]);
Memo1->Lines->Add(AnsiString(input_r[i]));
Memo2->Lines->Add(AnsiString(input_r[i]));
} */
for (int i=0;i<1000;i++)
{
input_r[0]=0.75-0.25+random(50)/100.0;
input_r[1]=0.75-0.25+random(50)/100.0;
input_r[2]=0.75-0.25+random(50)/100.0;
input_r[3]=0.75-0.25+random(50)/100.0;
output_l[0]=(input_r[0]+input_r[1]+input_r[2]+input_r[3])/4;
PT->Learn(input_r,output_l);
}
output=PT->GetResult();
for (int k=0;k<MAX_OUT;k++)
{
//ShowMessage(output[k]);
Series1->AddXY(k,output_l[k]);
Series2->AddXY(k,output[k]);
Memo1->Lines->Add(AnsiString(output[k]));
Memo2->Lines->Add(AnsiString(output_l[k]));
//}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button3Click(TObject *Sender)
{
double x=Edit1->Text.ToDouble();
double w=Edit2->Text.ToDouble();
double b=Edit3->Text.ToDouble();
double* input = new double[MAX_IN];
double* output = new double[MAX_OUT];
input[0]=0.75-0.25+random(50)/100.0;
input[1]=0.75-0.25+random(50)/100.0;
input[2]=0.75-0.25+random(50)/100.0;
input[3]=0.75-0.25+random(50)/100.0;
output[0]=(input[0]+input[1]+input[2]+input[3])/4;
Memo1->Lines->Clear();
Memo2->Lines->Clear();
Memo2->Lines->Add(AnsiString(output[0]));
output=PT->GetResult(input);
Memo1->Lines->Add(AnsiString(output[0]));
}
//---------------------------------------------------------------------------