/* * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.*; import java.applet.Applet; public class cbnn extends Applet { TextField textFieldDay; TextField textFieldLat; TextField textFieldLon; TextField textFieldH; TextField textFieldTemp; TextField textFieldSal; TextField textFieldChl; TextField textFieldI0; TextField textFieldKpar; TextField textFieldOut; Label lDay; Label lLat; Label lLon; Label lH; Label lTemp; Label lSal; Label lChl; Label lI0; Label lKpar; Label lOut; float Min[] = {-999.0f,0.0f,0.0f,36.8f,75.6f,0.0f,0.0f,-0.5f,-0.8f,-0.3f,0.0f,0.0f,0.0f,0.9f}; float Range[] = {-999.0f,1.0f,1.0f,2.7f,1.0f,45.0f,32.0f,32.5f,2.7f,3.0f,80.0f,6.0f,25.0f,3.0f}; float WeightJ[][] = { { -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f}, { -999.00000f, 1.1179217f, 0.0101311f, -0.9204994f, -1.0943702f, -0.9454726f, 0.2059380f}, { -999.00000f, -1.3951159f, -0.2785148f, -1.6874403f, -0.1386388f, 2.0132263f, 0.3970137f}, { -999.00000f, 1.3137296f, -0.1501982f, -0.3806240f, -1.1836826f, -1.6301687f, 0.3326885f}, { -999.00000f, -0.1405120f, 0.1482198f, -0.0269649f, 0.6887614f, -0.9537338f, 0.3218282f}, { -999.00000f, -0.9829919f, -0.1739332f, -0.4457617f, -1.0803348f, 2.2035191f, 0.3190686f}, { -999.00000f, 1.1396267f, -0.1349930f, 0.1063905f, -0.6918762f, -0.3310774f, 0.3300896f}, { -999.00000f, 0.7429932f, 0.1851048f, -0.7049026f, 0.3792348f, -0.5157382f, 0.1810287f}, { -999.00000f, -0.8631381f, 0.2560259f, 0.5069237f, 1.2906398f, -0.1563179f, 0.5214708f}, { -999.00000f, -1.2068669f, 0.2774791f, 0.9684459f, 1.5150957f, -1.6103222f, 0.3057791f}, { -999.00000f, -3.0562310f, 0.0794369f, -0.2629206f, 0.9460537f, 2.2830753f, 0.3139087f}, { -999.00000f, 0.5390286f, 0.3809506f, 0.1272732f, -0.4253768f, 0.8536015f, 0.3269658f}, { -999.00000f, -0.1617956f, 0.1686302f, 0.6428890f, 0.7863723f, -0.9714188f, 0.2691347f}, { -999.00000f, 0.4484000f, 0.4374717f, 0.4364363f, 0.3831376f, 0.3012143f, 0.2379863f} }; float WeightK[][] = { { -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f, -999.00000f}, { -999.00000f, -2.8890665f, 0.3404180f, 1.5227233f, 1.3787863f, -2.1747873f, 0.4385961f} }; public void init() { textFieldDay = new TextField("0",10); lDay = new Label("Day (1-365)",Label.RIGHT); textFieldLat = new TextField("0",10); lLat = new Label("Latitude (dd.ddddN)",Label.RIGHT); textFieldLon = new TextField("0",10); lLon = new Label("Longitude (dd.ddddW)",Label.RIGHT); textFieldH = new TextField("0",10); lH = new Label("Depth (m)",Label.RIGHT); textFieldTemp = new TextField("0",10); lTemp = new Label("Water temp. (°C)",Label.RIGHT); textFieldSal = new TextField("0",10); lSal = new Label("Salinity (PSU)",Label.RIGHT); textFieldChl = new TextField("0",10); lChl = new Label("Chlorophyll (µg/l)",Label.RIGHT); textFieldI0 = new TextField("0",10); lI0 = new Label("Irradiance (E/m^2/day)",Label.RIGHT); textFieldKpar = new TextField("0",10); lKpar = new Label("Light ext. coeff. (1/m)",Label.RIGHT); textFieldOut = new TextField("---",10); lOut = new Label("PP estimate (mg C/m^2/day)",Label.RIGHT); textFieldOut.setEditable(false); //GridLayout with 6 rows, 4 columns //and 5 pixels of padding around all cell edges setLayout(new GridLayout(8,6,5,5)); add(new Label(" ",Label.LEFT)); add(lDay); add(textFieldDay); add(new Label(" ",Label.LEFT)); add(lLat); add(textFieldLat); add(lLon); add(textFieldLon); add(lH); add(textFieldH); add(lTemp); add(textFieldTemp); add(lSal); add(textFieldSal); add(lChl); add(textFieldChl); add(lI0); add(textFieldI0); add(lKpar); add(textFieldKpar); add(new Label(" ",Label.LEFT)); add(new Label(" ",Label.LEFT)); add(new Label(" ",Label.LEFT)); add(new Label(" ",Label.LEFT)); add(lOut); add(textFieldOut); add(new Label(" ",Label.LEFT)); add(new Button("Run NN")); validate(); } public boolean action(Event evt, Object arg) { String text; Float risultato; float x[] = new float[10]; float y; text=textFieldDay.getText(); x[1] = Float.valueOf(text).floatValue(); text=textFieldLat.getText(); x[2] = Float.valueOf(text).floatValue(); text=textFieldLon.getText(); x[3] = Float.valueOf(text).floatValue(); text=textFieldH.getText(); x[4] = Float.valueOf(text).floatValue(); text=textFieldTemp.getText(); x[5] = Float.valueOf(text).floatValue(); text=textFieldSal.getText(); x[6] = Float.valueOf(text).floatValue(); text=textFieldChl.getText(); x[7] = Float.valueOf(text).floatValue(); text=textFieldI0.getText(); x[8] = Float.valueOf(text).floatValue(); text=textFieldKpar.getText(); x[9] = Float.valueOf(text).floatValue(); y = feedforward (x); risultato = new Float (y); text = risultato.toString(); textFieldOut.setText(text); textFieldDay.selectAll(); return true; } public float feedforward(float x[]) { int ni = 12; int nh = 5; int no = 1; float NetJ[] = new float[nh+1+1]; float NetK[] = new float[no+1]; float OutJ[] = new float[nh+1+1]; float OutK[] = new float[no+1]; float Inp[] = new float[ni+1+1]; float z = 1.0f; //fills the net input vector (and //generates auxiliary input variables) //cos jd Inp[1] = (float) (Math.cos(x[1] / 365 * 3.1415926 * 2) + 1) / 2; //sin jd Inp[2] = (float) (Math.sin(x[1] / 365 * 3.1415926 * 2) + 1) / 2; //lat Inp[3] = x[2]; //lon Inp[4] = x[3]; //h Inp[5] = x[4]; //temp Inp[6] = x[5]; //sal Inp[7] = x[6]; //log(chl) Inp[8] = (float) (Math.log(x[7])/Math.log(10)); //I(0) Inp[10] = x[8]; //kpar Inp[11] = x[9]; //Zp Inp[12] = (float) (Math.log(100)/x[9]); //log(euchl) if (Inp[12]>Inp[5]) { Inp[9] = (float) (Math.log(x[7]*Inp[5])/Math.log(10)); Inp[12]=Inp[5]; } else { Inp[9] = (float) (Math.log(x[7]*Inp[12])/Math.log(10)); } //maps net input vector into [0,1] for (int i = 1; i < (ni+2); i++) { Inp[i] = (Inp[i] - Min[i]) / Range[i]; } //bias node input Inp[13] = 1.0f; //data are fed forward through NN for (int k = 1; k < (nh+2); k++) { NetJ[k] = 0.0f; for (int l = 1; l < (ni+2); l++) { NetJ[k] = NetJ[k] + Inp[l] * WeightJ[l][k]; } OutJ[k] = (float) (1 / (1 + Math.exp((-1) * NetJ[k]))); } OutJ[nh + 1] = 1.0f; for (int k = 1; k < (no+1); k++) { NetK[k] = 0.0f; for (int l = 1; l < (nh+2); l++) { NetK[k] = NetK[k] + OutJ[l] * WeightK[k][l]; } OutK[k] = (float) (1 / (1 + Math.exp((-1) * NetK[k]))); } //transforms [0,1] output to PP units for (int i = 1; i< (no+1); i++) { OutK[i] = (float) (Math.pow(10, OutK[i] * Range[ni + 1] + Min[ni + 1])); } z = OutK[1]* 1.1553f; return z; } }