Skip to content

Instantly share code, notes, and snippets.

@mbuzdalov
Created September 7, 2017 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbuzdalov/b8517ccfc6cc9e403a2959eca5c403c3 to your computer and use it in GitHub Desktop.
Save mbuzdalov/b8517ccfc6cc9e403a2959eca5c403c3 to your computer and use it in GitHub Desktop.
Example with explicitly specified type for implicits
class Config(n: Int, k: Int, onePlusOne: Boolean, init: Int, rlAlgorithm: Option[Agent[Int, Int]])
extends OptConfiguration[BitSet, IndexedSeq[Int]]
{
require(n % k == 0)
private implicit val multiple: MultipleCriteria[IndexedSeq[Int]] = MultipleCriteria.fromIndexedSeqWithElementOrdering("XdivK", "OneMax")
private implicit val comparator: EARLCodomainComparator[IndexedSeq[Int]] = EARLCodomainComparator().fromMultipleCriteria(init)
private implicit val evaluator: Evaluator[BitSet, IndexedSeq[Int]] = Evaluator().usingFunction { v: BitSet =>
val oneMax = v.size
val xDivK = oneMax / k
IndexedSeq(xDivK, oneMax)
}
private implicit val initialization: Initialization[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Initialization().fromDomains(Some(BitSet.empty))
private implicit val termination: Termination.Pluggable[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Termination.Pluggable()
private implicit val selection: Selection[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Selection().all
private implicit val update: Update[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Update().best
private implicit val mutation: Mutation[BitSet, IndexedSeq[Int]] = Mutation().using(
if (onePlusOne) {
Mutation.Standard.BitSet.independentPointMutation(n)
} else {
Mutation.Standard.BitSet.singlePointMutation(n)
}
)
private implicit val iteration: Iteration[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Iteration().fromSelectionMutationEvaluateUpdate
private implicit val optimizer: Optimizer[BitSet, IndexedSeq[Int], IndexedSeqWorkingSet] = Optimizer().simple
private implicit val evaluationCount: EvaluationCount[BitSet, IndexedSeq[Int]] = new EvaluationCount()
CodomainThreshold().register(_(0), n / k)
EvaluationLimit().register(1000000)
rlAlgorithm match {
case Some(algo) =>
EARLConfiguration().registerOldWay(algo, v => v(0).output(0), (p, n) => n(0).output(0) - p(0).output(0))
case None =>
}
def run(): Double = {
optimizer() match {
case Optimizer.Result(_, CodomainThreshold) => evaluationCount()
case Optimizer.Result(_, EvaluationLimit) => Double.PositiveInfinity
case Optimizer.Result(_, _) => throw new AssertionError("Should not happen")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment