forked from StefanoPenazzi/chameleonQuant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
091101a
commit 278c965
Showing
26 changed files
with
792 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* | ||
*/ | ||
package or.model; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.function.Function; | ||
|
||
import data.source.internal.timeseries.TimeSeriesI; | ||
import io.jenetics.Chromosome; | ||
import io.jenetics.Genotype; | ||
import io.jenetics.IntegerChromosome; | ||
import io.jenetics.IntegerGene; | ||
import io.jenetics.Phenotype; | ||
import io.jenetics.engine.Constraint; | ||
import strategies.SingleMovingAverageCrossoverStrategy; | ||
|
||
/** | ||
* @author stefanopenazzi | ||
* | ||
*/ | ||
public abstract class SMASJeneticModel<T extends SingleMovingAverageCrossoverStrategy> extends StrategyJeneticModelAbstract<T> { | ||
|
||
private TimeSeriesI ts; | ||
|
||
public SMASJeneticModel(Class<T> strategyC,TimeSeriesI ts) { | ||
super(strategyC); | ||
this.ts = ts; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public Function<Genotype,Double> getFitnessFunction() { | ||
|
||
Function<Genotype,Double> res = gt -> { | ||
IntegerChromosome length = (IntegerChromosome)gt.get(0); | ||
T strategy = null; | ||
try { | ||
strategy = (T) this.strategyC.getConstructor(TimeSeriesI.class,String.class,Integer.TYPE,Integer.TYPE).newInstance(this.ts,"close",length.intValue(),0); | ||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ||
| NoSuchMethodException | SecurityException e) { | ||
e.printStackTrace(); | ||
} | ||
strategy.run(); | ||
return fitnessFunctionDesign(strategy); | ||
|
||
}; | ||
return res; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public abstract Boolean isValid(final Genotype gt); | ||
|
||
@SuppressWarnings("rawtypes") | ||
public abstract Genotype repairGenotype(final Genotype gt); | ||
|
||
public abstract double fitnessFunctionDesign(T smacs); | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
@Override | ||
public Genotype getEncoding() { | ||
return Genotype.of((Chromosome)IntegerChromosome.of(1, 150, 1)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Constraint getConstraint(){ | ||
return new RepairingConstraint(); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private class RepairingConstraint implements Constraint{ | ||
|
||
@Override | ||
public boolean test(Phenotype pt) { | ||
return isValid(pt.genotype()); | ||
} | ||
|
||
@Override | ||
public Phenotype repair(Phenotype pt, long generation) { | ||
return Phenotype.of(repairGenotype(pt.genotype()),generation); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* | ||
*/ | ||
package or.model; | ||
|
||
import data.source.internal.timeseries.TimeSeriesI; | ||
import io.jenetics.Genotype; | ||
import io.jenetics.IntegerChromosome; | ||
import io.jenetics.IntegerGene; | ||
import strategies.SingleMovingAverageCrossoverStrategy; | ||
|
||
/** | ||
* @author stefanopenazzi | ||
* | ||
*/ | ||
public class SSMASJeneticModel extends SMASJeneticModel { | ||
|
||
/** | ||
* @param strategyC | ||
* @param ts | ||
*/ | ||
public SSMASJeneticModel(Class strategyC, TimeSeriesI ts) { | ||
super(strategyC, ts); | ||
} | ||
|
||
@Override | ||
public double fitnessFunctionDesign(SingleMovingAverageCrossoverStrategy smacs) { | ||
return smacs.getTotNetProfit(); | ||
} | ||
|
||
@Override | ||
public Boolean isValid(Genotype gt) { | ||
return ((IntegerChromosome)gt.get(0)).intValue() > 0? true : false; | ||
} | ||
|
||
@Override | ||
public Genotype<IntegerGene> repairGenotype(Genotype gt) { | ||
return Genotype.of(IntegerChromosome.of(1, 200, 1)); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* | ||
*/ | ||
package or.model; | ||
|
||
import java.util.function.Function; | ||
|
||
import io.jenetics.Genotype; | ||
import io.jenetics.engine.Constraint; | ||
import strategies.StrategyI; | ||
|
||
/** | ||
* @author stefanopenazzi | ||
* | ||
*/ | ||
public abstract class StrategyJeneticModelAbstract <T extends StrategyI> implements ModelI { | ||
|
||
protected final Class<? extends StrategyI> strategyC; | ||
|
||
public StrategyJeneticModelAbstract(Class<? extends StrategyI> strategyC) { | ||
this.strategyC = strategyC; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public abstract Function<Genotype,Double> getFitnessFunction(); | ||
|
||
@SuppressWarnings("rawtypes") | ||
public abstract Genotype getEncoding(); | ||
|
||
public abstract Constraint <?,?> getConstraint(); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* | ||
*/ | ||
package or.model; | ||
|
||
/** | ||
* @author stefanopenazzi | ||
* | ||
*/ | ||
|
||
import io.jenetics.BitChromosome; | ||
import io.jenetics.BitGene; | ||
import io.jenetics.Genotype; | ||
import io.jenetics.RouletteWheelSelector; | ||
import io.jenetics.TournamentSelector; | ||
import io.jenetics.engine.Engine; | ||
import io.jenetics.engine.EvolutionResult; | ||
import io.jenetics.util.Factory; | ||
|
||
public class TestJeneticsSolver { | ||
|
||
// 2.) Definition of the fitness function. | ||
private static Integer eval(Genotype<BitGene> gt) { | ||
return gt.chromosome() | ||
.as(BitChromosome.class) | ||
.bitCount(); | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
// 1.) Define the genotype (factory) suitable | ||
// for the problem. | ||
|
||
/* A chromosome is an array of Genes. A Genotype is an array of chromosome | ||
* the chromosomes in the Genotype do not necessarily have the same size. | ||
* Still unclear how this works considering that the genes are the variables of | ||
* the model(a vector not a matrix). All the chromosomes must have the same genes' type */ | ||
|
||
Factory<Genotype<BitGene>> gtf = | ||
Genotype.of(BitChromosome.of(50, 0.5)); | ||
|
||
// 3.) Create the execution environment. | ||
Engine<BitGene, Integer> engine = Engine | ||
.builder(TestJeneticsSolver::eval, gtf) | ||
//Selection for crossover and mutation follows | ||
.survivorsSelector(new RouletteWheelSelector<>()) //Strongest individual are selected and avoid the crossover | ||
.offspringSelector(new TournamentSelector<>()) //Crossover | ||
.offspringFraction(0.7) | ||
.populationSize(50) | ||
//when the candidates are selected it is necessary specify how they crossover and how they mutate | ||
.build(); | ||
|
||
// 4.) Start the execution (evolution) and | ||
// collect the result. | ||
Genotype<BitGene> result = engine.stream() | ||
.limit(100) | ||
.collect(EvolutionResult.toBestGenotype()); | ||
|
||
System.out.println("Hello World:\n" + result); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
/* | ||
* CROSSOVER | ||
* The crossover operator is the process in which individuals from the | ||
* population trade genetic information, hopefully to create a new individual | ||
* which contains the best parts from its parents’ genomes. During crossover | ||
* each individual in the population is considered for crossover; this is where | ||
* the crossover rate parameter is used. By comparing the crossover rate to a | ||
* random number, we can decide whether the individual should have crossover | ||
* applied to it, or whether it should be added straight into the next | ||
* population unaffected by crossover. If an individual is selected for | ||
* crossover then a second parent needs be found. To find the second parent, we | ||
* need to pick one of many possible selection methods. | ||
*/ |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.