Skip to content

Commit

Permalink
new experimets for FDS
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuri82 committed Mar 26, 2018
1 parent c3b4917 commit 56b4970
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 1 deletion.
163 changes: 163 additions & 0 deletions experiments/src/main/kotlin/org/evomaster/experiments/pair/Main.kt
@@ -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
}
}
}
@@ -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())
}

}
@@ -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()
}
}
@@ -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()
}
}
@@ -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
}
@@ -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
}
}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -89,7 +89,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.0</kotlin.version>
<kotlin.version>1.2.30</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<junit.jupiter.version>5.0.2</junit.jupiter.version>
<junit.platform.version>1.0.2</junit.platform.version>
Expand Down

0 comments on commit 56b4970

Please sign in to comment.