📄 annealingscheme.java
字号:
package org.theblueplanet.annealing;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Encapsulates the parameters passed around by the UI layer of JSimul
*
* @author Charles M間nin
* @since October 17, 2001
* @version 1.0
*/
public class AnnealingScheme {
private double temperature; // Log10 value
private int coolingRate; // 0=no cooling 1=100% T drop
private int nIterations; // Number of iterations before cooling is applied
private int iteration; // The current iteration number;
private double[] offset;
private double tolerance;
private double defaultTemperature = 1.e+6;
private int defaultCoolingRate = 20;
private int defaultNIterations = 20;
private double defaultTolerance = 1.e-5;
private int ndim;
private Class ofClass;
private String className;
private Method method;
private String methodName;
private static String ofInterface = "org.theblueplanet.annealing.ObjectiveFunction";
/**
* Default constructor for the AnnealingScheme object
*/
public AnnealingScheme() {
setDefaultValues();
}
/**
* Convenience constructor for the AnnealingScheme object
*
* @param temperature The initial temperature
* @param coolingRate The cooling rate (from 0 to 1)
* @param nIterations The number of iterations before cooling is applied
* @param tolerance Description of Parameter
* @param offset Description of Parameter
* @param className Description of Parameter
* @param methodName Description of Parameter
*/
public AnnealingScheme(double temperature, int coolingRate, int nIterations,
double tolerance, double[] offset, String className,
String methodName)
throws Exception {
setTemperature(temperature);
setCoolingRate(coolingRate);
setNIterations(nIterations);
setTolerance(tolerance);
setOffset(offset);
setClassName(className);
setOfClass(className);
setMethodName(methodName);
setMethod(methodName);
iteration = 0;
}
/**
* Sets the Temperature attribute of the AnnealingScheme object
*
* @param temperature The new Temperature value
*/
public void setTemperature(double temperature) {
this.temperature = temperature;
}
/**
* Sets the Tolerance attribute of the AnnealingScheme object
*
* @param tolerance The new Tolerance value
*/
public void setTolerance(double tolerance) {
this.tolerance = tolerance;
}
/**
* Sets the Offset attribute of the AnnealingScheme object
*
* @param offset The new Offset value
*/
public void setOffset(double[] offset) {
this.offset = offset;
}
/**
* Sets the Offset attribute of the AnnealingScheme object
*
* @param offset The new Offset value
*/
public void setOffset(double offset, int ii) {
this.offset[ii] = offset;
}
/**
* Sets the Ndim attribute of the AnnealingScheme object
*
* @param ndim The new Ndim value
*/
public void setNdim(int ndim) {
this.ndim = ndim;
}
/**
* Sets the OfClass attribute of the AnnealingScheme object
*
* @param className The new OfClass value
* @exception ClassNotFoundException Description of Exception
* @exception IllegalAccessException Description of Exception
* @exception NoSuchFieldException Description of Exception
* @exception InstantiationException Description of Exception
* @exception NoSuchMethodException Description of Exception
* @exception InvocationTargetException Description of Exception
*/
public void setOfClass(String className)
throws ClassNotFoundException, IllegalAccessException,
NoSuchFieldException, InstantiationException,
NoSuchMethodException, InvocationTargetException {
ofClass = Class.forName(className);
Class[] interfaces = ofClass.getInterfaces();
boolean found = false;
for (int ii = 0; ii < interfaces.length; ii++) {
if (interfaces[ii].getName().equals(ofInterface)) {
found = true;
break;
}
}
if (!found) {
throw new InstantiationException("Objective function does not implement " + ofInterface);
}
Method getNdim = ofClass.getMethod("getNdim", null);
setNdim(((Integer) getNdim.invoke(ofClass.newInstance(), null)).intValue());
}
/**
* Sets the OfClass attribute of the AnnealingScheme object
*
* @exception ClassNotFoundException Description of Exception
* @exception IllegalAccessException Description of Exception
* @exception NoSuchFieldException Description of Exception
* @exception InstantiationException Description of Exception
* @exception NoSuchMethodException Description of Exception
* @exception InvocationTargetException Description of Exception
*/
public void setOfClass()
throws ClassNotFoundException, IllegalAccessException,
NoSuchFieldException, InstantiationException,
NoSuchMethodException, InvocationTargetException {
ofClass = Class.forName(getClassName());
Class[] interfaces = ofClass.getInterfaces();
boolean found = false;
for (int ii = 0; ii < interfaces.length; ii++) {
if (interfaces[ii].getName().equals(ofInterface)) {
found = true;
break;
}
}
if (!found) {
throw new InstantiationException("Objective function does not implement " + ofInterface);
}
Method getNdim = ofClass.getMethod("getNdim", null);
setNdim(((Integer) getNdim.invoke(ofClass.newInstance(), null)).intValue());
}
/**
* Sets the ClassName attribute of the AnnealingScheme object
*
* @param className The new ClassName value
*/
public void setClassName(String className) {
this.className = className;
}
/**
* Sets the Method attribute of the AnnealingScheme object
*
* @param methodName The new Method value
*/
public void setMethod(String methodName) {
Method[] methods = ofClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(methodName)) {
this.method = methods[i];
break;
}
}
}
/**
* Sets the Method attribute of the AnnealingScheme object
*/
public void setMethod() {
Method[] methods = ofClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(getMethodName())) {
this.method = methods[i];
}
}
}
/**
* Sets the MethodName attribute of the AnnealingScheme object
*
* @param methodName The new MethodName value
*/
public void setMethodName(String methodName) {
this.methodName = methodName;
}
/**
* Sets the CoolingRate attribute of the AnnealingScheme object
*
* @param coolingRate The new CoolingRate value
*/
public void setCoolingRate(int coolingRate) {
this.coolingRate = coolingRate;
}
/**
* Sets the NIterations attribute of the AnnealingScheme object
*
* @param nIterations The new NIterations value
*/
public void setNIterations(int nIterations) {
this.nIterations = nIterations;
}
/**
* Sets the DefaultValues for the AnnealingScheme object
*/
public void setDefaultValues() {
this.temperature = this.defaultTemperature;
this.coolingRate = this.defaultCoolingRate;
this.nIterations = this.defaultNIterations;
this.tolerance = this.defaultTolerance;
setDefaultOffset();
}
/**
* Sets the DefaultOffset for the AnnealingScheme object
*/
public void setDefaultOffset() {
offset = new double[ndim + 1];
for (int ii = 0; ii <= ndim; ii++) {
offset[ii] = 0.0;
}
}
/**
* Gets the ClassName attribute of the AnnealingScheme object
*
* @return The ClassName value
*/
public String getClassName() {
return className;
}
/**
* Gets the MethodName attribute of the AnnealingScheme object
*
* @return The MethodName value
*/
public String getMethodName() {
return methodName;
}
/**
* Gets the Offset attribute of the AnnealingScheme object
*
* @return The Offset value
*/
public double[] getOffset() {
return offset;
}
/**
* Gets the Temperature attribute of the AnnealingScheme object
*
* @return The Temperature value
*/
public double getTemperature() {
return temperature;
}
/**
* Gets the CoolingRate attribute of the AnnealingScheme object
*
* @return The CoolingRate value
*/
public int getCoolingRate() {
return coolingRate;
}
/**
* Gets the NIterations attribute of the AnnealingScheme object
*
* @return The NIterations value
*/
public int getNIterations() {
return nIterations;
}
/**
* Gets the Tolerance attribute of the AnnealingScheme object
*
* @return The Tolerance value
*/
public double getTolerance() {
return tolerance;
}
/**
* Gets the Ndim attribute of the AnnealingScheme object
*
* @return The Ndim value
*/
public int getNdim() {
return ndim;
}
/**
* Gets the OfClassObject attribute of the AnnealingScheme object
*
* @return The OfClassObject value
* @exception InstantiationException Description of Exception
* @exception IllegalAccessException Description of Exception
*/
public Object getOfClassObject()
throws InstantiationException, IllegalAccessException {
return getOfClass().newInstance();
}
/**
* Gets the OfClass attribute of the AnnealingScheme object
*
* @return The OfClass value
*/
public Class getOfClass() {
return ofClass;
}
/**
* Gets the Method attribute of the AnnealingScheme object
*
* @return The Method value
*/
public Method getMethod() {
return method;
}
/**
* Resets iteration to 0
*/
public void resetIteration() {
iteration = 0;
}
/**
* Increments the iteration field and cools if threshold is reached
*/
public void updateIteration() {
iteration++;
if (iteration == (nIterations - 1)) {
cool();
}
}
/**
* String representation of the AnnealingScheme Object
*
* @return The String representation of the Object
*/
public String toString() {
String string = null;
try {
StringBuffer sb = new StringBuffer();
for (int ii = 1; ii < offset.length; ii++) {
sb.append("offset[" + ii + "]=" + offset[ii] + " ");
}
sb.append("\n");
sb.append("T=" + getTemperature() + " R=" + getCoolingRate() +
"% N=" + getNIterations() + " tol=" + getTolerance() +
"\nClass=" + getClassName() +
"\nMethod=" + this.getMethodName());
string = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return string;
}
/**
* Updates temperature according to cooling scheme;
*/
private void cool() {
temperature *= (100. - coolingRate);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -