All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.framework.model.ShadowPrice.kt Maven / Gradle / Ivy

There is a newer version: 1.0.31
Show newest version
package fuookami.ospf.kotlin.framework.model

import kotlin.reflect.KClass
import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.error.*
import fuookami.ospf.kotlin.utils.functional.*
import fuookami.ospf.kotlin.core.frontend.model.mechanism.*

open class ShadowPriceKey(
    val limit: KClass<*>
)

data class ShadowPrice(
    val key: ShadowPriceKey,
    val price: Flt64
) {
    override fun toString(): String {
        return "$key: $price"
    }
}

typealias ShadowPriceExtractor = (AbstractShadowPriceMap, Args) -> Flt64

abstract class AbstractShadowPriceMap> {
    val map: Map by ::_map
    private val _map = HashMap()
    private val _extractors = ArrayList>()

    open operator fun invoke(arg: Args) = _extractors.sumOf { it(this, arg) }

    operator fun get(key: ShadowPriceKey): ShadowPrice? = _map[key]

    fun put(price: ShadowPrice) {
        _map[price.key] = price
    }

    fun put(extractor: ShadowPriceExtractor<@UnsafeVariance Args, @UnsafeVariance M>) {
        _extractors.add(extractor)
    }

    fun remove(key: ShadowPriceKey) {
        _map.remove(key)
    }
}

fun > extractShadowPrice(
    shadowPriceMap: Map,
    pipelineList: CGPipelineList,
    model: Model,
    shadowPrices: List
): Try {
    for (pipeline in pipelineList) {
        when (val ret = pipeline.refresh(shadowPriceMap, model, shadowPrices)) {
            is Ok -> {}
            is Failed -> {
                return Failed(ret.error)
            }
        }
        val extractor = pipeline.extractor() ?: continue
        shadowPriceMap.put(extractor)
    }
    return ok
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy