def obj_nlopt_inspect(x, grad, para): #retrieve r and k A = para[0] B = para[1] E = para[2] F = para[3] num_m = int(x.size / 2) r = x[0:num_m] k = x[num_m:] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D, scenario, k) C = hp.C(A, B, r) U = hp.U_scrap(C, USY, miuY, sigmaY_Taylor, k, Sp, Sc) 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_scrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v, Sp, Sc) dsigmaY_dki = hp.dsigmaY_dki(D, sigmaX, r, i, k) grad_k[i] = hp.dU_dki_scrap(USY, miuY, sigmaY_Taylor, k, i, C, Sc, dsigmaY_dki, Sp) grad_combine = np.concatenate((grad_r, grad_k), axis=0) if grad.size > 0: grad[:] = grad_combine #Make sure to assign value using [:] #print(U) #print(lamada) return U
def obj_nlopt_inspect_fixk(x, grad, para): A = para[0] B = para[1] E = para[2] F = para[3] r = x[0:m] k = 3.0 * np.ones_like(r) 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_scrap(C, USY, miuY, sigmaY_Taylor, k, Sp, Sc) 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_scrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v, Sp, Sc) if grad.size > 0: grad[:] = grad_r #Make sure to assign value using [:] #print(U) #print(lamada) return U
def obj_nlopt_inspect(x, grad): #retrieve r and k num_m = int(x.size / 2) r = x[0:num_m] k = x[num_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) U = hp.U_scrap(C, USY, miuY, sigmaY_Taylor, k) 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) if grad.size > 0: grad[:] = grad_combine return U
def obj_nlopt_inspect(x, grad): #retrieve r and k num_m = int(x.size / 2) r = x[0:num_m] k = x[num_m:] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) #Update Lambda by simulation global lamada #lamada = hp.updateLambda(D,sigmaX,k,miu,NSample) sigmaY_Taylor = lamada * sigmaY_Taylor #Compute Unit Cost C = hp.C(A, B, r) U = hp.U_scrap(C, USY, miuY, sigmaY_Taylor, k, Sp, Sc) #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, Sp, Sc) grad_k[i] = hp.dU_dki_scrap(USY, miuY, sigmaY_Taylor, k[i], C[i], Sc[i]) grad_combine = np.concatenate((grad_r, grad_k), axis=0) if grad.size > 0: grad[:] = grad_combine #Make sure to assign value using [:] print(U) print(lamada) return U
def obj_grad_scipy_noinspect(x): #retrieve r and k grad = np.zeros(m) r = x[0:m] k = x[m:] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) #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_noscrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v) grad[:] = grad_r return grad
def obj_nlopt_noinspect(x, grad): #retrieve r as the optimization variable x. (k will not be optimized, so just use const) r = x[0:x.size] sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) #Compute Unit Cost C = hp.C(A, B, r) U = hp.U_noscrap(C, USY, miuY, sigmaY_Taylor) sigmaX = hp.sigma(E, F, r) sigmaY_Taylor = hp.sigmaY(sigmaX, D) 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_noscrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v) if grad.size > 0: grad[:] = grad_r return U
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 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
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, scenario, k) sigmaY_Taylor_minus = hp.sigmaY(sigmaX_minus, D, scenario, k) if scenario == 1: #NO INSPECT #gradient computed by numerical estimation grad_numerical_r[i] = (hp.U_noscrap( C_plus, USY, miuY, sigmaY_Taylor_plus, Sp) - hp.U_noscrap( C_minus, USY, miuY, sigmaY_Taylor_minus, Sp)) / (2 * epsilon) print('Numerical_No scrap_' + 'dr' + str(i), '=', grad_numerical_r[i]) #gradient computed by equation 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_equation_r[i] = hp.dU_dri_noscrap(USY, miuY, sigmaY_Taylor, C, k, i, dsigmaY_dri_v, dCi_dri_v, Sp) print('Equation_No scrap_' + 'dr' + str(i), '=', grad_equation_r[i]) elif scenario == 2: #Inspection FIX k #Varify dr #gradient computed by numerical estimation grad_numerical_r[i] = ( hp.U_scrap(C_plus, USY, miuY, sigmaY_Taylor_plus, k, Sp, Sc) - hp.U_scrap(C_minus, USY, miuY, sigmaY_Taylor_minus, k, Sp, Sc)) / ( 2 * epsilon) print('Numerical_scrap_' + 'dr' + str(i), '=', grad_numerical_r[i]) #gradient computed by equation dCi_dri_v = hp.dCi_dri(B[i], r[i]) dsigmai_dri_v = hp.dsigmai_dri(F[i], r[i])