'''


'''

from maintLib import lambdaE, tauBRP
import numpy as np
import math
from scipy.optimize import minimize
MTTF_A = 8000
q_B = 0.1
MTTF_B = 16000
alpha = 3
c_PM = 4000
c_CM = 9000
c_FT = 1000
S = 750
MDT = 8
c_U = 25000
tau_A_O = 1000
tau_B_O = 14000
tau_B_FT = 7000
c_F = c_CM + q_B * c_U * MDT

print("\na) Objective function:")
print("C(tau) = c_PM/tau + lambdaE(tau,MTTF,alpha)*c_F")
print("\nwhere c_F = c_CM + q_B * c_U * MDT")

# First objective function:
def C_1(tau,MTTF,alpha,c_PM,c_F):
    return c_PM/tau +   lambdaE(tau,MTTF,alpha,True)*c_F

print("\nb) Analytical solution:\ntau* = MTTF_A/gamma(1 + 1/alpha)*(c_PM /(c_F *((alpha - 1))))^(1/alpha)")

tau_A_O = MTTF_A / math.gamma(1 + 1 / alpha) * \
        (c_PM / (c_F * ((alpha - 1)))) ** (1 / alpha)
print("\nNumerical result by formula:")
print("Analytical: tau* =",tau_A_O)
print("C(tau*) =",C_1(tau_A_O,MTTF_A,alpha,c_PM,c_F))

tau_A_O=tauBRP(MTTF_A,alpha,c_PM,c_F,True)
print("\nResult by using tauBRP funciton, 'simple':")
print("tau* =",tau_A_O)
print("BRP: C(tau*) =",c_PM/tau_A_O+lambdaE(tau_A_O,MTTF_A,alpha,True)*c_F)
 
tau_A_O=tauBRP(MTTF_A,alpha,c_PM,c_F)
print("\nResult by using tauBRP funciton, 'improved':")
print("tau* =",tau_A_O)
print("BRP: C(tau*) =",c_PM/tau_A_O+lambdaE(tau_A_O,MTTF_A,alpha)*c_F)

MTTF_A *= 1/8760 # Numerically, more stable with "x" in the order of magnitude 1    
res = minimize(C_1, tau_A_O-50, args = (MTTF_A,alpha,c_PM,c_F))      
print("\nc) Python minimize: tau* =",res.x[0], "(yeras) =", res.x[0]*8760, "(hours)") 
print("Ojective function =",res.fun, "(per year) =",res.fun/8760, "(per hour)") 



