swisscheese4d.java

来自「eclispse下运行 模拟退火算法的java源代码」· Java 代码 · 共 58 行

JAVA
58
字号
package org.theblueplanet.annealing.examples;

import org.theblueplanet.annealing.ObjectiveFunction;

/**
 *  JAVA Wrapper around the example in Numerical Recipes in C,
 *  example book, pp 183
 *
 * @author     Charles M間nin
 * @since    October 29, 2001
 * @version    1.0
 */
public class SwissCheese4d implements ObjectiveFunction {
    private final static int NDIM    = 4;
    private final static double RAD  = 0.3;
    private final static double AUG  = 2.0;

    /**
     *  Gets the Ndim attribute of the SwissCheese4d object
     *
     * @return    The number of dimensions
     */
    public int getNdim() {
        return NDIM;
    }

    /**
     *  The 4d swiss cheese function
     *
     * @param  vertex  The vertices of the Simplex that the function is to evaluate
     * @return          The value of the function at the vertices
     */
    public static double func(double[] vertex) {
        double sumd   = 0;
        double sumr   = 0;
        double[] wid  = { 0.0, 1.0, 3.0, 10.0, 30.0 };
        for (int jj = 1; jj <= NDIM; jj++) {        //Fortran convention
            double q  = vertex[jj] * wid[jj];
            double r  = (double) (q >= 0 ? (int) (q + 0.5) : -(int) (0.5 - q));
            sumr += q * q;
            sumd += (q - r) * (q - r);
        }
        return 1 + sumr * (1 + (sumd > RAD * RAD ? AUG : AUG * sumd / (RAD * RAD)));
    }

    /**
     *  The main program to test the SwissCheese4d class
     *
     * @param  args  The command line arguments
     */
    public static void main(String[] args) {
        double[] vertex  = { 0, 0, 0, -10, 30 };
        System.out.println("" + func(vertex));
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?