Skip to content

Instantly share code, notes, and snippets.

View jenetics's full-sized avatar
🙄

Franz Wilhelmstötter jenetics

🙄
View GitHub Profile
@jenetics
jenetics / Lifecycle.java
Created March 9, 2023 20:58
Guaranteed closing of several resources (AutoCloseable)
/*
* Copyright (c) 2023 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import io.jenetics.DoubleGene;
import io.jenetics.EnumGene;
import io.jenetics.Genotype;
import io.jenetics.engine.Codec;
import io.jenetics.engine.Codecs;
import io.jenetics.engine.Engine;
import io.jenetics.engine.EvolutionResult;
import io.jenetics.util.DoubleRange;
import io.jenetics.util.ProxySorter;
@jenetics
jenetics / CodecMapping.java
Last active January 13, 2020 20:19
Codec for creating a split double range
/*
* This codec creates a split range, where only values between [0, 2) and
* [8, 10) are valid. The valid range is marked with '-' and the invalid
* range with 'x'.
*
* +--+--+--+--+--+--+--+--+--+--+
* | | | | | | | | | | |
* 0 1 2 3 4 5 6 7 8 9 10
* |-----|xxxxxxxxxxxxxxxxx|-----|
* ^ |llllllll|rrrrrrrr| ^
@jenetics
jenetics / ConstrainedSampleGA.java
Created January 10, 2020 17:03
Structure of a more complex GA problem
import static java.util.Collections.emptyList;
import static io.jenetics.engine.EvolutionResult.toBestPhenotype;
import java.util.List;
import java.util.function.Function;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
import io.jenetics.DoubleChromosome;
import io.jenetics.DoubleGene;
@jenetics
jenetics / Point3.java
Created November 22, 2019 21:30
Implementation of 3D-Point usable for Multi-objective optimization
import static java.lang.String.format;
import java.util.Comparator;
/**
* Sample implementation of a 3D double point.
*/
public final class Point3 implements Vec<Point3> {
private final double _x;
private final double _y;
void sortStringArray() {
final String[] strings = new String[]{"g", "f", "r", "u", "a"};
final int[] proxy = ProxySorter.sort(
strings, strings.length,
(a, i, j) -> a[i].compareTo(a[j])
);
// Printing strings in ascending order.
for (int index : proxy) {
System.out.println(strings[index]);
@jenetics
jenetics / PolinomialCodec.java
Last active September 26, 2019 17:50
Polynomial codec
final class Polynomial {
private final double[] a;
Polynomial(final double... a) {
this.a = a.clone();
}
// Uses Horner's Method to evaluate the polynomial.
double apply(final double x) {
double result = a[a.length - 1];
final Error error = Error.of(
LossFunction::mse,
Complexity.ofMaxNodeCount(50)
);
private static final Regression<Double> REGRESSION = Regression.of(
Regression.codecOf(OPERATIONS, TERMINALS, 5),
Error.of(LossFunction::mse),
Sample.ofDouble(-1.0, -8.0000),
// ...
Sample.ofDouble(0.9, 1.3860),
Sample.ofDouble(1.0, 2.0000)
);
public static void main(final String[] args) {