PlotGenerator.kt.mj 6.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
fun generateScalingPlot(logs: List<ParEpLog>, graphs: Array<String>, k: Int, allowedNumberOfCores: Array<Int>, filename: String, aspect: (ParEpLog) -> Double) {
    println(string)

    val filtered = logs.filter { it.graphName in graphs && it.numberOfCores in allowedNumberOfCores && it.k == k }
    val runtimes = filtered.groupBy { Pair(it.graphName, it.numberOfCores) }
            .mapValues { (_, values) -> values.map(aspect).average() }

    File(filename).bufferedWriter().use {
        for (graph in graphs) {
            for (numberOfCores in allowedNumberOfCores) {
                val key = Pair(graph, numberOfCores)
                val runtime = runtimes[key]
                if (runtime != null) {
                    it.write(string_string_string)
                } else {
                    println(string)
                }
            }
        }
    }
}

fun generateTwoPlots(db: Db, algorithms: Array<String>, graphs: Array<String>, ks: Array<Int>, filename: String, aspect: (Log) -> Double) {
    println(string)

    val filtered = db.filter {
        it.algorithm in algorithms && it.graphName in graphs && it.k in ks
    }

    data class Point(val algorithm: String, val x: Int, val y: Double)
    val points = ArrayList<Point>()

    val instanceNames = graphs
            .flatMap { graph -> ks.map { k -> Pair(graph, k) } }
            .sortedBy { (graph, k) -> dimensions[graph]!![string]!! }

    var x = 0
    for ((graph, k) in instanceNames) {
            val instances = filtered
                    .filter { it.graphName == graph && it.k == k }
                    .groupBy { it.algorithm }
            val values = instances.mapValues { (_, values) -> values.map(aspect).average() }

            for (algorithm in algorithms) {
                points.add(Point(algorithm, x, values[algorithm]!!))
            }
        ++x
    }

    File(filename).bufferedWriter().use {
        for ((algorithm, x, y) in points) {
            it.write(string)
            it.newLine()
        }
    }
}

fun generateSinglePlot(db: Db, algorithms: Array<String>, graphs: Array<String>, ks: Array<Int>, filename: String) {
    println(string)

    data class Point(val algorithm: String, val x: Double, val y: Double)
    val points = ArrayList<Point>()

    val filtered = db.filter {
        it.algorithm in algorithms && it.graphName in graphs && it.k in ks
    }

    for (graph in graphs) {
        for (k in ks) {
            val instances = filtered
                    .filter { it.graphName == graph && it.k == k }
                    .groupBy { it.algorithm }
            val cuts = instances.mapValues { (_, values) -> values.map { it.vertexCut }.average() }
            val bestCut = cuts.map { (_, cut) -> cut }.min() as Double

            val runtimes = instances.mapValues { (_, values) -> values.map { it.time }.average() }
            val bestRuntime = runtimes.map { (_, runtime) -> runtime }.min() as Double

            for (algorithm in algorithms) {
                val cut = cuts[algorithm]
                if (cut == null) {
                    println(string)
                    continue
                }
                val runtime = runtimes[algorithm]!!

                points.add(Point(algorithm, runtime / bestRuntime, 1 - (bestCut / cut)))
            }
        }
    }

    File(filename).bufferedWriter().use {
        for ((algorithm, x, y) in points) {
            it.write(string)
            it.newLine()
        }
    }
}

fun generatePerformancePlot(db: Db, algorithms: Array<String>, graphs: Array<String>, ks: Array<Int>, filename: String, aspect: (Log) -> Double) {
    println(string)

    val performanceValues = HashMap<String, MutableList<Double>>().apply {
        algorithms.forEach { this/*@*/apply[it] = ArrayList() }
    }
    val filtered = db.filter { it.algorithm in algorithms && it.graphName in graphs && it.k in ks }

    for (graph in graphs) {
        for (k in ks) {
            val values = filtered.filter { it.graphName == graph && it.k == k }
                    .groupBy { it.algorithm }
                    .mapValues { (_, values) -> values.map(aspect).average() }
            val best = values.map { (_, average) -> average }.min()
            if (best == null) {
                println(string)
                continue
            }

            for (algorithm in algorithms) {
                val value = values[algorithm]
                if (value != null) {
                    if (value == 0.0) {
                        throw Exception(string)
                    }
                    performanceValues[algorithm]!!.add(1.0 - best / value)
                    if (algorithm in arrayOf(string)) {
                        println(string)
                    }
                } else {
                    performanceValues[algorithm]!!.add(1.0)
                }
            }
        }
    }

    performanceValues.forEach { (_, values) -> values.sortDescending() }

    File(filename).bufferedWriter().use { writer ->
        for (algorithm in algorithms) {
            var i = 1
            performanceValues[algorithm]?.forEach {
                writer.write(string)
                writer.newLine()
                ++i
            }
        }
    }
}

class PerformancePlotGenerator {

    private val records = HashMap<String, ArrayList<Double>>()

    fun add(algorithm: String, performance: Double) = records.getOrPut(algorithm) { ArrayList() }.add(performance)

    fun generate(writer: Writer) {
        val (maxLengthAlgorithm, maxLength) = records
                .mapValues { (_, v) -> v.size }
                .maxBy { (_, v) -> v }!!
        val (minLengthAlgorithm, minLength) = records
                .mapValues { (_, v) -> v.size }
                .minBy { (_, v) -> v }!!

        if (minLength != maxLength) {
            println(string)
        }

        for (i in 0 until maxLength) {
            val notNull = records.filter { (_, v) -> v.size > i }
            val best = notNull.map { (_, v) -> v[i] }.min()!!
            val values = notNull.mapValues { (_, v) -> 1.0 - best / v[i] }
            values.forEach { (k, v) -> records[k]?.let { it[i] = v } }
        }

        records.forEach { (_, v) -> v.sort() }
        for (i in 0 until maxLength) {
            val notNull = records.filter { (_, v) -> v.size > i }
            val values = notNull.mapValues { (_, v) -> v[i] }
            values.forEach { (k, v) -> writer.write(string) }
        }

    }
}