Skip to content

Commit

Permalink
make all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Peva Blanchard committed Jul 20, 2023
1 parent 4ff43d4 commit 3736120
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Assessment(
substanceCharacterizations,
observableProducts,
observableSubstances,
system.parameters.size,
system.effectiveParametersSize,
)

val terminalProducts = processes
Expand All @@ -56,13 +56,13 @@ class Assessment(
terminalProducts,
terminalSubstances,
indicators,
system.parameters.size,
system.effectiveParametersSize,
)

demandMatrix = DemandMatrix(
targetProcess,
observablePorts,
system.parameters.size,
system.effectiveParametersSize,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class Evaluator(
fun trace(expression: EProcessTemplateApplication): EvaluationTrace {
LOG.info("Start recursive Compile")
try {
val result = EvaluationTrace.empty(expression.arguments.mapValues { it.value.toValue() })
recursiveCompile(result, HashSet(), HashSet(setOf(expression)))
val arguments = expression.arguments.mapValues {
dataReducer.reduce(it.value)
}
val result = EvaluationTrace.empty(arguments.mapValues { it.value.toValue() })
recursiveCompile(result, HashSet(), HashSet(setOf(EProcessTemplateApplication(expression.template, arguments))))
LOG.info("End recursive Compile, found ${result.getNumberOfProcesses()} processes and ${result.getNumberOfSubstanceCharacterizations()} substances")
return result
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.kleis.lcaplugin.core.lang.value

import arrow.core.filterIsInstance
import ch.kleis.lcaplugin.core.HasUID


Expand All @@ -8,6 +9,11 @@ data class SystemValue(
val processes: Set<ProcessValue>,
val substanceCharacterizations: Set<SubstanceCharacterizationValue>,
) : Value, HasUID {
val effectiveParametersSize = parameters
.filterIsInstance<String, QuantityValue>()
.filter { it.value.amount.first.isNotEmpty() }
.size


val productToProcessMap: Map<ProductValue, ProcessValue> =
processes.flatMap { process -> process.products.map { it.product to process } }.toMap()
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/ch/kleis/lcaplugin/core/math/DualMatrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import org.jetbrains.kotlinx.multik.ndarray.operations.minus
import org.jetbrains.kotlinx.multik.ndarray.operations.plus
import org.jetbrains.kotlinx.multik.ndarray.operations.unaryMinus

/*
TODO: TEST ME !!!
*/

private fun d2Ejml(m: D2Array<Double>): EJMLMatrix {
val (rows, cols) = m.shape
val r = EJMLMatrixFactory.INSTANCE.zero(rows, cols)
Expand All @@ -22,7 +26,8 @@ private fun d2Ejml(m: D2Array<Double>): EJMLMatrix {
}

private fun d3Ejml(m: D3Array<Double>): EJMLMatrix {
val (rows, cols, nParams) = m.shape
val (rows, cols, nps) = m.shape
val nParams = if (nps == 0) 1 else nps
val r = EJMLMatrixFactory.INSTANCE.zero(rows, cols * nParams)
m.forEachMultiIndexed { index, d ->
val (i, j, k) = index
Expand All @@ -45,6 +50,7 @@ private fun ejmlD2(m: EJMLMatrix): D2Array<Double> {
private fun ejmlD3(m: EJMLMatrix, nParams: Int): D3Array<Double> {
val (rows, cols) = Pair(m.rowDim(), m.colDim())
val r = mk.zeros<Double>(rows, cols, nParams)
if (nParams == 0) return r
val iterator = m.matrix.getMatrix<DMatrixSparseCSC>().createCoordinateIterator()
while (iterator.hasNext()) {
val v = iterator.next()
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/ch/kleis/lcaplugin/core/math/DualNumber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class DualNumber(
val zeroth: Double,
val first: D1Array<Double>,
) {

override fun toString(): String {
return "$zeroth"
}
Expand Down
36 changes: 25 additions & 11 deletions src/main/kotlin/ch/kleis/lcaplugin/core/math/ejml/EJMLMatrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,57 @@ package ch.kleis.lcaplugin.core.math.ejml
import org.ejml.simple.SimpleMatrix

class EJMLMatrix(internal val matrix: SimpleMatrix) {
fun value(row: Int, col: Int): Double =
fun value(row: Int, col: Int): Double =
matrix[row, col]

operator fun minus(other: EJMLMatrix): EJMLMatrix {
operator fun minus(other: EJMLMatrix): EJMLMatrix {
return EJMLMatrix(
matrix - other.matrix
)
}

operator fun plus(other: EJMLMatrix): EJMLMatrix {
operator fun plus(other: EJMLMatrix): EJMLMatrix {
return EJMLMatrix(
matrix + other.matrix
)
}

fun matDiv(other: EJMLMatrix): EJMLMatrix? {
fun matDiv(other: EJMLMatrix): EJMLMatrix? {
val solver = EJMLSolver.INSTANCE
return solver.solve(this, other)
}

fun matMul(other: EJMLMatrix): EJMLMatrix {
fun matMul(other: EJMLMatrix): EJMLMatrix {
if (this.isScalar()) {
return EJMLMatrix(
other.matrix.scale(value(0, 0))
)
}
if (other.isScalar()) {
return EJMLMatrix(
this.matrix.scale(other.value(0, 0))
)
}
return EJMLMatrix(
matrix.mult(other.matrix)
)
}

fun add(row: Int, col: Int, value: Double) {
private fun isScalar(): Boolean {
return rowDim() == 1 && colDim() == 1
}

fun add(row: Int, col: Int, value: Double) {
matrix.set(row, col, value(row, col) + value)
}

fun set(row: Int, col: Int, value: Double) =
fun set(row: Int, col: Int, value: Double) =
matrix.set(row, col, value)

fun negate(): EJMLMatrix = EJMLMatrix(matrix.negative())
fun transpose(): EJMLMatrix = EJMLMatrix(matrix.transpose())
fun negate(): EJMLMatrix = EJMLMatrix(matrix.negative())
fun transpose(): EJMLMatrix = EJMLMatrix(matrix.transpose())

fun rowDim(): Int = matrix.numRows
fun rowDim(): Int = matrix.numRows

fun colDim(): Int = matrix.numCols
fun colDim(): Int = matrix.numCols
}
18 changes: 13 additions & 5 deletions src/test/kotlin/ch/kleis/lcaplugin/e2e/E2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ch.kleis.lcaplugin.core.lang.value.FromProcessRefValue
import ch.kleis.lcaplugin.core.lang.value.ProductValue
import ch.kleis.lcaplugin.core.lang.value.QuantityValue
import ch.kleis.lcaplugin.core.lang.value.UnitValue
import ch.kleis.lcaplugin.core.math.times
import ch.kleis.lcaplugin.core.prelude.Prelude
import ch.kleis.lcaplugin.language.parser.LcaLangAbstractParser
import ch.kleis.lcaplugin.language.psi.LcaFile
Expand Down Expand Up @@ -311,9 +312,9 @@ class E2ETest : BasePlatformTestCase() {
val key = ProductValue(
"in", kg,
)
assertEquals(
QuantityValue(DualNumber.constant(3.0), kg), actual.impacts[key]
)
val nParams = 3
val expected = QuantityValue(DualNumber.constant(3.0), kg)
assertEquals(expected, actual.impacts[key])
}

@Test
Expand Down Expand Up @@ -350,7 +351,10 @@ class E2ETest : BasePlatformTestCase() {
val actual = reducer.reduce(expr)

// then
val expected = EQuantityScale(DualNumber.constant(200.0), EUnitLiteral(UnitSymbol.of("m").pow(4.0), 1.0, Prelude.length.pow(4.0)))
val expected = EQuantityScale(
DualNumber.constant(200.0),
EUnitLiteral(UnitSymbol.of("m").pow(4.0), 1.0, Prelude.length.pow(4.0))
)
TestCase.assertEquals(expected, actual)
}

Expand Down Expand Up @@ -539,7 +543,11 @@ class E2ETest : BasePlatformTestCase() {
}

val ratio = result.valueRatio(output, input)
assertEquals(QuantityValue(DualNumber.constant(3.0), asValue(UnitFixture.kg)), ratio)
val expected = QuantityValue(
DualNumber.constant(3.0),
asValue(UnitFixture.kg)
)
assertEquals(expected, ratio)
}

@Test
Expand Down

0 comments on commit 3736120

Please sign in to comment.