From fb2ad11347d9a0531447d759ec9b9921a16f1d05 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 19 Sep 2017 17:45:10 -0700 Subject: [PATCH 1/3] Improve AddressDecoder optimization function This function is better 27% of the time but worse 6% of the time. --- src/main/scala/diplomacy/AddressDecoder.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/scala/diplomacy/AddressDecoder.scala b/src/main/scala/diplomacy/AddressDecoder.scala index 1b2ed837..9000c7ab 100644 --- a/src/main/scala/diplomacy/AddressDecoder.scala +++ b/src/main/scala/diplomacy/AddressDecoder.scala @@ -70,10 +70,10 @@ object AddressDecoder def bitScore(partitions: Partitions): Seq[Int] = { val maxPortsPerPartition = partitions.map(_.size).max - val sumPortsPerPartition = partitions.map(_.size).sum val maxSetsPerPartition = partitions.map(_.map(_.size).sum).max - val sumSetsPerPartition = partitions.map(_.map(_.size).sum).sum - Seq(maxPortsPerPartition, sumPortsPerPartition, maxSetsPerPartition, sumSetsPerPartition) + val sumSquarePortsPerPartition = partitions.map(p => p.size * p.size).sum + val sumSquareSetsPerPartition = partitions.map(_.map(p => p.size * p.size).sum).max + Seq(maxPortsPerPartition, maxSetsPerPartition, sumSquarePortsPerPartition, sumSquareSetsPerPartition) } def partitionPort(port: Port, bit: BigInt): (Port, Port) = { From 72bd89a2af550c5bc33fb05a337c3b30dfb5b8af Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 19 Sep 2017 17:46:04 -0700 Subject: [PATCH 2/3] Add another AddressDecoder debug message --- src/main/scala/diplomacy/AddressDecoder.scala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/scala/diplomacy/AddressDecoder.scala b/src/main/scala/diplomacy/AddressDecoder.scala index 9000c7ab..5e54d173 100644 --- a/src/main/scala/diplomacy/AddressDecoder.scala +++ b/src/main/scala/diplomacy/AddressDecoder.scala @@ -123,6 +123,8 @@ object AddressDecoder val candidates = bits.map { bit => val result = partitionPartitions(partitions, bit) val score = bitScore(result) + if (debug) + println(" For bit %x, %s".format(bit, score.toString)) (score, bit, result) } val (bestScore, bestBit, bestPartitions) = candidates.min(Ordering.by[(Seq[Int], BigInt, Partitions), Iterable[Int]](_._1.toIterable)) From 87b92cb20697d986df60af066bb9405e429a6ffc Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 19 Sep 2017 17:46:18 -0700 Subject: [PATCH 3/3] Scan AddressDecoder bits left to right This heuristic is brittle but fixes deduplication in RocketTile. --- src/main/scala/diplomacy/AddressDecoder.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/scala/diplomacy/AddressDecoder.scala b/src/main/scala/diplomacy/AddressDecoder.scala index 5e54d173..8ccc6f09 100644 --- a/src/main/scala/diplomacy/AddressDecoder.scala +++ b/src/main/scala/diplomacy/AddressDecoder.scala @@ -31,11 +31,11 @@ object AddressDecoder } } } - val maxBits = log2Ceil(nonEmptyPorts.map(_.map(_.base).max).max) - val (bitsToTry, bitsToTake) = (0 to maxBits).map(BigInt(1) << _).partition(b => (givenBits & b) == 0) + val maxBits = log2Ceil(1 + nonEmptyPorts.map(_.map(_.base).max).max) + val (bitsToTry, bitsToTake) = (0 until maxBits).map(BigInt(1) << _).partition(b => (givenBits & b) == 0) val partitions = Seq(nonEmptyPorts.map(_.sorted).sorted(portOrder)) val givenPartitions = bitsToTake.foldLeft(partitions) { (p, b) => partitionPartitions(p, b) } - val selected = recurse(givenPartitions, bitsToTry.toSeq) + val selected = recurse(givenPartitions, bitsToTry.reverse.toSeq) val output = selected.reduceLeft(_ | _) | givenBits // Modify the AddressSets to allow the new wider match functions