Source code for CADRE.KS
import numpy as np
from openmdao.main.api import Component
from openmdao.lib.datatypes.api import Float, Array
[docs]class KSfunction(object):
"""Helper class that can be used inside other components to aggregate constraint
vectors with a KS function."""
[docs] def compute(self, g, rho=50):
"""Gets the value of the KS function for the given array of constraints."""
self.rho = rho
self.g_max = np.max(g)
self.g_diff = g-self.g_max
self.exponents = np.exp(rho * self.g_diff)
self.summation = np.sum(self.exponents)
self.KS = self.g_max + 1.0/rho * np.log(self.summation)
return self.KS
[docs] def derivatives(self):
"""returns a row vector of [dKS_gd, dKS_drho]"""
dsum_dg = self.rho*self.exponents
dKS_dsum = 1.0/self.rho/self.summation
self.dKS_dg = dKS_dsum * dsum_dg
dsum_drho = np.sum(self.g_diff*self.exponents)
self.dKS_drho = dKS_dsum * dsum_drho
return self.dKS_dg, self.dKS_drho
[docs]class KSComp(Component):
"""Aggregates a number of functions to a single value via the
Kreisselmeier-Steinhauser Function."""
rho = Float(.1,
iotype="in",
desc="Hyperparameter for the KS function")
KS = Float(0,
iotype="out",
desc="Value of the aggregate KS function")
def __init__(self, n=2):
super(KS, self).__init__()
self.n = n
self.add('g',Array(zeros((n,)),
size=(n,1),
dtype=Float,
iotype="in",
desc="Array of function values to be aggregated"))
self._ks = KSfunction()
[docs] def execute(self):
self.KS = self._ks.compute(self.g, self.rho)
[docs] def linearize(self):
"""Linearize around the last executed point"""
#use g_max, exponsnte, summation from last executed point
self.J = np.hstack(self._ks.derivatives())
[docs] def provideDer(self):
ins = ('g','rho')
outs = ('KS', )
return ins, outs, self.J