gpt4 book ai didi

java - 将 main() 添加到 Java 类时出现问题

转载 作者:行者123 更新时间:2023-12-01 18:47:05 25 4
gpt4 key购买 nike

Java 新手,似乎无法在 Google 上找到这个简单问题的正确答案。

我正在使用遗传编程框架进行一些研究,并且一直在尝试根据我的需要定制他们的示例。定义主要功能是留给用户的,所以这就是我想要做的。

我正在尝试创建一个预先存在的类,使其成为可运行的文件。它扩展了更高级别的类来调用其他功能。

这是我遇到问题的代码(来自内置示例):

public class Multiplexer extends GPModel {
public Multiplexer(final int noInputBits) {
...
}

...
public static void main(String[] args) {
new Multiplexer(6).run();
}
}

这是我看到的错误。我使用 NetBeans 7.3.1 和 JDK 1.7。

Error: Main method not found in class org.epochx.gp.model.Multiplexer, please define the main method as: public static void main(String[] args) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

这应该是相当微不足道的......我已经创建了具有 main 函数的其他类,并且没有遇到这个问题......它似乎只存在于框架内定义的预先存在的类中这个问题。

<小时/>

编辑

抱歉,封装类已经是公共(public)的,忘了提及。

<小时/>

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();
}
}

最佳答案

我尝试了你的代码,对我来说很有效。尝试清理和构建...或者更轻松地创建一个新类 Test 并将 main 放在那里。

关于java - 将 main() 添加到 Java 类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com