├── .gitattributes ├── README.md ├── data ├── A.txt ├── b.txt └── c.txt ├── src ├── Gradient.py ├── Hessian.py ├── Newton.py └── ReadFile.py └── 凸优化算法 I: 内点法(interior point method)求解线性规划问题.pdf /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # linear-programming 2 | interior point method for linear programming 3 | See the link below for a program documentation(in Chinese): 4 | https://github.com/PrimerLi/linear-programming/blob/master/%E5%87%B8%E4%BC%98%E5%8C%96%E7%AE%97%E6%B3%95%20I:%20%E5%86%85%E7%82%B9%E6%B3%95(interior%20point%20method)%E6%B1%82%E8%A7%A3%E7%BA%BF%E6%80%A7%E8%A7%84%E5%88%92%E9%97%AE%E9%A2%98.pdf 5 | 6 | -------------------------------------------------------------------------------- /data/A.txt: -------------------------------------------------------------------------------- 1 | 4 2 2 | 0.81 0.4 3 | 0.4 1.1 4 | -1 0 5 | 0 -1 6 | -------------------------------------------------------------------------------- /data/b.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 1 3 | 1 4 | 0 5 | 0 6 | -------------------------------------------------------------------------------- /data/c.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /src/Gradient.py: -------------------------------------------------------------------------------- 1 | import ReadFile 2 | import numpy as np 3 | 4 | def gradient(t, A, b, c, variables): 5 | numberOfVariables = len(variables) 6 | n = len(c) 7 | m = len(b) 8 | (row, col) = A.shape 9 | assert(row == m) # number of constraints 10 | assert(col == n) # number of variables 11 | assert(numberOfVariables == n) 12 | 13 | slack = A.dot(variables) - b 14 | vector = np.zeros(m) 15 | for i in range(m): 16 | vector[i] = 1.0/slack[i] 17 | g = t*c - A.transpose().dot(vector) 18 | return g 19 | 20 | def main(): 21 | import os 22 | import sys 23 | 24 | A, b, c = ReadFile.read("../data/A.txt", "../data/b.txt", "../data/c.txt") 25 | x = np.zeros(len(c)) 26 | x[0] = 0.1 27 | x[1] = 0.2 28 | t = 1.0 29 | print "Gradient = ", gradient(t, A, b, c, x) 30 | return 0 31 | 32 | if __name__ == "__main__": 33 | import sys 34 | sys.exit(main()) 35 | -------------------------------------------------------------------------------- /src/Hessian.py: -------------------------------------------------------------------------------- 1 | import ReadFile 2 | import numpy as np 3 | 4 | def Hessian(A, b, x): 5 | (row, col) = A.shape 6 | m = len(b) # number of constraints 7 | n = len(x) # number of variables 8 | assert(m == row) 9 | assert(n == col) 10 | slack = A.dot(x) - b 11 | vector = np.zeros(len(slack)) 12 | for i in range(len(vector)): 13 | vector[i] = 1.0/(slack[i]**2) 14 | D = np.diag(vector) 15 | return A.transpose().dot(D).dot(A) 16 | 17 | def main(): 18 | import os 19 | import sys 20 | 21 | A, b, c = ReadFile.read("../data/A.txt", "../data/b.txt", "../data/c.txt") 22 | x = np.zeros(len(c)) 23 | x[0] = 0.1 24 | x[1] = 0.2 25 | H = Hessian(A, b, x) 26 | ReadFile.printMatrix(H) 27 | return 0 28 | 29 | if __name__ == "__main__": 30 | import sys 31 | sys.exit(main()) 32 | -------------------------------------------------------------------------------- /src/Newton.py: -------------------------------------------------------------------------------- 1 | ''' 2 | CopyRight 2018 3 | @author Enzhi Li 4 | All rights reserved. 5 | ''' 6 | 7 | import numpy as np 8 | import ReadFile 9 | import Gradient 10 | import Hessian 11 | 12 | def feasible(A, x, b): 13 | (row, col) = A.shape 14 | assert(col == len(x)) 15 | assert(row == len(b)) 16 | slack = A.dot(x) - b 17 | for i in range(len(slack)): 18 | if (slack[i] > 0): 19 | return False 20 | return True 21 | 22 | def invertible(matrix): 23 | (row, col) = matrix.shape 24 | assert(row == col) 25 | inverse = np.linalg.inv(matrix) 26 | identity = np.diag(np.ones(row)) 27 | eps = 1.0e-10 28 | return np.linalg.norm(identity - inverse.dot(matrix)) < eps 29 | 30 | def newton(t, A, b, c, x0): 31 | assert(feasible(A, x0, b)) 32 | maxIterationNumber = 20 33 | eps = 1.0e-8 34 | count = 0 35 | while(True): 36 | count = count + 1 37 | if (count > maxIterationNumber): 38 | break 39 | H = Hessian.Hessian(A, b, x0) 40 | gradient = Gradient.gradient(t, A, b, c, x0) 41 | assert(invertible(H)) 42 | inverseOfHessian = np.linalg.inv(H) 43 | x = x0 - inverseOfHessian.dot(gradient) 44 | error = np.linalg.norm(x - x0) 45 | print "count = ", count, ", error = ", error 46 | x0 = x 47 | assert(feasible(A, x0, b)) 48 | if (error < eps): 49 | break 50 | return x0 51 | 52 | def printSolutions(solutions, outputFileName): 53 | 54 | def toString(vector): 55 | result = "" 56 | for i in range(len(vector)): 57 | result = result + str(vector[i]) + " " 58 | return result 59 | 60 | ofile = open(outputFileName, "w") 61 | for i in range(len(solutions)): 62 | ofile.write(toString(solutions[i]) + "\n") 63 | ofile.close() 64 | 65 | def solver(t0, A, b, c, x0, factor, solutionRecords): 66 | assert(factor > 1) 67 | assert(t0 > 0) 68 | upperBound = 1000*t0 69 | t = [] 70 | t.append(t0) 71 | solutions = [] 72 | assert(feasible(A, x0, b)) 73 | solutions.append(x0) 74 | eps = 1.0e-10 75 | while(t0 < upperBound): 76 | solution = newton(t0, A, b, c, x0) 77 | isFeasible = feasible(A, solution, b) 78 | if (isFeasible): 79 | diff = solution - x0 80 | error = np.linalg.norm(diff) 81 | solutions.append(solution) 82 | print solution 83 | x0 = solution 84 | t0 = factor*t0 85 | t.append(t0) 86 | print "t = ", t0, ", error = ", error 87 | if (error < eps): 88 | break 89 | else: 90 | break 91 | printSolutions(solutions, solutionRecords) 92 | return x0 93 | 94 | def main(): 95 | import os 96 | import sys 97 | import random 98 | 99 | A, b, c = ReadFile.read("../data/A.txt", "../data/b.txt", "../data/c.txt") 100 | t = 1.0 101 | initials = [] 102 | numberOfTests = 4 103 | for i in range(numberOfTests): 104 | x0 = np.zeros(len(c)) 105 | for i in range(len(x0)): 106 | x0[i] = 0.4 + random.uniform(-0.3, 0.3) 107 | initials.append(x0) 108 | for i in range(len(initials)): 109 | print "******************* Test number = "+ str(i + 1) + " **************************" 110 | factor = 1.2 111 | solutionRecords = "solutions_" + str(i+1) + ".txt" 112 | solution = solver(t, A, b, c, initials[i], factor, solutionRecords) 113 | print "Solution to the problem is: " 114 | print solution 115 | print "****************************************************************" 116 | return 0 117 | 118 | if __name__ == "__main__": 119 | import sys 120 | sys.exit(main()) 121 | -------------------------------------------------------------------------------- /src/ReadFile.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def readMatrix(inputFileName): 4 | import os 5 | assert(os.path.exists(inputFileName)) 6 | ifile = open(inputFileName, "r") 7 | string = ifile.readline() 8 | a = string.split() 9 | row = int(a[0]) 10 | col = int(a[1]) 11 | A = np.zeros((row, col)) 12 | for i in range(row): 13 | string = ifile.readline() 14 | a = string.split() 15 | for j in range(len(a)): 16 | A[i, j] = float(a[j]) 17 | ifile.close() 18 | return A 19 | 20 | def readVector(inputFileName): 21 | import os 22 | assert(os.path.exists(inputFileName)) 23 | ifile = open(inputFileName, "r") 24 | line = ifile.readline().strip() 25 | length = int(line) 26 | vector = np.zeros(length) 27 | for i in range(length): 28 | line = ifile.readline().strip() 29 | vector[i] = float(line) 30 | ifile.close() 31 | return vector 32 | 33 | def printMatrix(matrix): 34 | (row, col) = matrix.shape 35 | for i in range(row): 36 | for j in range(col): 37 | print matrix[i, j], " ", 38 | print "\n", 39 | 40 | def read(AFile, bFile, cFile): 41 | import os 42 | 43 | assert(os.path.exists(AFile)) 44 | assert(os.path.exists(bFile)) 45 | assert(os.path.exists(cFile)) 46 | 47 | A = readMatrix(AFile) 48 | b = readVector(bFile) 49 | c = readVector(cFile) 50 | 51 | if (False): 52 | print "c = ", c 53 | print "A = " 54 | printMatrix(A) 55 | print "b = ", b 56 | 57 | return A, b, c 58 | 59 | def main(): 60 | A, b, c = read("../data/A.txt", "../data/b.txt", "../data/c.txt") 61 | if (True): 62 | print "c = ", c 63 | print "A = " 64 | printMatrix(A) 65 | print "b = ", b 66 | 67 | if __name__ == "__main__": 68 | import sys 69 | sys.exit(main()) 70 | -------------------------------------------------------------------------------- /凸优化算法 I: 内点法(interior point method)求解线性规划问题.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrimerLi/linear-programming/51b766301b5915cd2b0d0759e7eeed749ef74b7e/凸优化算法 I: 内点法(interior point method)求解线性规划问题.pdf --------------------------------------------------------------------------------