|
| 1 | +import re |
| 2 | +from email.headerregistry import Group |
| 3 | +import cvxpy as cp |
| 4 | +from cvxpy import Expression |
| 5 | + |
| 6 | +import cvxopt |
| 7 | + |
| 8 | +from cvxpy import Minimize, Problem, Variable, quad_form |
| 9 | +from cvxpy.problems.objective import Objective |
| 10 | + |
| 11 | +import operator |
| 12 | + |
| 13 | +priority0 = ['**'] |
| 14 | +priority1 = ['@', '*', '/'] |
| 15 | +priority2 = ['+', '-'] |
| 16 | + |
| 17 | + |
| 18 | +def is_operator(s): |
| 19 | + try: |
| 20 | + return hasattr(operator, s) |
| 21 | + except TypeError: |
| 22 | + return False |
| 23 | + |
| 24 | + |
| 25 | +def is_float(s): |
| 26 | + try: |
| 27 | + float(s) |
| 28 | + return True |
| 29 | + except ValueError: |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +class Visual: |
| 34 | + def __init__(self, obj: Objective): |
| 35 | + self.name = obj.NAME |
| 36 | + self.expr = str(obj.expr).replace("+ -", " - ") |
| 37 | + self.parameters = [] |
| 38 | + self.variables = [] |
| 39 | + self.operators = [] |
| 40 | + self.func = [] |
| 41 | + |
| 42 | + def create_lists(self): |
| 43 | + isFunc = False |
| 44 | + isMatrix = False |
| 45 | + func_string = "" |
| 46 | + matrix_string = "" |
| 47 | + for s in self.expr.split(): |
| 48 | + # --------func--------- |
| 49 | + if ')' in s: |
| 50 | + isFunc = False |
| 51 | + func_string += " " + s |
| 52 | + self.func.append(func_string) |
| 53 | + func_string = "" |
| 54 | + continue |
| 55 | + if isFunc: |
| 56 | + func_string += " " + s |
| 57 | + continue |
| 58 | + if '(' in s: |
| 59 | + isFunc = True |
| 60 | + func_string += s |
| 61 | + continue |
| 62 | + # --------func--------- |
| 63 | + |
| 64 | + # --------parameter--------- |
| 65 | + if is_float(s) and not isMatrix: |
| 66 | + self.parameters.append(s) |
| 67 | + continue |
| 68 | + if ']' in s: |
| 69 | + isMatrix = False |
| 70 | + matrix_string += " " + s |
| 71 | + self.parameters.append(matrix_string) |
| 72 | + matrix_string = "" |
| 73 | + continue |
| 74 | + if isMatrix: |
| 75 | + matrix_string += " " + s |
| 76 | + continue |
| 77 | + if '[' in s: |
| 78 | + isMatrix = True |
| 79 | + matrix_string += s |
| 80 | + continue |
| 81 | + # --------parameter--------- |
| 82 | + |
| 83 | + # --------variables--------- |
| 84 | + if "var" in s: |
| 85 | + self.variables.append(s) |
| 86 | + continue |
| 87 | + # --------variables--------- |
| 88 | + |
| 89 | + self.operators.append(s) |
| 90 | + |
| 91 | + def split_expr(self): |
| 92 | + if len(self.operators) == 0: |
| 93 | + return |
| 94 | + |
| 95 | + def show(self): |
| 96 | + pass |
| 97 | + |
| 98 | + def priority(self): |
| 99 | + pr = 0 |
| 100 | + ans = '' |
| 101 | + for op in self.operators: |
| 102 | + if op in priority2: |
| 103 | + self.operators.remove(op) |
| 104 | + return op |
| 105 | + if op in priority1: |
| 106 | + pr = 1 |
| 107 | + ans = op |
| 108 | + if op in priority0 and pr == 0: |
| 109 | + ans = op |
| 110 | + self.operators.remove(ans) |
| 111 | + return ans |
| 112 | + |
| 113 | + |
| 114 | +# def visual(objective): |
| 115 | +# str_objective = str(objective.expr) |
| 116 | +# print(str_objective) |
| 117 | +# split_objective = str_objective.split() |
| 118 | +# print(objective.NAME) |
| 119 | +# print(objective.args) |
| 120 | + |
| 121 | + |
| 122 | +# n = 4 |
| 123 | +# A = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] |
| 124 | +# b = [1, 2, 3] |
| 125 | +# x = cp.Variable(n) |
| 126 | +# y = cp.Variable() |
| 127 | +# |
| 128 | +# objective = cp.Minimize(cp.sum_squares(A@x+b+y)) |
| 129 | +# |
| 130 | +# constraints = [x <= 0, x <= 1, y <= 1] |
| 131 | +# prob = cp.Problem(objective, constraints) |
| 132 | +# prob.solve() |
| 133 | +# |
| 134 | +# visual(objective) |
| 135 | + |
| 136 | +n = 3 |
| 137 | +P = cvxopt.matrix([13, 12, -2, |
| 138 | + 12, 17, 6, |
| 139 | + -2, 6, 12], (n, n)) |
| 140 | +q = cvxopt.matrix([-22, -14.5, 13], (n, 1)) |
| 141 | +r = 1 |
| 142 | +x_star = cvxopt.matrix([1, 1 / 2, -1], (n, 1)) |
| 143 | + |
| 144 | +# Frame and solve the problem |
| 145 | + |
| 146 | +x = Variable(n) |
| 147 | +y = Variable(n) |
| 148 | +objective = Minimize(0.5 * quad_form(x, P) + cp.sum_squares(x) - q.T @ x + r) |
| 149 | +constraints = [x >= -1, x <= 1] |
| 150 | + |
| 151 | +p = Problem(objective, constraints) |
| 152 | +# The optimal objective is returned by p.solve(). |
| 153 | +result = p.solve() |
| 154 | +print(str(objective.expr)) |
| 155 | + |
| 156 | +# expression = "2 * math.pow(3, 2) + math.sqrt(16)" |
| 157 | +# tokens = re.findall('[\d\.]+|\(|\)|\+|\-|\@|\/|\^|\w+\(', str(objective.expr)) |
| 158 | +# |
| 159 | +# print(tokens) |
| 160 | + |
| 161 | +for s in str(objective.expr).split(): |
| 162 | + print("s= ", s) |
| 163 | + |
| 164 | +v = Visual(objective) |
| 165 | +v.split_expr() |
| 166 | +print(v.func) |
| 167 | +print(v.parameters) |
| 168 | +print(v.variables) |
| 169 | +print(v.operators) |
0 commit comments