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
Showing
7 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
experiments/src/main/kotlin/org/evomaster/experiments/pair/Main.kt
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,163 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import com.google.inject.Injector | ||
import com.google.inject.Key | ||
import com.google.inject.TypeLiteral | ||
import com.netflix.governator.guice.LifecycleInjector | ||
import com.netflix.governator.lifecycle.LifecycleManager | ||
import org.evomaster.core.BaseModule | ||
import org.evomaster.core.EMConfig | ||
import org.evomaster.core.EMConfig.Algorithm.* | ||
import org.evomaster.core.EMConfig.FeedbackDirectedSampling.NONE | ||
import org.evomaster.core.search.algorithms.MioAlgorithm | ||
import org.evomaster.core.search.algorithms.MosaAlgorithm | ||
import org.evomaster.core.search.algorithms.RandomAlgorithm | ||
import org.evomaster.core.search.algorithms.WtsAlgorithm | ||
import org.evomaster.core.search.service.SearchAlgorithm | ||
import java.util.* | ||
|
||
class Main { | ||
|
||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
infeasible() | ||
} | ||
|
||
|
||
private fun infeasible() { | ||
|
||
printHeader() | ||
|
||
val budget = 10_000 | ||
val range = 10_000 | ||
val nTargetsPerVar = 50 | ||
val T_BOTH = "both" | ||
val T_ONLY_X = "onlyX" | ||
val types = listOf(T_BOTH, T_ONLY_X) | ||
val repetitions = 100 //FIXME | ||
|
||
for (seed in 0 until repetitions) { | ||
|
||
for (t in types) { | ||
|
||
for (inf in listOf(1, 2, 3, 4, 5, 10, 20, 30, 40, 50)) { | ||
// for (inf in listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { | ||
|
||
var optimaX: MutableList<Int> | ||
var optimaY: MutableList<Int> | ||
|
||
if (t == T_BOTH) { | ||
val xInf = Math.ceil(inf / 2.0).toInt() | ||
val yInf = Math.floor(inf / 2.0).toInt() | ||
|
||
optimaX = createOptima(nTargetsPerVar - xInf, range, seed.toLong()) | ||
.apply { addAll(createInfeasible(xInf)) } | ||
optimaY = createOptima(nTargetsPerVar - yInf, range, seed.toLong()) | ||
.apply { addAll(createInfeasible(yInf)) } | ||
} else { | ||
optimaX = createOptima(nTargetsPerVar - inf, range, seed.toLong()) | ||
.apply { addAll(createInfeasible(inf)) } | ||
optimaY = createOptima(nTargetsPerVar, range, seed.toLong()) | ||
} | ||
|
||
assert(optimaX.size + optimaY.size == nTargetsPerVar * 2) | ||
|
||
listOf(RANDOM, MOSA, WTS).forEach { a -> | ||
runAlg(a, seed.toLong(), budget, range, optimaX, optimaY, inf, NONE, t) | ||
} | ||
|
||
for(fds in EMConfig.FeedbackDirectedSampling.values()) { | ||
runAlg(MIO, seed.toLong(), budget, range, optimaX, optimaY, inf, fds, t) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun runAlg(alg: EMConfig.Algorithm, seed: Long, budget: Int, range: Int, | ||
optimaX: List<Int>, optimaY: List<Int>, | ||
infeasible: Int, | ||
fds: EMConfig.FeedbackDirectedSampling, | ||
type: String) { | ||
|
||
val a = getAlg(alg, seed, budget, range, optimaX, optimaY, fds) | ||
|
||
val manager = a.first.getInstance(LifecycleManager::class.java) | ||
|
||
manager.start() | ||
val sol = a.second.search() | ||
manager.close() | ||
|
||
val size = optimaX.size + optimaY.size | ||
val covered = sol.overall.coveredTargets() | ||
val cov = 100.0 * (covered.toDouble() / size.toDouble()) | ||
|
||
println("${a.second.getType()},$cov,$size,$range,$budget,$type,$infeasible,$fds") | ||
} | ||
|
||
private fun printHeader() { | ||
println("algorithm,coverage,n,range,budget,type,infeasible,fds") | ||
} | ||
|
||
|
||
private fun getAlg(algType: EMConfig.Algorithm, | ||
seed: Long, budget: Int, range: Int, | ||
optimaX: List<Int>, optimaY: List<Int>, | ||
fds: EMConfig.FeedbackDirectedSampling | ||
) | ||
: Pair<Injector, SearchAlgorithm<PairIndividual>> { | ||
|
||
val injector = LifecycleInjector.builder() | ||
.withModules(PairModule(), BaseModule(arrayOf("--showProgress=false"))) | ||
.build().createInjector() | ||
|
||
val config = injector.getInstance(EMConfig::class.java) | ||
// config.showProgress = false | ||
config.algorithm = algType | ||
config.seed = seed | ||
config.maxActionEvaluations = budget | ||
config.stoppingCriterion = EMConfig.StoppingCriterion.FITNESS_EVALUATIONS | ||
config.tournamentSize = 10 //as in MOSA paper | ||
|
||
config.feedbackDirectedSampling = fds | ||
|
||
val ppd = injector.getInstance(PairProblemDefinition::class.java) | ||
ppd.range = range | ||
ppd.optimaX.clear() | ||
ppd.optimaX.addAll(optimaX) | ||
ppd.optimaY.clear() | ||
ppd.optimaY.addAll(optimaY) | ||
|
||
|
||
val alg = when (algType) { | ||
MIO -> injector.getInstance(Key.get( | ||
object : TypeLiteral<MioAlgorithm<PairIndividual>>() {})) | ||
RANDOM -> injector.getInstance(Key.get( | ||
object : TypeLiteral<RandomAlgorithm<PairIndividual>>() {})) | ||
MOSA -> injector.getInstance(Key.get( | ||
object : TypeLiteral<MosaAlgorithm<PairIndividual>>() {})) | ||
WTS -> injector.getInstance(Key.get( | ||
object : TypeLiteral<WtsAlgorithm<PairIndividual>>() {})) | ||
} | ||
return Pair(injector, alg) | ||
} | ||
|
||
|
||
private fun createInfeasible(n: Int): MutableList<Int> { | ||
return MutableList(n) { -1 } | ||
} | ||
|
||
private fun createOptima(n: Int, range: Int, seed: Long): MutableList<Int> { | ||
val optima = mutableListOf<Int>() | ||
val rand = Random(seed) | ||
(1..n).forEach { | ||
optima.add(rand.nextInt(range + 1)) | ||
} | ||
|
||
return optima | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
experiments/src/main/kotlin/org/evomaster/experiments/pair/PairFitness.kt
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,34 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import com.google.inject.Inject | ||
import org.evomaster.core.search.EvaluatedIndividual | ||
import org.evomaster.core.search.FitnessValue | ||
import org.evomaster.core.search.service.FitnessFunction | ||
import java.util.stream.Stream | ||
|
||
|
||
class PairFitness : FitnessFunction<PairIndividual>() { | ||
|
||
@Inject | ||
lateinit var ppd: PairProblemDefinition | ||
|
||
|
||
override fun doCalculateCoverage(individual: PairIndividual): EvaluatedIndividual<PairIndividual>? { | ||
|
||
val fv = FitnessValue(individual.size().toDouble()) | ||
|
||
val x = individual.x.value | ||
val y = individual.y.value | ||
|
||
var id = 0 | ||
|
||
Stream.concat( | ||
ppd.optimaX.stream().map { Math.abs(it - x) }, | ||
ppd.optimaY.stream().map { Math.abs(it - y) }) | ||
.map { 1.0 / (it + 1.0) } | ||
.forEach { h -> fv.updateTarget(id++, h) } | ||
|
||
return EvaluatedIndividual(fv, individual.copy() as PairIndividual, listOf()) | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
experiments/src/main/kotlin/org/evomaster/experiments/pair/PairIndividual.kt
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,26 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import org.evomaster.core.search.Action | ||
import org.evomaster.core.search.Individual | ||
import org.evomaster.core.search.gene.Gene | ||
import org.evomaster.core.search.gene.IntegerGene | ||
|
||
|
||
class PairIndividual(val x: IntegerGene, val y: IntegerGene) : Individual() { | ||
|
||
override fun copy(): Individual { | ||
return PairIndividual(x.copy() as IntegerGene, y.copy() as IntegerGene) | ||
} | ||
|
||
override fun seeGenes(): List<out Gene> { | ||
return listOf(x,y) | ||
} | ||
|
||
override fun size(): Int { | ||
return 1 | ||
} | ||
|
||
override fun seeActions(): List<out Action> { | ||
return listOf() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
experiments/src/main/kotlin/org/evomaster/experiments/pair/PairModule.kt
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,35 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import com.google.inject.AbstractModule | ||
import com.google.inject.TypeLiteral | ||
import org.evomaster.core.search.mutator.EmptyStructureMutator | ||
import org.evomaster.core.search.mutator.StandardMutator | ||
import org.evomaster.core.search.service.* | ||
|
||
|
||
class PairModule : AbstractModule(){ | ||
|
||
override fun configure() { | ||
bind(object : TypeLiteral<Sampler<PairIndividual>>() {}) | ||
.to(PairSampler::class.java) | ||
.asEagerSingleton() | ||
|
||
bind(object : TypeLiteral<FitnessFunction<PairIndividual>>() {}) | ||
.to(PairFitness::class.java) | ||
.asEagerSingleton() | ||
|
||
bind(object : TypeLiteral<Mutator<PairIndividual>>() {}) | ||
.to(object : TypeLiteral<StandardMutator<PairIndividual>>() {}) | ||
.asEagerSingleton() | ||
|
||
bind(object : TypeLiteral<Archive<PairIndividual>>() {}) | ||
.asEagerSingleton() | ||
|
||
bind(PairProblemDefinition::class.java) | ||
.asEagerSingleton() | ||
|
||
bind(StructureMutator::class.java) | ||
.to(EmptyStructureMutator::class.java) | ||
.asEagerSingleton() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
experiments/src/main/kotlin/org/evomaster/experiments/pair/PairProblemDefinition.kt
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,18 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import com.google.inject.Inject | ||
import org.evomaster.core.search.service.Randomness | ||
|
||
|
||
class PairProblemDefinition { | ||
|
||
|
||
@Inject | ||
lateinit var randomness : Randomness | ||
|
||
val optimaX: MutableList<Int> = mutableListOf() | ||
|
||
val optimaY: MutableList<Int> = mutableListOf() | ||
|
||
var range = 1000 | ||
} |
24 changes: 24 additions & 0 deletions
24
experiments/src/main/kotlin/org/evomaster/experiments/pair/PairSampler.kt
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,24 @@ | ||
package org.evomaster.experiments.pair | ||
|
||
import com.google.inject.Inject | ||
import org.evomaster.core.search.gene.IntegerGene | ||
import org.evomaster.core.search.service.Sampler | ||
|
||
|
||
class PairSampler : Sampler<PairIndividual>(){ | ||
|
||
@Inject | ||
lateinit var lpd : PairProblemDefinition | ||
|
||
|
||
override fun sampleAtRandom(): PairIndividual { | ||
|
||
val ind = PairIndividual( | ||
IntegerGene("x", 0, 0, lpd.range), | ||
IntegerGene("y", 0, 0, lpd.range) | ||
) | ||
ind.seeGenes().forEach { g -> g.randomize(randomness, false) } | ||
|
||
return ind | ||
} | ||
} |
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