# -*- coding: utf-8 -*- """ Created on Tue Jun 4 16:42:45 2019 @author: elena """ from input_data import input_data from LPFMethod import longpath from MehrotraMethod import mehrotra from cent_meas import cent_meas ''' * ANALYSIS LONG-STEP PATH-FOLLOWING METHOD * ''' # Input_data (A, b, c) = input_data(10) # Applicaton of longpath x, s, u = longpath(A, b, c) # Recall the dataframe dfu = cent_meas(x, u, 'LPF')
# Input data in canonical form A2 = q[:,:7] b2 = q[:,7] c2 = np.array([16, 10, 8, 9, 48, 60, 53]) (A3, c3) = stdForm(A2, c2) return(A2, b2, c2) #%% """ run the methods """ if __name__ == "__main__": (A, b, c) = ssteel1() x, s , u = affine(A, b, c, c_form = 1, info = 1, ip = 1) dfu = cent_meas(x, u, label = 'Affine', plot = 0) # 17 it """ LPF1 """ # 161 iterations x, s, u = longpath1(A, b, c, c_form = 1, info = 1, ip = 0) dful = cent_meas(x, u, label = 'LPF', plot = 0) """ LPF2 """ # 13 it x, s, u, sigma_l2 = longpath2(A, b, c, c_form = 1, info = 1, ip = 1) dfc = cent_meas(x, u, label = 'LPF2', plot = 0) """ LPF predictor corrector """ # 14 iterations x, s, u, sigma_pc = longpathPC(A, b, c, c_form = 1, info = 1, ip = 0) dfpc = cent_meas(x, u, label = 'LPF PC', plot = 0)
print( '\nCurrent point:\n x = {} \n lambda = {} \n s = {}.\n'.format( x.round(decimals=3), y.round(decimals=3), s.round(decimals=3))) print('Dual next gap: {}.\n'.format("%10.3f" % g)) #%% print_boxed("Found optimal solution of the problem at\n x* = {}.\n\n". format(x.round(decimals=3)) + "Dual gap: {}\n".format("%2.2e" % g) + "Optimal cost: {}\n".format("%10.8E" % z) + "Number of iteration: {}".format(it)) return x, s, u, sig #%% ''' =============== LPF METHOD test =============== ''' if __name__ == "__main__": # Input data of canonical LP: (A, b, c) = input_data(18) x, s, u, sig = longpath2(A, b, c, info=1) dlpf = cent_meas(x, u, 'LPF', plot=0)
V = np.concatenate((T, U), axis=0) # RHS of the linear system with minus rb = b - np.dot(A, x) rc = c - np.dot(A.T, y) - s rxs = -x * s + cp * (sum(x * s) / c_A) * np.ones( c_A) # Newton step toward x*s = sigma*mi r = np.hstack((rb, rc - np.dot(X_inv, rxs))) o = np.linalg.solve(V, r) # SEARCH DIRECTION: y1 = o[:r_A] x1 = o[r_A:c_A + r_A] s1 = np.dot(X_inv, rxs) - np.dot(W1, x1) return x1, y1, s1, rb, rc #%% ''' ================== LPF PC METHOD test ================== ''' if __name__ == "__main__": (A, b, c) = input_data(21) x, s, u, sig = longpathPC(A, b, c) ul = cent_meas(x, u, 'LPF Predictor corrector', plot=0)
q = r.as_matrix() q = np.asarray(q) c = q[0, :64] A = q[1:21, :64] b = q[1:21, 64] return (A, b, c) #%% """ run the methods """ if __name__ == "__main__": (A, b, c) = tubprod() # canonical form! """ Affine """ # 29 it x, s, u = affine(A, b, c, ip=1) dfu = cent_meas(x, u, label='Affine', plot=0) """ LPF1 """ # IT DOESN'T WORK!!! # x_l, s_l, u_l = longpath1(A, b, c, info = 1, ip = 1) """ LPF2 """ # 21 it x, s, u, sigma_l2 = longpath2(A, b, c, info=1, ip=0) dfc = cent_meas(x, u, label='LPF2', plot=0) """ LPF predictor corrector """ # 20 iterations x, s, u, sigma_pc = longpathPC(A, b, c, info=1, ip=1) dfpc = cent_meas(x, u, label='LPF PC', plot=0) """ Mehrotra """ # 10 iterations start = time.time() x_m, s_m, u_m, sigma_m = mehrotra(A, b, c, info=1, ip=1)
#%% """ run the methods """ #x_a, s_a , u_a = affine(A, b, c) #dfu = cent_meas(x_a, u_a, label = 'Affine') # 29 it """ LPF1 """ # 174 iterations / 174 iterations start = time.time() x_l, s_l, u_l = longpath1(A, b, c, c_form = 1, info = 1, ip = 0) time_lpf1 = time.time()-start print('Time of the algorithm is {} \n\n'.format("%2.2e"%time_lpf1)) dful = cent_meas(x_l, u_l, label = 'LPF', plot = 0) """ LPF2 """ # 12 it start = time.time() x_c, s_c, u_c, sigma_l2 = longpath2(A, b, c, c_form = 1, info = 1) time_lpf2 = time.time()-start print('Time of the algorithm is {} \n\n'.format("%2.2e"%time_lpf2)) dfc = cent_meas(x_c, u_c, label = 'LPF2', plot= 0) """ LPF predictor corrector """ # 12 iterations/ 14 iteraions start = time.time() x_pc, s_pc, u_pc, sigma_pc = longpathPC(A, b, c, c_form = 1, info = 1, ip = 0) time_lpfpc = time.time()-start
g = z - np.dot(y, b) # Termination elements tm = term(it, b, c, rb, rc, z, g) u.append([it, g, x.copy(), s.copy(), rb.copy(), rc.copy()]) print('Dual next gap: {}.\n'.format("%10.3f" % g)) print_boxed("Found optimal solution of the problem at\n x* = {}.\n\n". format(x.round(decimals=3)) + "Dual gap: {}\n".format("%10.6f" % g) + "Optimal cost: {}\n".format("%10.3f" % z) + "Number of iteration: {}".format(it)) print("Centering parameter sigma:{}.\n".format("%10.3f" % cp)) return x, s, u #%%ù """Input data""" if __name__ == "__main__": # Input data of canonical LP: (A, b, c) = input_data(1) # Central parameter cp1 = 0.9 x, s, u = longpathC(A, b, c, cp=cp1) cent_meas(x, u, ' LPF_cp')
""" = PRINT PRIMAL FEASIBILITY SPEED CONVERGENCE = Compute: W = [ max|b - Ax_{k}| ] with x_{k} sequence generated by LPF2 Z = [ max|b - Ax_{k}| ] with x_{k} sequence generated by LPF_PC Plot the speed of the convergence of the error of feasibility """ i = 0 A, b, c = input_data(i) x2, s2, u2, sig2 = longpath2(A, b, c) dlpf = cent_meas(x2, u2, 'LPF', plot = 0) W = dlpf['pf'] x3, s3, u3, sig3 = longpathPC(A, b, c) ul = cent_meas(x3, u3, 'LPF Predictor corrector', plot = 0) Z = ul['pf'] plt.figure() plt.subplot(211) plt.plot(W) plt.title('Speed error LPF2') locs, labels = plt.xticks(np.arange(0, len(W), step = 1)) plt.yscale('log') plt.grid(b = True) plt.subplot(212)
raise TimeoutError("Iterations maxed out") if info == 0: print('\nCurrent point:\n x = {} \n lambda = {} \n s = {}.\n'.format(x.round(decimals = 3), y.round(decimals = 3), s.round(decimals = 3))) print('Dual next gap: {}.\n'.format("%2.2e"%g)) #%% print_boxed("Found optimal solution of the problem at\n x* = {}.\n\n".format(x.round(decimals = 3)) + "Dual gap: {}\n".format("%10.6e"%g) + "Optimal cost: {}\n".format("%10.3f"%z) + "Number of iteration: {}".format(it)) return x, s, u #%% ''' =============== LPF METHOD test =============== ''' if __name__ == "__main__": # Input data of canonical LP: (A, b, c) = input_data(21) x, s, u = longpath1(A, b, c) dfm = cent_meas(x, u, 'LPF', plot = 0)
print("Iterations maxed out") return x, s, u print('\nCurrent point:\n x = {} \n lambda = {} \n s = {}.\n'.format( x.round(decimals=3), y.round(decimals=3), s.round(decimals=3))) z = np.dot(c, x) g = z - np.dot(y, b) u.append([it, g, x.copy(), s.copy(), rb.copy(), rc.copy()]) # Termination elements tm = term(it, b, c, rb, rc, z, g) print('Dual next gap: {}.\n'.format("%10.3f" % g)) print_boxed("Found optimal solution of the problem at\n x* = {}.\n\n". format(x.round(decimals=3)) + "Dual gap: {}\n".format("%10.6f" % g) + "Optimal cost: {}\n".format("%10.3f" % z) + "Number of iteration: {}".format(it)) return x, s, u #%% if __name__ == "__main__": # Input data of canonical LP: (A, b, c) = input_data(0) x, s, u = longpath2(A, b, c) ul = cent_meas(x, u, 'LPF2')
r_A, c_A = A.shape A = np.vstack((A, np.zeros((10, c_A)))) for i in range(1, 11): A[r_A - 1 + i, 12 + i] = 1 return (A, b, c) #%% """ run the methods """ (A, b, c) = onb() # input data in canonical form IP = 0 """ Affine """ # IP = 1 doesn't work x, s, u = affine(A, b, c, ip=IP) dfu = cent_meas(x, u, label='Affine', plot=0) # 29 it """ LPF1 """ # 307 iterations x, s, u = longpath1(A, b, c, c_form=0, info=1, ip=IP) dfu = cent_meas(x, u, label='LPF', plot=0) """ LPF2 """ # 16 it x, s, u, sigma_l2 = longpath2(A, b, c, c_form=0, info=1, ip=1) """ LPF predictor corrector """ # 41 iterations / 17 iterations ip = 1 x_pc, s_pc, u_pc, sigma_pc = longpathPC(A, b, c, c_form=0, info=0) x_pc, s_pc, u_pc, sigma_pc = longpathPC(A, b, c, c_form=0, info=0, ip=1) dfpc = cent_meas(x_pc, u_pc, label='LPF PC', plot=0) """ Mehrotra """ # 8 iterations
raise TimeoutError("Iterations maxed out") if info == 0: print('CORRECTOR STEP:\nCurrent primal-dual point: \n x = ', x, '\nlambda = ', y, '\n s = ', s) print('Current g: {}\n'.format("%.3f" % g)) #%% print_boxed( "Found optimal solution of the standard problem at\n x* = {}.\n\n". format(x) + "Dual gap: {}\n".format("%2.2e" % g) + "Optimal cost: {}\n".format("%10.3f" % z) + "Number of iteration: {}".format(it)) return x, s, u, sig #%% ''' ==================== MEHROTRA METHOD test ==================== ''' if __name__ == "__main__": (A, b, c) = input_data(21) # Recall problem data xm, sm, um, sigmam = mehrotra(A, b, c) dm = cent_meas(xm, um, 'Mehrotra', plot=0)
tm = max(m, n, q) if it == max_it: raise TimeoutError("Iterations maxed out") if info == 0: print('Current point:\n x = {} \n lambda = {} \n s = {}.\n'.format(x, y, s)) print('Dual next gap: {}.\n'.format("%10.3f"%g)) #%% print_boxed("Found optimal solution of the problem at\n x* = {}.\n".format(x) + "Dual gap: {}\n".format("%10.6f"%g) + "Optimal cost: {}\n".format("%.8E" %z) + "Number of iterations: {}".format(it)) return x, s, u #%% ''' === PRIMAL-DUAL AFFINE-SCALING METHOD test === ''' if __name__ == "__main__": (A, b, c) = input_data(5) x, s, u = affine(A, b, c, max_it = 1000, info = 1) up = cent_meas(x, u, 'Affine', plot = 0)
print('Tollerance: {}.\n'.format("%10.3f" % tm)) print('CORRECTOR STEP:\nCurrent primal-dual point: \n x = ', x, '\nlambda = ', y, '\n s = ', s) print('Current g: {}\nCurrent g1: {}\n'.format( "%.3f" % g, "%.3f" % g1)) print_boxed( "Found optimal solution of the standard problem at\n x* = {}.\n\n". format(x) + "Dual gap: {}\n".format("%2.2e" % g) + "Optimal cost: {}\n".format("%2.8E" % z) + "Number of iteration: {}".format(it)) return x, s, u, sig #%% """Input data""" # Input data of canonical LP: if __name__ == "__main__": # Input data of canonical LP: (A, b, c) = input_data(20) x, s, u, sigma = mehrotra2(A, b, c, info=1) dm = cent_meas(x, u, 'Mehrotra with augmented system', plot=0)