📄 weightvectors.java
字号:
package fi.javasom.jsom;
/**
* This is the main container for the synaptic weight vectors.
*
* Copyright (C) 2001 Tomi Suuronen
*
* @version 1.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import fi.javasom.jsom.*;
import java.util.Random;
import java.util.Vector;
import java.io.*; //this line is only for debugging
public class WeightVectors
{
private Vector weight;
private double[] values;
private double[] location;
private String lattice; //topology of the map
private Random generator;
private int xDim;
private int yDim;
private int dimension; //dimensionality of a node
/**
* Main constructor. Used to contain the synaptic weight vectors during the learning phase.
* @param int xDim - X-dimension of the map constructed.
* @param int yDim - Y-dimension of the map constructed.
* @param int dimension - dimensionality of a node.
* @param String type - Lattice type of the map constructed (hexa or rect)
*/
public WeightVectors(int xDim,int yDim,int dimension,String type)
{
weight = new Vector(0,10);
this.xDim = xDim;
this.yDim = yDim;
this.dimension = dimension;
values = new double[dimension];
location = new double[2];
generator = new Random();
lattice = type;
int size = xDim * yDim;
int yCounter = 0;
int xCounter = 0;
double xValue = 0;
double yValue = 0;
boolean evenRow = false; //for hexagonal lattice, cheking if the current row number is even or odd
boolean even = true; //for hexagonal lattice, checking if the xDim is even or odd
if(lattice.equals("rect")) //rectangular lattice
{
for(int i=0;i<size;i++)
{
for(int j=0;j<dimension;j++)
{
values[j] = generator.nextDouble();
}
if(xCounter<xDim)
{
location[0] = xCounter;
location[1] = yCounter;
xCounter++;
}
else
{
xCounter = 0;
yCounter++;
location[0] = xCounter;
location[1] = yCounter;
xCounter++;
}
weight.addElement(new SomNode(values,location));
}
}
else //hexagonal lattice
{
for(int i=0;i<size;i++)
{
for(int j=0;j<dimension;j++)
{
values[j] = generator.nextDouble();
}
if(xCounter<xDim)
{
location[0] = xValue;
location[1] = yValue;
xValue += 1.0;
xCounter++;
}
else
{
xCounter = 0;
yValue += 0.866;
if(evenRow)
{
xValue = 0.0;
evenRow = false;
}
else
{
xValue = 0.5;
evenRow = true;
}
location[0] = xValue;
location[1] = yValue;
xValue += 1.0;
xCounter++;
}
weight.addElement(new SomNode(values,location));
}
}
weight.trimToSize();
}
/**
* Returns the x-dimension of this map.
*
* @return int - x-dimensionality of the map.
*/
public int getXDimension()
{
return xDim;
}
/**
* Returns the y-dimension of this map.
*
* @return int - y-dimensionality of the map.
*/
public int getYDimension()
{
return yDim;
}
/**
* Returns the node values at a specific node.
*
* @param int index - index of the SomNode
* @return double[] - returns the Node values from the specified index.
*/
public double[] getNodeValuesAt(int index)
{
SomNode cache = (SomNode)weight.elementAt(index);
return (cache.getValues());
}
/**
* Sets the node values at a specific node.
*
* @param int index - index of the SomNode
* @param double[] values - values of the SomNode
*/
public void setNodeValuesAt(int index,double[] values)
{
SomNode cache = (SomNode)weight.elementAt(index);
cache.setValues(values);
weight.setElementAt(cache,index);
}
/**
* Returns the node values at a specific node.
*
* @param int index - index of the SomNode
* @return double[] - returns the Node location from the specified index.
*/
public double[] getNodeLocationAt(int index)
{
SomNode cache = (SomNode)weight.elementAt(index);
return (cache.getLocation());
}
/**
* Returns the dimensionality of a node (it is the same for all of them).
*
* @return int - dimensionality of nodes.
*/
public int getDimensionalityOfNodes()
{
return dimension;
}
/**
* Returns the number of weight vectors.
*
* @return int - returns the number of weight vectors.
*/
public int getCount()
{
return weight.size();
}
/**
* Sets the label of a specific weight vector at the specified index.
*
* @param int index - the index of SomNode.
* @param String label - the new label for this SomNode.
* @return String - returns the Node label from the specified index.
*/
public void setNodeLabelAt(int index, String label)
{
SomNode cache = (SomNode)weight.elementAt(index);
if(cache.isLabeled())
{
cache.addLabel(label);
}
else
{
cache.setLabel(label);
}
weight.setElementAt(cache,index);
}
/**
* Returns a Node label of a specific weight vector from the specified index.
*
* @param int index - the index of SomNode.
* @return String - returns the Node label from the specified index.
*/
public String getNodeLabelAt(int index)
{
SomNode cache = (SomNode)weight.elementAt(index);
return (cache.getLabel());
}
/**
* Returns the lattice type used in initializing node locations.
*
* @return String - lattice :: hexa | rect.
*/
public String getLatticeType()
{
return lattice;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -