Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to find two optimal parameters with jenetics ? #167

Closed
ZUOJIANYI opened this issue Feb 10, 2017 · 3 comments
Closed

how to find two optimal parameters with jenetics ? #167

ZUOJIANYI opened this issue Feb 10, 2017 · 3 comments
Labels

Comments

@ZUOJIANYI
Copy link

No description provided.

@ZUOJIANYI
Copy link
Author

here the code, can anyone help me? thanks
`
public double fitness(double C, double gamma) {

    problem = scaler.scale(runoff, rain, evap, rbye, rde, doub_rain, doub_evap, doub_rainbye, doub_evapbyr, triple_rain, triple_evap);
    param.svm_type = SvmParameter.EPSILON_SVR;
    param.kernel_type = SvmParameter.RBF;
    param.cache_size = 500;
    param.eps = 0.01;
    param.C = Math.pow(2,C);
    param.gamma = Math.pow(2,gamma);


    CrossValidation cv = new CrossValidation();
    cv.doCrossValidation(problem,param,5);

    return cv.getCVSCC();
}

public void testGA(){

    final Engine<DoubleGene,Double> engine = Engine
            .builder(GetMonthDataForQPE::fitness,
                    codecs.ofScalar(DoubleRange.of(-8.0,8.0)),codecs.ofScalar(DoubleRange.of(-8.0,8.0)))
            .populationSize(500)
            .optimize(Optimize.MAXIMUM)
            .alterers(
                    new Mutator<>(0.03),
                    new MeanAlterer(0.6)
            )
            .build();


    // Create evolution statistics consumer.
    final EvolutionStatistics<Double, ?>
            statistics = EvolutionStatistics.ofNumber();

    final Phenotype<DoubleGene, Double> best = engine.stream()
            // Truncate the evolution stream after 7 "steady"
            // generations.
            .limit(bySteadyFitness(7))
            // The evolution will stop after maximal 100
            // generations.
            .limit(100)
            // Update the evaluation statistics after
            // each generation
            .peek(statistics)
            // Collect (reduce) the evolution stream to
            // its best phenotype.
            .collect(toBestPhenotype());

    System.out.println(statistics);
    System.out.println(best);

}

`

@ZUOJIANYI
Copy link
Author

In the mthond 'testGA', i got the error with Engine.builder:
Cannot resolve method 'builder(, org.jenetics.engine.Codec<java.lang.Double,org.jenetics.DoubleGene>, org.jenetics.engine.Codec<java.lang.Double,org.jenetics.DoubleGene>)'

@jenetics
Copy link
Owner

The main problem with your code was, that the fitness function can only take one parameter. If you want to have more, you have to wrap it into an array:

static double fitness(final double[] params) {
    final double C = params[0];
    final double gamma = params[1];

    // Your fitness calculation.
    return C + gamma;
}

The fixed program would look like this:

public class GetMonthDataForQPE {

    static double fitness(final double[] params) {
        final double C = params[0];
        final double gamma = params[1];
    
        // Your fitness calculation.
        return C + gamma;
    }

    public static void main(final String[] args) {
        codecs.ofVector(DoubleRange.of(-8.0, 8.0), 2);

        final Engine<DoubleGene, Double> engine = Engine
            .builder(
                GetMonthDataForQPE::fitness,
                codecs.ofVector(DoubleRange.of(-8.0, 8.0), 2))
            .populationSize(500)
            .optimize(Optimize.MAXIMUM)
            .alterers(
                    new Mutator<>(0.03),
                    new MeanAlterer<>(0.6)
                )
            .build();


        // Create evolution statistics consumer.
        final EvolutionStatistics<Double, ?>
            statistics = EvolutionStatistics.ofNumber();

        final Phenotype<DoubleGene, Double> best = engine.stream()
            .limit(bySteadyFitness(7))
            .limit(100)
            .peek(statistics)
            .collect(toBestPhenotype());

        System.out.println(statistics);
        System.out.println(best);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants