Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.github.ericmedvet.jgea.experimenter.builders.Mappers Maven / Gradle / Ivy
/*-
* ========================LICENSE_START=================================
* jgea-experimenter
* %%
* Copyright (C) 2018 - 2023 Eric Medvet
* %%
* 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
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package io.github.ericmedvet.jgea.experimenter.builders;
import io.github.ericmedvet.jgea.core.representation.NamedMultivariateRealFunction;
import io.github.ericmedvet.jgea.core.representation.NamedUnivariateRealFunction;
import io.github.ericmedvet.jgea.core.representation.grammar.Chooser;
import io.github.ericmedvet.jgea.core.representation.grammar.Developer;
import io.github.ericmedvet.jgea.core.representation.grammar.grid.*;
import io.github.ericmedvet.jgea.core.representation.graph.Graph;
import io.github.ericmedvet.jgea.core.representation.graph.Node;
import io.github.ericmedvet.jgea.core.representation.graph.numeric.functiongraph.FunctionGraph;
import io.github.ericmedvet.jgea.core.representation.graph.numeric.operatorgraph.OperatorGraph;
import io.github.ericmedvet.jgea.core.representation.sequence.bit.BitString;
import io.github.ericmedvet.jgea.core.representation.sequence.integer.IntString;
import io.github.ericmedvet.jgea.core.representation.tree.Tree;
import io.github.ericmedvet.jgea.core.representation.tree.numeric.Element;
import io.github.ericmedvet.jgea.core.representation.tree.numeric.TreeBasedMultivariateRealFunction;
import io.github.ericmedvet.jgea.core.representation.tree.numeric.TreeBasedUnivariateRealFunction;
import io.github.ericmedvet.jgea.experimenter.InvertibleMapper;
import io.github.ericmedvet.jnb.core.Param;
import io.github.ericmedvet.jsdynsym.buildable.builders.NumericalDynamicalSystems;
import io.github.ericmedvet.jsdynsym.core.NumericalParametrized;
import io.github.ericmedvet.jsdynsym.core.StatelessSystem;
import io.github.ericmedvet.jsdynsym.core.numerical.MultivariateRealFunction;
import io.github.ericmedvet.jsdynsym.core.numerical.ann.MultiLayerPerceptron;
import io.github.ericmedvet.jsdynsym.grid.Grid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
public class Mappers {
private Mappers() {}
@SuppressWarnings("unused")
public static InvertibleMapper> bitStringToGrammarGrid(
@Param("grammar") GridGrammar grammar,
@Param(value = "l", dI = 256) int l,
@Param(value = "overwrite") boolean overwrite,
@Param(
value = "criteria",
dSs = {"least_recent", "lowest_y", "lowest_x"})
List criteria) {
Developer, GridGrammar.ReferencedGrid> gridDeveloper =
new StandardGridDeveloper<>(grammar, overwrite, criteria);
return InvertibleMapper.from(
(eGrid, bs) -> {
Chooser> chooser = new BitStringChooser<>(bs, grammar);
return gridDeveloper.develop(chooser).orElse(eGrid);
},
eGrid -> new BitString(l));
}
@SuppressWarnings("unused")
public static InvertibleMapper compose(
@Param(value = "first") InvertibleMapper first,
@Param(value = "second") InvertibleMapper second) {
return first.andThen(second);
}
@SuppressWarnings("unused")
public static InvertibleMapper, Grid> doubleStringToGrammarGrid(
@Param("grammar") GridGrammar grammar,
@Param(value = "l", dI = 256) int l,
@Param(value = "overwrite") boolean overwrite,
@Param(
value = "criteria",
dSs = {"least_recent", "lowest_y", "lowest_x"})
List criteria) {
Developer, GridGrammar.ReferencedGrid> gridDeveloper =
new StandardGridDeveloper<>(grammar, overwrite, criteria);
return InvertibleMapper.from(
(eGrid, vs) -> {
Chooser> chooser = new DoublesChooser<>(vs, grammar);
return gridDeveloper.develop(chooser).orElse(eGrid);
},
eGrid -> Collections.nCopies(l, 0d));
}
@SuppressWarnings("unused")
public static InvertibleMapper, NamedMultivariateRealFunction> fGraphToMrf(
@Param(value = "postOperator", dS = "identity")
MultiLayerPerceptron.ActivationFunction postOperator) {
return InvertibleMapper.from(
(nmrf, g) -> new FunctionGraph(g, nmrf.xVarNames(), nmrf.yVarNames()),
nmrf -> FunctionGraph.sampleFor(nmrf.xVarNames(), nmrf.yVarNames()));
}
@SuppressWarnings("unused")
public static InvertibleMapper identity() {
return InvertibleMapper.identity();
}
@SuppressWarnings("unused")
public static InvertibleMapper> intStringToGrammarGrid(
@Param("grammar") GridGrammar grammar,
@Param(value = "upperBound", dI = 16) int upperBound,
@Param(value = "l", dI = 256) int l,
@Param(value = "overwrite") boolean overwrite,
@Param(
value = "criteria",
dSs = {"least_recent", "lowest_y", "lowest_x"})
List criteria) {
Developer, GridGrammar.ReferencedGrid> gridDeveloper =
new StandardGridDeveloper<>(grammar, overwrite, criteria);
return InvertibleMapper.from(
(eGrid, is) -> {
Chooser> chooser = new IntStringChooser<>(is, grammar);
return gridDeveloper.develop(chooser).orElse(eGrid);
},
eGrid -> new IntString(Collections.nCopies(l, 0), 0, upperBound));
}
@SuppressWarnings("unused")
public static InvertibleMapper, NamedMultivariateRealFunction> mlpToMrf(
@Param(value = "innerLayerRatio", dD = 0.65) double innerLayerRatio,
@Param(value = "nOfInnerLayers", dI = 1) int nOfInnerLayers,
@Param(value = "activationFunction", dS = "tanh")
MultiLayerPerceptron.ActivationFunction activationFunction) {
Function innerNeuronsFunction =
nmrf -> {
int[] innerNeurons = new int[nOfInnerLayers];
int centerSize = (int) Math.max(2, Math.round(nmrf.xVarNames().size() * innerLayerRatio));
if (nOfInnerLayers > 1) {
for (int i = 0; i < nOfInnerLayers / 2; i++) {
innerNeurons[i] =
nmrf.xVarNames().size()
+ (centerSize - nmrf.xVarNames().size()) / (nOfInnerLayers / 2 + 1) * (i + 1);
}
for (int i = nOfInnerLayers / 2; i < nOfInnerLayers; i++) {
innerNeurons[i] =
centerSize
+ (nmrf.yVarNames().size() - centerSize)
/ (nOfInnerLayers / 2 + 1)
* (i - nOfInnerLayers / 2);
}
} else if (nOfInnerLayers > 0) {
innerNeurons[0] = centerSize;
}
return innerNeurons;
};
return InvertibleMapper.from(
(nmrf, params) ->
NamedMultivariateRealFunction.from(
new MultiLayerPerceptron(
activationFunction,
nmrf.xVarNames().size(),
innerNeuronsFunction.apply(nmrf),
nmrf.yVarNames().size(),
params.stream().mapToDouble(v -> v).toArray()),
nmrf.xVarNames(),
nmrf.yVarNames()),
nmrf ->
Collections.nCopies(
new MultiLayerPerceptron(
activationFunction,
nmrf.xVarNames().size(),
innerNeuronsFunction.apply(nmrf),
nmrf.yVarNames().size())
.getParams()
.length,
0d));
}
@SuppressWarnings("unused")
public static
InvertibleMapper mrfToUrf() {
return InvertibleMapper.from(
(nurf, nmrf) -> NamedUnivariateRealFunction.from(nmrf), nurf -> nurf);
}
@SuppressWarnings("unused")
public static InvertibleMapper>, NamedMultivariateRealFunction>
multiSrTreeToMrf(
@Param(value = "postOperator", dS = "identity")
MultiLayerPerceptron.ActivationFunction postOperator) {
return InvertibleMapper.from(
(nmrf, ts) -> new TreeBasedMultivariateRealFunction(ts, nmrf.xVarNames(), nmrf.yVarNames()),
nmrf -> TreeBasedMultivariateRealFunction.sampleFor(nmrf.xVarNames(), nmrf.yVarNames()));
}
@SuppressWarnings("unused")
public static InvertibleMapper, NamedMultivariateRealFunction>
numericalParametrizedToMrf(
@Param("function")
NumericalDynamicalSystems.Builder
function) {
return InvertibleMapper.from(
(nmrf, params) -> {
if (nmrf instanceof NumericalParametrized) {
MultivariateRealFunction np = function.apply(nmrf.xVarNames(), nmrf.yVarNames());
((NumericalParametrized) np).setParams(params.stream().mapToDouble(v -> v).toArray());
return NamedMultivariateRealFunction.from(np, nmrf.xVarNames(), nmrf.yVarNames());
}
throw new IllegalArgumentException(
"The provided function is not numerical parametrized.");
},
nmrf -> {
if (nmrf instanceof NumericalParametrized parametrized) {
return Arrays.stream(parametrized.getParams()).boxed().toList();
}
throw new IllegalArgumentException(
"The provided function is not numerical parametrized.");
});
}
@SuppressWarnings("unused")
public static InvertibleMapper<
Graph, NamedMultivariateRealFunction>
oGraphToMrf(
@Param(value = "postOperator", dS = "identity")
MultiLayerPerceptron.ActivationFunction postOperator) {
return InvertibleMapper.from(
(nmrf, g) -> new OperatorGraph(g, nmrf.xVarNames(), nmrf.yVarNames()),
nmrf -> OperatorGraph.sampleFor(nmrf.xVarNames(), nmrf.yVarNames()));
}
@SuppressWarnings("unused")
public static InvertibleMapper, NamedUnivariateRealFunction> srTreeToUrf(
@Param(value = "postOperator", dS = "identity")
MultiLayerPerceptron.ActivationFunction postOperator) {
return InvertibleMapper.from(
(nurf, t) -> new TreeBasedUnivariateRealFunction(t, nurf.xVarNames(), nurf.yVarName()),
nurf -> TreeBasedUnivariateRealFunction.sampleFor(nurf.xVarNames(), nurf.yVarName()));
}
}