Java 的新手,似乎无法在 Google 上找到这个有点简单的问题的正确答案。
我正在使用遗传编程框架进行一些研究,并且一直在尝试根据我的需要定制他们的示例。定义主要功能留给用户,所以这就是我想要做的。
有一个预先存在的类,我正在尝试制作一个可运行的文件。它扩展了一个更高级别的类来调用其他功能。
这是我遇到问题的代码(来自内置示例):
public class Multiplexer extends GPModel {
public Multiplexer(final int noInputBits) {
...
}
...
public static void main(String[] args) {
new Multiplexer(6).run();
}
}
这是我看到的错误。我正在使用带有 JDK 1.7 的 NetBeans 7.3.1。
错误:在类 org.epochx.gp.model.Multiplexer 中找不到主方法,请将主方法定义为:public static void main(String[] args) Java 结果:1 BUILD SUCCESSFUL(总时间:0 秒)
它应该是相当微不足道的......我已经创建了具有主要功能的其他类并且没有遇到这个问题......它似乎只有在框架内定义的预先存在的类中存在这个问题。
编辑
抱歉,封装类已经公开了,忘记说了。
Edit2 完整源代码,根据要求...(epochx.org,如果有人感兴趣,v1.4.1)
package org.epochx.gp.model;
import java.util.*;
import org.epochx.epox.*;
import org.epochx.epox.bool.*;
import org.epochx.epox.lang.IfFunction;
import org.epochx.gp.representation.GPCandidateProgram;
import org.epochx.representation.CandidateProgram;
import org.epochx.tools.util.BoolUtils;
/**
* GP model for the multiplexer problems.
*
* <h4>Multiplexer problem</h4>
*
* Given n binary inputValues, a program that solves the majority problem will
* return true in all circumstances where a majority of the inputValues are true
* (or 1), and return false whenever there is not a majority of true values.
*/
public class Multiplexer extends GPModel {
// The names of the inputValues used in the grammar.
private final Variable[] variables;
// The boolean input sequences.
private final boolean[][] inputValues;
// No input bits.
private int noAddressBits;
private int noDataBits;
/**
* Constructs a Multiplexer model for the given number of inputs.
*
* @param noInputBits the number of inputs the multiplexer problem should be
* for
*/
public Multiplexer(final int noInputBits) {
// Generate the input sequences.
inputValues = BoolUtils.generateBoolSequences(noInputBits);
// Calculate number of address/data bits.
setBitSizes(noInputBits);
// Define functions.
final List<Node> syntax = new ArrayList<Node>();
syntax.add(new IfFunction());
syntax.add(new AndFunction());
syntax.add(new OrFunction());
syntax.add(new NotFunction());
// Define terminal variables.
variables = new Variable[noInputBits];
// Add address inputs.
for (int i = 0; i < noAddressBits; i++) {
variables[i] = new Variable("a" + i, Boolean.class);
syntax.add(variables[i]);
}
// Add data inputs.
for (int i = noAddressBits; i < noInputBits; i++) {
variables[i] = new Variable("d" + i, Boolean.class);
syntax.add(variables[i]);
}
setSyntax(syntax);
}
/**
* Calculates the fitness score for the given program. The fitness of a
* program for the majority problem is calculated by evaluating it
* using each of the possible sets of input values. There are
* <code>2^noInputBits</code> possible sets of inputs. The fitness of the
* program is the quantity of those input sequences that the program
* returned an incorrect response for. That is, a fitness value of
* <code>0.0</code> indicates the program responded correctly for every
* possible set of input values.
*
* @param p {@inheritDoc}
* @return the calculated fitness for the given program.
*/
@Override
public double getFitness(final CandidateProgram p) {
final GPCandidateProgram program = (GPCandidateProgram) p;
double score = 0;
// Execute on all possible inputs.
for (final boolean[] in: inputValues) {
// Set the variables.
for (int i = 0; i < in.length; i++) {
variables[i].setValue(in[i]);
}
if ((Boolean) program.evaluate() == multiplex(in)) {
score++;
}
}
return inputValues.length - score;
}
/*
* Calculate and set the number of address and data bits.
*/
private void setBitSizes(final int noInputBits) {
noAddressBits = 1;
while (true) {
noDataBits = (int) Math.pow(2, noAddressBits);
if ((noAddressBits + noDataBits) == noInputBits) {
break;
}
noAddressBits++;
}
}
/*
* Calculate what the correct response should be for the given inputs.
*/
private Boolean multiplex(final boolean[] vars) {
// Calculate which data position to use.
int dataPosition = 0;
for (int i = 0; i < noAddressBits; i++) {
if (vars[i]) {
dataPosition += Math.pow(2, i);
}
}
return vars[noAddressBits + dataPosition];
}
@Override
public Class<?> getReturnType() {
return Boolean.class;
}
public static void main(String[] args) {
new Multiplexer(6).run();
}
}