www.pudn.com > Network.rar > Network.java


package mathtools.bptools; 
 
import java.util.Random; 
import java.util.Vector; 
 
/** 
 * 

Title: 高级算法

*

Description: 包括遗传算法和SCE算法

*

Copyright: Copyright (c) 2005

*

Company: 大连理工大学水利信息研究所

* @author 廖胜利 * @version 1.0 */ public class Network { private static final int BIGNUMBER = 1000000; private Random rand = new Random(); private char index_ActiveFun = '0'; private boolean trainRandomMode; private int[] order; private int n_input; private int n_output; private int n_layers; private Layer[] hideLayers; private Layer inputLayer; private Layer outputLayer; private double alfa; private double lph; private double lpo; private double[] inputs; private double[] desireds; private double error_total; private double error_total_one_circle_all; private double error_compared_to_tolerance; private double error_total_one_circle; public Network(double[][] n_input,double[] n_output,int n_hideLayers){ } public void setParamters(double alfa,double lph,double lpo){ this.lph = lph; this.lpo = lpo; this.alfa = alfa; } public void firstTimeSetting(){ for (int i = 0 ;i< n_layers;i++) for (int j = 0 ;j< hideLayers[i].n_nodes ;j++) hideLayers[i].nodes[j].threshold = -1.0 * rand.nextDouble()*BIGNUMBER / BIGNUMBER; for (int i = 0 ;i< n_output ;i++) outputLayer.nodes[i].threshold = -1.0 * rand.nextDouble()*BIGNUMBER / BIGNUMBER; } public void beforeTraining(double[] inputs,double[] desireds){ for(int i=0;i= 0; i--) for (int j = 0; j < hideLayers[i].n_nodes; j++) { hideLayers[i].nodes[j].error = 0; for (int x = 0; x < hideLayers[i + 1].n_nodes; x++) { hideLayers[i].nodes[j].error += hideLayers[i].nodes[j].weights[x] * hideLayers[i + 1].nodes[x].error; } hideLayers[i].nodes[j].error *= difActivefun(hideLayers[i].nodes[j].activation, index_ActiveFun); } for (int j = 0; j < inputLayer.n_nodes; j++) { inputLayer.nodes[j].error = 0; for (int x = 0; x < hideLayers[0].n_nodes; x++) { inputLayer.nodes[j].error += inputLayer.nodes[j].weights[x] * hideLayers[0].nodes[x].error; } inputLayer.nodes[j].error *= difActivefun(inputLayer.nodes[j].activation, index_ActiveFun); } } public void calNewThresholds() { double delta = 0; for (int i = 0; i < n_layers; i++) { for (int x = 0; x < hideLayers[i].n_nodes; x++) { delta = alfa * hideLayers[i].nodes[x].deltathresholdlast + hideLayers[i].nodes[x].error * lph; hideLayers[i].nodes[x].deltathresholdlast = delta; hideLayers[i].nodes[x].threshold = hideLayers[i].nodes[x]. threshold + delta; } } for (int x = 0; x < outputLayer.n_nodes; x++) { delta = alfa * outputLayer.nodes[x].deltathresholdlast + outputLayer.nodes[x].error * lpo; outputLayer.nodes[x].deltathresholdlast = delta; outputLayer.nodes[x].threshold = outputLayer.nodes[x].threshold + delta; } } public void calNewWeightsInHides() { double tmp = 0; double delta = 0; for (int j = 0; j < hideLayers[n_layers - 1].n_nodes; j++) { tmp = hideLayers[n_layers - 1].nodes[j].activation * lpo; for (int x = 0; x < n_output; x++) { delta = alfa * hideLayers[n_layers -1].nodes[j].deltaweightslast[x] + tmp * outputLayer.nodes[x].error; hideLayers[n_layers - 1].nodes[j].deltaweightslast[x] = delta; hideLayers[n_layers - 1].nodes[j].weights[x] = hideLayers[n_layers - 1].nodes[j].weights[x] + delta; } } for (int i = 0; i < n_layers - 1; i++) { for (int j = 0; j < hideLayers[i].n_nodes; j++) { tmp = hideLayers[i].nodes[j].activation * lph; for (int x = 0; x < hideLayers[i + 1].n_nodes - 1; x++) { delta = alfa * hideLayers[i].nodes[j].deltaweightslast[x] + tmp * hideLayers[i + 1].nodes[x].error; hideLayers[i].nodes[j].deltaweightslast[x] = delta; hideLayers[i].nodes[j].weights[x] = hideLayers[i].nodes[j].weights[x] + delta; } } } } public void calNewWeightsInInputs(){ double tmp = 0; double delta = 0; for(int i=0;i