def output(x): #Retrieve r and k global ite r = x[0:m] #k = x[m:] cost = hp.C(A, B, r) for i in range(m): print(ite, 'r' + str(i + 1) + ' ', r[i], 'k' + str(i + 1) + ' ', k[i], end='') sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) U = hp.U_scrap(cost, USY, miuY, sigmaY_Taylor, k) print(ite, ' U=', U) ite += 1
def obj_grad_scipy_inspect(x): #retrieve r and k grad = np.zeros_like(x) r = x[0:m] k = x[m:] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) sigmaY_Taylor = lamada * sigmaY_Taylor #Compute Unit Cost C = hp.C(A, B, r) for i in range(0, m): # Change this for loop to vectorization dCi_dri_v = hp.dCi_dri(B[i], r[i]) dsigmai_dri_v = hp.dsigmai_dri(F[i], r[i]) dsigmaY_dri_v = hp.dsigmaY_dri(D, sigmaX, r, i, dsigmai_dri_v) grad_r[i] = hp.dU_dri_scrap(USY, miuY, sigmaY_Taylor, C, k, i, lamada, dsigmaY_dri_v, dCi_dri_v) grad_k[i] = hp.dU_dki_scrap(USY, miuY, sigmaY_Taylor, k[i], C[i]) grad_combine = np.concatenate((grad_r, grad_k), axis=0) grad[:] = grad_combine return grad
def casestudy_U(): para = np.array([A, B, E, F]) result = optimize(True, para) U_equation = result['U'] r_opt = result['r'] if scenario == INSPECT: k_opt = result['k'] else: k_opt = 3 * np.ones_like(r_opt) sigma_opt = hp.sigma(E, F, r_opt) [N, M] = hp.estimateNandM(miu, E, F, r_opt, k_opt, NSample, USY, miuY, scenario) U_simulation = hp.U_inspect_simulation(NSample, r_opt, A, B, E, F, k_opt, miu, USY, miuY, Sp, Sc) print('U Equation: ', U_equation) print('U Simulation: ', U_simulation) satisfactionrate = hp.satisfactionrate_component_product( miu, E, F, r, k, NSample, USY, miuY, scenario) print('beta: ', satisfactionrate['beta']) print('sigmaY: ', hp.sigmaY(sigma_opt, D, scenario, k_opt)) print('opt cost:', hp.C(A, B, r_opt)) print('N: ', N) print('M: ', M) print('Gama', satisfactionrate['gammas'])
def obj_nlopt_noinspect(x, grad, para): #retrieve r as the optimization variable x. (k will not be optimized, so just use const) A = para[0] B = para[1] E = para[2] F = para[3] r = x[0:m] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D, scenario, k) #Compute Unit Cost C = hp.C(A, B, r) U = hp.U_noscrap(C, USY, miuY, sigmaY_Taylor, Sp) for i in range(0, m): # Change this for loop to vectorization dCi_dri_v = hp.dCi_dri(B[i], r[i]) dsigmai_dri_v = hp.dsigmai_dri(F[i], r[i]) dsigmaY_dri_v = hp.dsigmaY_dri(D, sigmaX, r, i, dsigmai_dri_v, scenario, k) grad_r[i] = hp.dU_dri_noscrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v, Sp) if grad.size > 0: grad[:] = grad_r #Make sure to assign value using [:] #print(U) return U
#Scrap cost of a product Sp = np.sum(A) / 10 #Scrap costs of components Sc = A / 10 D1 = hp.dy_dx1(miu[0], miu[1], miu[2]) D2 = hp.dy_dx2(miu[0], miu[1], miu[2]) D3 = hp.dy_dx3(miu[0], miu[1], miu[2]) D = np.array([D1, D2, D3]) #r=hp.sigmator(sigmaX,E,F) r = 10 * np.random.rand(3) k = 5 * np.random.rand(3) #3 * np.ones_like(r) # sigmaX = hp.sigma(E, F, r) #Compute Unit Cost of initial value C = hp.C(A, B, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D, scenario, k) #Nominal value of Y miuY = np.radians(7.0124) ##Upper specification limit USY = miuY + np.radians(2.0) #U = hp.U_scrap(C,USY,sigmaY,k) grad_numerical_r = np.zeros(m) grad_equation_r = np.zeros(m) grad_numerical_k = np.zeros(m)
def gradientcheck(x, case): if case == SCRAP: grad_equation = obj_grad_scipy_inspect(x) #retrieve grad of r and k grad_equation_r = grad_equation[0:m] grad_equation_k = grad_equation[m:] grad_numerical_k = np.zeros(m) grad_numerical_r = np.zeros(m) elif case == NOSCRAP: grad_equation_r = obj_grad_scipy_noinspect(x) grad_numerical_r = np.zeros(m) C = hp.C(A, B, r) for i in range(0, m): ri_add_epsilon = np.copy(r) ri_minus_epsilon = np.copy(r) ri_add_epsilon[i] += epsilon ri_minus_epsilon[i] -= epsilon ki_add_epsilon = np.copy(k) ki_minus_epsilon = np.copy(k) ki_add_epsilon[i] += epsilon ki_minus_epsilon[i] -= epsilon sigmaX_plus = hp.sigma(E, F, ri_add_epsilon) sigmaX_minus = hp.sigma(E, F, ri_minus_epsilon) C_plus = hp.C(A, B, ri_add_epsilon) C_minus = hp.C(A, B, ri_minus_epsilon) sigmaY_Taylor_plus = hp.sigmaY(sigmaX_plus, D) sigmaY_Taylor_minus = hp.sigmaY(sigmaX_minus, D) if case == SCRAP: sigmaY_Taylor_p = lamada * sigmaY_Taylor #Varify dr sigmaY_Taylor_plus *= lamada sigmaY_Taylor_minus *= lamada #gradient computed by numerical estimation grad_numerical_r[i] = ( hp.U_scrap(C_plus, USY, miuY, sigmaY_Taylor_plus, k) - hp.U_scrap(C_minus, USY, miuY, sigmaY_Taylor_minus, k)) / ( 2 * epsilon) #varify dk grad_numerical_k[i] = ( hp.U_scrap(C, USY, miuY, sigmaY_Taylor_p, ki_add_epsilon) - hp.U_scrap(C, USY, miuY, sigmaY_Taylor_p, ki_minus_epsilon)) / (2 * epsilon) print('Numerical_scrap_' + 'dr' + str(i), '=', grad_numerical_r[i]) print('Equation_scrap_' + 'dr' + str(i), '=', grad_equation_r[i]) print('Numerical_scrap_' + 'dk' + str(i), '=', grad_numerical_k[i]) print('Equation_scrap_' + 'dk' + str(i), '=', grad_equation_k[i]) elif case == NOSCRAP: #gradient computed by numerical estimation grad_numerical_r[i] = (hp.U_noscrap( C_plus, USY, miuY, sigmaY_Taylor_plus) - hp.U_noscrap( C_minus, USY, miuY, sigmaY_Taylor_minus)) / (2 * epsilon) print('Numerical_No scrap_' + 'dr' + str(i), '=', grad_numerical_r[i]) print('Equation_No scrap_' + 'dr' + str(i), '=', grad_equation_r[i]) distance12_r = distance.euclidean(grad_equation_r, grad_numerical_r) length1_r = distance.euclidean(grad_equation_r, np.zeros_like(grad_equation_r)) length2_r = distance.euclidean(grad_numerical_r, np.zeros_like(grad_numerical_r)) graderror_r = distance12_r / (length1_r + length2_r) print('error of dr=', graderror_r) if case == SCRAP: distance12_k = distance.euclidean(grad_equation_k, grad_numerical_k) length1_k = distance.euclidean(grad_equation_k, np.zeros_like(grad_equation_k)) length2_k = distance.euclidean(grad_numerical_k, np.zeros_like(grad_numerical_k)) graderror_k = distance12_k / (length1_k + length2_k) print('error of dk=', graderror_k)
for i in range(m): print(ite, 'r' + str(i + 1) + ' ', r[i], 'k' + str(i + 1) + ' ', k[i], end='') sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) U = hp.U_scrap(cost, USY, miuY, sigmaY_Taylor, k) print(ite, ' U=', U) ite += 1 #Unit cost of initial values sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) cost = hp.C(A, B, r) SCIPY = 0 NLOPT = 1 opt_lib = NLOPT if opt_lib == SCIPY: if case == SCRAP: #Scrap #Define Upper and Lower boundaries #The order is ([lower bnd for x1, lower bnd for x2], [Higher bnd for x1, Higher bnd for x2]) mbounds = Bounds([ smallvalue, smallvalue, smallvalue, smallvalue, smallvalue, smallvalue ], [
# -*- coding: utf-8 -*- """ Created on Sun May 26 10:04:08 2019 @author: wyue """ #import packages import numpy as np import helpers as hp epsilon = 1e-7 rMin = 1e-2 # Lower bound is 0, to prevent dividing by zero, set lower bond to a small value rMax = 100 rRst = np.array([20, 20, 20]) A = np.array([0.87, 1.71, 3.54]) #np.array([5.0, 3.0, 1.0]) B = np.array([2.062, 1.276, 1.965]) #np.array([20.0, 36.7, 36.0]) F = np.array([0.001798 / 3, 0.001653 / 3, 0.002 / 3]) #E = sigmaX_init - np.multiply(F,np.power(r,2)) #np.array([sigmaX_init1-1, sigmaX_init2-1, sigmaX_init3-1]) E = np.array([0.083, 0.096, 0.129]) sigmaMin = hp.sigma(E, F, rMin) sigmaMax = hp.sigma(E, F, rMax) simgaRst = hp.sigma(E, F, rRst)
for i in range(m): print(ite, 'r' + str(i + 1) + ' ', r[i], 'k' + str(i + 1) + ' ', k[i], end='') sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) U = hp.U_scrap(cost, USY, miuY, sigmaY_Taylor, k, Sp, Sc) print(ite, ' U=', U) ite += 1 #Unit cost of initial values sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) cost = hp.C(A, B, r) SCIPY = 0 NLOPT = 1 opt_lib = NLOPT if opt_lib == SCIPY: if scenario == INSPECT: #Scrap #Define Upper and Lower boundaries #The order is ([lower bnd for x1, lower bnd for x2], [Higher bnd for x1, Higher bnd for x2]) mbounds = Bounds([ smallvalue, smallvalue, smallvalue, smallvalue, smallvalue, smallvalue ], [