Skip to content

Commit

Permalink
Reduced overhead of constraint consultation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Marte committed Jun 1, 2019
1 parent b9a2c90 commit 7c42d53
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/yuck/constraints/BinPacking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ final class BinPacking

override def consult(before: SearchState, after: SearchState, move: Move) = {
loadDeltas.clear
for (x <- move.involvedVariables) {
for (x <- move) {
val item = x2Item(x)
val j = before.value(item.bin).value
val k = after.value(item.bin).value
Expand Down
2 changes: 1 addition & 1 deletion src/main/yuck/constraints/CountConst.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class CountConst

private def computeDelta(before: SearchState, after: SearchState, move: Move): Int = {
var delta = 0
for (x <- move.involvedVariables) {
for (x <- move) {
if (before.anyValue(x) == a && after.anyValue(x) != a) {
delta -= 1
} else if (before.anyValue(x) != a && after.anyValue(x) == a) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/yuck/constraints/Disjunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class Disjunction
override def consult(before: SearchState, after: SearchState, move: Move) = {
futureSum = sum
futureTrueCount = trueCount
for (x <- move.involvedVariables) {
for (x <- move) {
val y = x.asInstanceOf[BooleanVariable]
val a = before.value(y).violation
val b = after.value(y).violation
Expand Down
2 changes: 1 addition & 1 deletion src/main/yuck/constraints/DistributionMaintainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class DistributionMaintainer
Nil

override def commit(before: SearchState, after: SearchState, move: Move) = {
for (x <- move.involvedVariables) {
for (x <- move) {
val (i, ax) = indexMap(x)
distribution.setFrequency(i, computeFrequency(ax, after))
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/yuck/constraints/IntegerTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ final class IntegerTable
val xs = move.involvedVariables.toIterator
if (hasDuplicateVariables) xs.map(x2is).flatten else xs.map(x2i)
}
for (i <- is) {
while (is.hasNext) {
val i = is.next
val x = xs(i)
val a = before.value(x).value
val b = after.value(x).value
Expand Down
4 changes: 2 additions & 2 deletions src/main/yuck/constraints/Inverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final class Inverse
}
}
futureCosts = currentCosts
for (x <- move.involvedVariables) {
for (x <- move) {
val maybeI = f.x2i.get(x)
if (maybeI.isDefined) {
val i = maybeI.get
Expand Down Expand Up @@ -181,7 +181,7 @@ final class Inverse
}

override def commit(before: SearchState, after: SearchState, move: Move) = {
for (x <- move.involvedVariables) {
for (x <- move) {
val maybeI = f.x2i.get(x)
if (maybeI.isDefined) {
val i = maybeI.get
Expand Down
8 changes: 4 additions & 4 deletions src/main/yuck/constraints/LinearCombination.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class LinearCombination
override def inVariables = axs.toIterator.map(_.x)
override def outVariables = List(y)

private val id2ax = immutable.HashMap[AnyVariable, AX[Value]]() ++ (axs.toIterator.map(_.x).zip(axs.toIterator))
private val x2ax = immutable.HashMap[AnyVariable, AX[Value]]() ++ (axs.toIterator.map(_.x).zip(axs.toIterator))
private var sum = valueTraits.zero
private val effects = List(new ReusableEffectWithFixedVariable[Value](y))
private val effect = effects.head
Expand All @@ -38,7 +38,7 @@ final class LinearCombination

override def initialize(now: SearchState) = {
sum = valueTraits.zero
for ((_, ax) <- id2ax) {
for ((_, ax) <- x2ax) {
sum += ax.a * now.value(ax.x)
}
effect.a = sum
Expand All @@ -47,8 +47,8 @@ final class LinearCombination

override def consult(before: SearchState, after: SearchState, move: Move) = {
effect.a = sum
for (x <- move.involvedVariables) {
val ax = id2ax.get(x).get
for (x0 <- move) {
val ax = x2ax(x0)
effect.a = effect.a.addAndSub(ax.a, after.value(ax.x), before.value(ax.x))
}
effects
Expand Down
12 changes: 4 additions & 8 deletions src/main/yuck/constraints/LinearConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,12 @@ final class LinearConstraint

override def consult(before: SearchState, after: SearchState, move: Move) = {
futureSum = currentSum
if (as == null) {
for (x0 <- move.involvedVariables) {
if (x0 != z) {
for (x0 <- move) {
if (x0 != z) {
if (as == null) {
val x = valueTraits.safeDowncast(x0)
futureSum = futureSum.addAndSub(after.value(x), before.value(x))
}
}
} else {
for (x0 <- move.involvedVariables) {
if (x0 != z) {
} else {
val i = x2i(x0)
val x = xs(i)
futureSum = futureSum.addAndSub(as(i), after.value(x), before.value(x))
Expand Down
6 changes: 3 additions & 3 deletions src/main/yuck/constraints/Sum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ final class Sum

override def consult(before: SearchState, after: SearchState, move: Move) = {
effect.a = sum
for (x <- move.involvedVariables) {
val y = valueTraits.safeDowncast(x)
effect.a = effect.a.addAndSub(after.value(y), before.value(y))
for (x0 <- move) {
val x = valueTraits.safeDowncast(x0)
effect.a = effect.a.addAndSub(after.value(x), before.value(x))
}
effects
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/yuck/constraints/ValueFrequencyTracker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ abstract class ValueFrequencyTracker

override def consult(before: SearchState, after: SearchState, move: Move) = {
futureValueRegistry = valueRegistry
for (x <- todo(move)) {
val y = valueTraits.safeDowncast(x)
val n = variableRegistry(x)
val it = todo(move).toIterator
while (it.hasNext) {
val x0 = it.next
val x = valueTraits.safeDowncast(x0)
val n = variableRegistry(x0)
futureValueRegistry =
registerValue(deregisterValue(futureValueRegistry, before.value(y), n), after.value(y), n)
registerValue(deregisterValue(futureValueRegistry, before.value(x), n), after.value(x), n)
}
effect.a = computeResult(after, futureValueRegistry)
effects
Expand Down
1 change: 1 addition & 0 deletions src/main/yuck/core/BulkMove.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class BulkMove(id: Id[Move]) extends Move(id) {

override def isEmpty = effectDir.isEmpty
override def effects = effectDir.valuesIterator
override def foreach[U](f: AnyVariable => U) = effectDir.foreachKey(f)
override def size = effectDir.size
override def involvedVariables = effectDir.keysIterator
override def involves(x: AnyVariable) = effectDir.contains(x)
Expand Down
9 changes: 6 additions & 3 deletions src/main/yuck/core/Move.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package yuck.core
*
* @author Michael Marte
*/
abstract class Move(val id: Id[Move]) extends Ordered[Move] {
abstract class Move(val id: Id[Move]) extends Ordered[Move] with Traversable[AnyVariable] {

@inline final override def hashCode = id.hashCode
override def toString = effects.toList.sortBy(_.anyVariable.id).mkString(", ")
Expand All @@ -15,10 +15,13 @@ abstract class Move(val id: Id[Move]) extends Ordered[Move] {
def effects: TraversableOnce[AnyEffect]

/** Returns true iff the move does not involve any variable. */
def isEmpty: Boolean = effects.isEmpty
override def isEmpty: Boolean = effects.isEmpty

/** Returns the number of variables involved in the move. */
def size: Int = effects.size
override def size: Int = effects.size

/** Iterates the variables of the move. */
override def foreach[U](f: AnyVariable => U) = involvedVariables.foreach(f)

/** Returns the variables involved in the move. */
def involvedVariables: TraversableOnce[AnyVariable] = effects.toIterator.map(_.anyVariable)
Expand Down

0 comments on commit 7c42d53

Please sign in to comment.