package ann;
import ann.*;
public class Nnetwork implements java.io.Serializable,Cloneable{
public int n_input;
public int n_output;
public int n_layer;
public Layer hidelayer[];
public Layer input, output;
boolean iftrain[];
public double LH=-10;
public double LO=-10;
public double desiredOutputs[];
public double a=0.2;
public int connecttype;
public double total_error, total_error_one_circle_all;
public double error_compared_to_tolerance;
double total_error_one_circle[];
public int trainnum;
public Nnetwork() {
}
public Nnetwork(int inp, int hide[], int outp, int hidenum, int connecttype0) {
connecttype = connecttype0;
int i, j;
n_input = inp;
n_output = outp;
total_error_one_circle = new double[outp];
desiredOutputs = new double[outp];
output = new Layer(n_output);
for (i = 0; i < n_output; i++) {
output.nodes[i] = new Node(0);
}
n_layer = hidenum;
hidelayer = new Layer[n_layer];
for (i = n_layer - 1; i >= 0; i--) {
hidelayer[i] = new Layer(hide[i]);
for (j = 0; j < hidelayer[i].N_NODES; j++) {
if (i == n_layer - 1) {
hidelayer[i].nodes[j] = new Node(outp);
}
else {
hidelayer[i].nodes[j] = new Node(hidelayer[i + 1].N_NODES);
}
}
}
input = new Layer(n_input);
for (i = 0; i < n_input; i++) {
input.nodes[i] = new Node(hidelayer[0].N_NODES);
}
}
void FirstTimeSettings() {
for (int i = 0; i < n_layer; i++) {
int j;
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].threshold = (2 * Math.random() - 1) *
hidelayer[i].nodes[j].amultinum;
}
}
for (int i = 0; i < n_output; i++) {
output.nodes[i].threshold = (2 * Math.random() - 1) *
output.nodes[i].amultinum;
}
}
void BeforeTraining(double inp[], double outp[]) {
int i;
for (i = 0; i < n_input; i++) {
input.nodes[i].activation = inp[i];
}
for (i = 0; i < n_output; i++) {
desiredOutputs[i] = outp[i];
}
}
public void Calc_Activation(double result[]) {
int i, j, ci;
for (i = 0; i < n_layer; i++) {
if (i == 0) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].activation = 0;
for (ci = 0; ci < n_input; ci++) {
hidelayer[i].nodes[j].activation += input.nodes[ci].activation *input.nodes[ci].weights[j];
}
hidelayer[i].nodes[j].activation += hidelayer[i].nodes[j].threshold;
hidelayer[i].nodes[j].activation = activefun(hidelayer[i].nodes[j].activation);
}
}
else {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].activation = 0;
for (ci = 0; ci < hidelayer[i - 1].N_NODES; ci++) {
hidelayer[i].nodes[j].activation += hidelayer[i -1].nodes[ci].activation * hidelayer[i - 1].nodes[ci].weights[j];
}
hidelayer[i].nodes[j].activation += hidelayer[i].nodes[j].threshold;
hidelayer[i].nodes[j].activation = activefun(hidelayer[i].nodes[j].activation);
}
}
}
for (j = 0; j < output.N_NODES; j++) {
output.nodes[j].activation = 0;
for (ci = 0; ci < hidelayer[n_layer - 1].N_NODES; ci++) {
output.nodes[j].activation += hidelayer[n_layer -1].nodes[ci].activation * hidelayer[n_layer -1].nodes[ci].weights[j];
}
output.nodes[j].activation += output.nodes[j].threshold;
output.nodes[j].activation = activefun(output.nodes[j].activation);
}
for (i = 0; i < n_output; i++) {
result[i] = output.nodes[i].activation;
}
}
void Calc_error_output() {
for (int x = 0; x < n_output; x++)
//output.nodes[x].error = output.nodes[x].activation * (1 - output.nodes[x].activation ) * (desiredOutputs[x] - output.nodes[x].activation );
{
output.nodes[x].error += (output.nodes[x].activation - desiredOutputs[x]);
output.nodes[x].error *= difactivefun(output.nodes[x].activation);
}
}
void Calc_error_hidden() {
int j, i;
for (j = 0; j < hidelayer[n_layer - 1].N_NODES; j++) {
for (int x = 0; x < n_output; x++) {
hidelayer[n_layer - 1].nodes[j].error += hidelayer[n_layer -
1].nodes[j].weights[x] * output.nodes[x].error;
}
hidelayer[n_layer -
1].nodes[j].error *=
difactivefun(hidelayer[n_layer - 1].nodes[j].activation);
}
for (i = n_layer - 2; i >= 0; i--) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
for (int x = 0; x < hidelayer[i + 1].N_NODES; x++) {
hidelayer[i].nodes[j].error += hidelayer[i].nodes[j].weights[x] *
hidelayer[i + 1].nodes[x].error;
}
hidelayer[i].nodes[j].error *=
difactivefun(hidelayer[i].nodes[j].activation);
}
}
}
void Calc_new_Thresholds() {
int i;
// computing the thresholds for next itration for hidden layer
for (i = 0; i < n_layer; i++) {
for (int x = 0; x < hidelayer[i].N_NODES; x++) {
double det = a * hidelayer[i].nodes[x].detthresholdlast +
hidelayer[i].nodes[x].error * LH;
hidelayer[i].nodes[x].detthresholdlast = det;
hidelayer[i].nodes[x].threshold += det;
}
}
for (int y = 0; y < output.N_NODES; y++) {
double det = a * output.nodes[y].detthresholdlast +
output.nodes[y].error * LO;
output.nodes[y].detthresholdlast = det;
output.nodes[y].threshold += det;
}
}
void Calc_new_weights_in_hidden() {
int i, j;
double temp = 0.0f;
for (j = 0; j < hidelayer[n_layer - 1].N_NODES; j++) {
temp = hidelayer[n_layer - 1].nodes[j].activation * LO;
for (int y = 0; y < n_output; y++) {
double det = a * hidelayer[n_layer - 1].nodes[j].detweightslast[y] +
temp * output.nodes[y].error;
hidelayer[n_layer - 1].nodes[j].detweightslast[y] = det;
hidelayer[n_layer - 1].nodes[j].weights[y] += det;
}
}
for (i = 0; i < n_layer - 1; i++) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
temp = hidelayer[i].nodes[j].activation * LH;
for (int y = 0; y < hidelayer[i + 1].N_NODES; y++) {
double det = a * hidelayer[i].nodes[j].detweightslast[y] +
temp * hidelayer[i + 1].nodes[y].error;
hidelayer[i].nodes[j].detweightslast[y] = det;
hidelayer[i].nodes[j].weights[y] += det;
}
}
}
}
void Calc_new_weights_in_input() {
int j;
double temp = 0.0f;
for (j = 0; j < input.N_NODES; j++) {
temp = input.nodes[j].activation * LH;
for (int y = 0; y < hidelayer[0].N_NODES; y++) {
double det = a * input.nodes[j].detweightslast[y] +
temp * hidelayer[0].nodes[y].error;
input.nodes[j].detweightslast[y] = det;
input.nodes[j].weights[y] += det;
}
}
}
double Calc_total_error_in_pattern() {
double temp = 0.0;
for (int x = 0; x < n_output; x++) {
[x].activation - desiredOutputs[x]);
continue;
temp += Math.pow((output.nodes[x].activation - desiredOutputs[x]), 2);
total_error_one_circle[x] += Math.pow((output.nodes[x].activation - desiredOutputs[x]), 2);
}
total_error = temp;
total_error_one_circle_all += total_error;
return temp;
}
void reset_error() {
for (int i = 0; i < n_input; i++) {
input.nodes[i].error = 0;
}
for (int i = 0; i < n_output; i++) {
output.nodes[i].error = 0;
}
for (int i = 0; i < n_layer; i++) {
for (int j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].error = 0;
}
}
}
void reset_total_err