📄 bpngui.java
字号:
if(currentMe.patternPath != null && currentMe.patternPath.compareTo("")!=0)
loadPatterns.setDirectory(currentMe.patternPath);
loadPatterns.show();
patternfile = loadPatterns.getFile();
if(patternfile == null) return;
patternpath = loadPatterns.getDirectory();
loadPatternFile(patternpath, patternfile);
}
public void selectedStartLearning() {
try{
if(currentLearningThread == null){
currentLearningThread = new
learningThread(this, mainBPN, currentLearningSet,
curLearnStopMode, maxSesStop, minErrStop,
curSaveMode, CHOSE_RAND, showDataFrequence, showGraphFrequence);
currentLearningThread.start();
buttonStartStop.setLabel("Stop");
isLearning = true;
}
}catch(BPNException bpne){
todo = ""+bpne;
return;
}
}
public void selectedStepBy(){
try{
if(currentLearningThread == null){
currentLearningThread = new
learningThread(this, mainBPN, currentLearningSet,
MAXS, stepBy, 0.0,
curSaveMode, CHOSE_RAND, showDataFrequence, showGraphFrequence);
currentLearningThread.start();
buttonStartStop.setLabel("Stop");
isLearning = true;
}else{
todo = "BPN Neural Network is already in a learning session.";
return;
}
}catch(BPNException bpne){
todo = ""+bpne;
return;
}
}
public void selectedStopLearning() {
if(currentLearningThread != null){
currentLearningThread.stopNow();
currentLearningThread = null;
System.gc();
}
isLearning = false;
buttonStartStop.setLabel("Start");
refreshBPNgui();
}
public void selectedStopRules() {
if(BPNstopRules == null) BPNstopRules = new stopRules(this);
BPNstopRules.show(curLearnStopMode, maxSesStop, minErrStop);
}
public void selectedNetworkParameters() {
if(isLearning) return;
if(BPNnetworkParameters == null) BPNnetworkParameters = new networkParameters(this);
BPNnetworkParameters.show(mainBPN, minDomain, maxDomain);
}
public void selectedErrorGraph() {
if(BPNerrorGraph.isVisible()){
BPNerrorGraph.hide( );
}else{
BPNerrorGraph.show(minErrStop);
}
}
public void selectedDisplayFrequences(){
if(BPNdisplayRules == null) BPNdisplayRules = new displayRules(this);
BPNdisplayRules.show(showDataFrequence, showGraphFrequence);
}
//////////////////////////////////////////////////////////////////
//
// network structure modifiers
//
//////////////////////////////////////////////////////////////////
public void addLayerBefore(int selectedLayer){
if(isLearning) return;
if(selectedLayer==0){
todo = "Cannot create layers before Input Layer.";
return;
}
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
// allocate enough mem
int newLayerDef[] = new int[bpnd.layerDef.length+1];
String newActFnDef[] = new String[bpnd.layerDef.length];
double newBiasDef[] = new double[bpnd.layerDef.length+1];
for(int i=0,oldref=0; i<bpnd.layerDef.length+1;i++){
if(i == selectedLayer){
newLayerDef[i] = 1; // set default layer lenght
newBiasDef[i] = 0.0;// set default bias value
// copy from layer reference (which was poppedup)
// NOTE: the use of offs to reference right layer
newActFnDef[i-1] = bpnd.activationFnDef[i-1];
}else{
// WARNING :lost of details !
newBiasDef[i] = bpnd.layerBias1[oldref];
newLayerDef[i] = bpnd.layerDef[oldref];
if(i!=0) newActFnDef[i-1]= bpnd.activationFnDef[oldref-1];
oldref++;
}
}
createNewMainBPN(newLayerDef, newActFnDef, newBiasDef);
}
public void addLayerAfter(int selectedLayer){
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
if(selectedLayer==bpnd.layerDef.length-1){
todo = "Cannot create layers after Output Layer.";
return;
}
// as stupid it could seem ...
addLayerBefore(selectedLayer + 1);
}
public void deleteLayer(int selectedLayer){
if(isLearning) return;
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
if(selectedLayer==0){
todo = "Cannot delete Input Layer.";
return;
}
if(selectedLayer==bpnd.layerDef.length-1){
todo = "Cannot delete Output Layer.";
return;
}
// allocate enough mem
int newLayerDef[] = new int[bpnd.layerDef.length-1];
double newBiasDef[] = new double[bpnd.layerDef.length-1];
String newActFnDef[] = new String[bpnd.layerDef.length-2];
for(int i=0,newref=0; i<bpnd.layerDef.length;i++){
if(i != selectedLayer){
newLayerDef[newref] = bpnd.layerDef[i];
// WARNING :lost of details !
newBiasDef[newref] = bpnd.layerBias1[i];
if(newref!=0) newActFnDef[newref-1]= bpnd.activationFnDef[i-1];
newref++;
}
}
createNewMainBPN(newLayerDef, newActFnDef, newBiasDef);
}
public void editLayer(int selectedLayer){
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
if(BPNlayerSettings == null)BPNlayerSettings = new layerSettings(this);
BPNlayerSettings.show(bpnd, selectedLayer, "Layer "+selectedLayer);
}
public void createNewMainBPN(int newLayerDef[], String newActFnDef[], double newBias[]){
if(isLearning) return;
double tempMomentum = mainBPN.getMomentum();
double tempLearningRate = mainBPN.getLearningRate();
double tempInitMin = mainBPN.getInitMin();
double tempInitMax = mainBPN.getInitMax();
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
BPN tempBPN = null;
// if input or output layer change size the patterns become obsolete
if(currentLearningSet != null && (newLayerDef[0] != bpnd.layerDef[0] ||
newLayerDef[newLayerDef.length-1] != bpnd.layerDef[bpnd.layerDef.length-1])){
currentLearningSet = null;
entryPatternRef.setText("0");
entryPatternRef.disable();
valuesScale.setScaleIsOn(false);
putOnMonitor("Input or Output layer size changed, patterns are obsolete.\n");
}
bpnd = null;
// Create a new BPN with new descriptors !
try{
tempBPN = new BPN(newLayerDef, newActFnDef);
}catch(BPNException bpne){
todo = ""+bpne;
return;
}
if(tempBPN==null){
todo = "Something gone wrong on creating new NN.";
return;
}
// free some memory
undoBPN = mainBPN;
System.gc();
// installing new one
mainBPN = tempBPN;
// refreshing data and graphics in BPNgui
refreshBPNgui();
// restoring old NN settings
mainBPN.setMomentum(tempMomentum);
mainBPN.setLearningRate(tempLearningRate);
mainBPN.setInitMin(tempInitMin);
mainBPN.setInitMax(tempInitMax);
// randomizing weigths
try{
mainBPN.randomize();
}catch(BPNException bpne){
todo = ""+bpne;
}
for(int i=0; i<newLayerDef.length; i++)
setBias(i, newBias[i]);
needSave = true;
}
public void undoAction(int selectedLayer){
// if(undoBPN == null){
// todo = "Nothing to undo.";
// return;
// }
// BPN tempBPN = undoBPN;
// // perform swap
// undoBPN = mainBPN;
// mainBPN = tempBPN;
//
// // refreshing data and graphics in BPNgui
// refreshBPNgui();
// putOnMonitor("Undo last action.\n");
}
//////////////////////////////////////////////////////////////////
//
// Callbacks from windows
//
//////////////////////////////////////////////////////////////////
public void setDomainRange(double min, double max){
minDomain = min;
maxDomain = max;
valuesScale.setDomainValue(min, max);
if(currentLearningSet != null){
currentLearningSet.scaleDataTo(min,max);
refreshBPNgui();
}
currentMe.minDomain = minDomain;
currentMe.maxDomain = maxDomain;
}
public void setDisplayFrequences(int dataFreq, int graphFreq){
showDataFrequence = dataFreq;
showGraphFrequence = graphFreq;
}
public void setMinimalErrorStop(double val){
minErrStop = val;
BPNerrorGraph.setMinLimit(minErrStop);
}
public void setMaximalSessionStop(int val){
maxSesStop = val;
}
public void setCurrentError(double err){
labelCurrentGlobalError.setText(""+err);
BPNerrorGraph.putElement(err);
}
public void setCurrentSession(int val){
labelLearningSession.setText(""+val);
}
public void setLowerGlobalError(double val){
labelLowerGlobalError.setText(""+val);
labelCurrentGlobalError.setText(""+val);
BPNerrorGraph.putElement(val);
}
//////////////////////////////////////////////////////////////////
//
// network values modifiers
//
//////////////////////////////////////////////////////////////////
public void setBias(int layer, double value){
if(isLearning) return;
try{
mainBPN.setBias(layer, value);
if(currentLearningSet != null)
refreshBPNgui();
}catch(BPNException bpne){
todo = ""+bpne;
}
needSave = true;
}
public void setActivationFnClass(int layer, String ActFn){
if(isLearning) return;
try{
mainBPN.setActivationFnClass(layer, ActFn);
if(currentLearningSet != null)
refreshBPNgui();
}catch(BPNException bpne){
todo = ""+bpne;
}
needSave = true;
}
//////////////////////////////////////////////////////////////////
//
// network I/O
//
//////////////////////////////////////////////////////////////////
public boolean saveModule(String path, String file){
try{
mainBPN.saveNeuralNetwork(path,file);
}catch(BPNException bpne){
todo = ""+bpne;
return false;
}
needSave = false;
putOnMonitor("Module saved to "+path+file+"\n");
return true;
}
public void loadPatternFile(String patternpath, String patternfile){
learningSet tempLearningSet;
BPNdescriptor bpnd = mainBPN.getBPNdescriptor();
try{
tempLearningSet = new learningSet(patternpath,patternfile, bpnd.layerDef[0], bpnd.layerDef[bpnd.layerDef.length-1], minDomain, maxDomain );
}catch(BPNException bpne){
todo = ""+bpne;
return;
}
currentMe.patternPath = "."; //patternpath;
currentMe.patternFile = patternfile;
// enable components
entryPatternRef.enable();
valuesScale.setScaleIsOn(true);
currentLearningSet = tempLearningSet;
showPattern();
putOnMonitor("successfully loaded "+currentLearningSet.getSize()+" patterns.\n");
}
//////////////////////////////////////////////////////////////////
//
// Monitoring & footering
//
//////////////////////////////////////////////////////////////////
public void putOnMonitor(String msg){
textMonitor.appendText(msg);
}
public void putOnFooter(String msg){
labelOnFoot.setText(msg);
}
//////////////////////////////////////////////////////////////////
//
// thread guard starter, stopper and body
//
//////////////////////////////////////////////////////////////////
public void start() {
if(threadGuard == null){
threadGuard = new Thread(this, "BPNgui guard");
threadGuard.start();
}
}
public void stop(){
threadGuard.stop();
threadGuard = null;
}
public void run(){
Runtime rt = Runtime.getRuntime();
long mem1,mem2;
while(true){
// Call Garbage Collect
//System.gc();
if(todo.compareTo(NONE) != 0){
BPNerrorDialog = new errorDialog(this,todo);
BPNerrorDialog.show();
todo = NONE;
}
mem1 = rt.totalMemory()-rt.freeMemory();
// never try to free some memory ...
System.gc();
mem2 = rt.totalMemory()-rt.freeMemory();
//putOnFooter("Used Memory "+mem2+" of total "+rt.totalMemory()+" from last GC saved "+(mem2-mem1));
// wait a tenth of a second, then draw it again!
try {Thread.sleep(500);} catch (InterruptedException e){};
}
}
//////////////////////////////////////////////////////////////////
//
// Window constructors
//
//////////////////////////////////////////////////////////////////
public void setTitle(String path, String name){
this.setTitle("BPN - "+path+name);
}
private void reset(GridBagConstraints con) {
con.gridx = GridBagConstraints.RELATIVE;
con.gridy = GridBagConstraints.RELATIVE;
con.gridwidth = 1;
con.gridheight = 1;
con.weightx = 0;
con.weighty = 0;
con.anchor = GridBagConstraints.CENTER;
con.fill = GridBagConstraints.NONE;
con.insets = new Insets(0, 0, 0, 0);
con.ipadx = 0;
con.ipady = 0;
}
private void createComponents(){
int w=0, h=0;
Separator separatorV1;
Separator separatorV2;
Separator separatorH1;
Separator separatorH2;
Separator separatorH3;
Separator separatorH4;
Separator separatorH5;
Separator separatorH6;
Label label_2;
Label label_3;
Label label_4;
Label label_5;
Label label_8;
Label label_10;
Label label_11;
Label label_12;
Label label_13;
// offscreen Creations
saveBPN = new FileDialog(this, "Save BPN File", FileDialog.SAVE);
loadPatterns = new FileDialog(this, "Load Pattern File ", FileDialog.LOAD);
BPNerrorGraph = new errorGraph();
// main panel
GridBagLayout grid = new GridBagLayout();
int rowHeights[] = {0,5,111,5,20,30,5,20,25,25,5,20,15,15,15,5,20,30,5,20};
int columnWidths[] = {0,300,5,50,5,75,75,35,75,5};
//int columnWidths[] = {0,300,5,75,75,35,75,5,40};
double rowWeights[] = {0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.01};
double columnWeights[] = {0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
grid.rowHeights = rowHeights;
grid.columnWidths = columnWidths;
grid.rowWeights = rowWeights;
grid.columnWeights = columnWeights;
w = 0;
h = 0;
for(int i=0;i<rowHeights.length;i++) h += rowHeights[i];
for(int i=0;i<columnWidths.length;i++) w += columnWidths[i];
resize(insets().left + insets().right+w+50, insets().top + insets().bottom + h +70);
this.setBackground(Color.lightGray);
CheckboxGroup keepWeightsGroup = new CheckboxGroup();
separatorV1 = new Separator(Separator.VERTICAL);
separatorV1.perSide = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -