commonMain.space.kscience.kmath.optimization.FunctionOptimization.kt Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2018-2022 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package space.kscience.kmath.optimization
import space.kscience.kmath.expressions.DifferentiableExpression
import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.misc.FeatureSet
public class OptimizationValue(public val value: T) : OptimizationFeature {
override fun toString(): String = "Value($value)"
}
public enum class FunctionOptimizationTarget : OptimizationFeature {
MAXIMIZE,
MINIMIZE
}
public class FunctionOptimization(
override val features: FeatureSet,
public val expression: DifferentiableExpression,
) : OptimizationProblem {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as FunctionOptimization<*>
if (features != other.features) return false
if (expression != other.expression) return false
return true
}
override fun hashCode(): Int {
var result = features.hashCode()
result = 31 * result + expression.hashCode()
return result
}
override fun toString(): String = "FunctionOptimization(features=$features)"
}
public fun FunctionOptimization.withFeatures(
vararg newFeature: OptimizationFeature,
): FunctionOptimization = FunctionOptimization(
features.with(*newFeature),
expression,
)
/**
* Optimizes differentiable expression using specific [optimizer] form given [startingPoint].
*/
public suspend fun DifferentiableExpression.optimizeWith(
optimizer: Optimizer>,
startingPoint: Map,
vararg features: OptimizationFeature,
): FunctionOptimization {
val problem = FunctionOptimization(FeatureSet.of(OptimizationStartPoint(startingPoint), *features), this)
return optimizer.optimize(problem)
}
public val FunctionOptimization.resultValueOrNull: T?
get() = getFeature>()?.point?.let { expression(it) }
public val FunctionOptimization.resultValue: T
get() = resultValueOrNull ?: error("Result is not present in $this")