Skip to content

Commit b8d4ea1

Browse files
committed
virson 2
1 parent 0339dc8 commit b8d4ea1

File tree

6 files changed

+210
-1
lines changed

6 files changed

+210
-1
lines changed

Visual/binary_tree.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
#
3+
# class Node:
4+
# def __init__(self, data):
5+
# self.left = None
6+
# self.right = None
7+
# self.data = data
8+
#
9+
# def insert(self, data):
10+
# # Compare the new value with the parent node
11+
# if self.data:
12+
# if data < self.data:
13+
# if self.left is None:
14+
# self.left = Node(data)
15+
# else:
16+
# self.left.insert(data)
17+
# elif data > self.data:
18+
# if self.right is None:
19+
# self.right = Node(data)
20+
# else:
21+
# self.right.insert(data)
22+
# else:
23+
# self.data = data
24+
#
25+
# # Print the tree
26+
# def PrintTree(self):
27+
# if self.left:
28+
# self.left.PrintTree()
29+
# print( self.data),
30+
# if self.right:
31+
# self.right.PrintTree()

Visual/test.py

-1
This file was deleted.

Visual/visual.py

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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)

analyzer/solvers.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from cvxpy.error import DCPError
1414
import cvxpy as cp
1515

16+
from Visual.visual import prob
17+
1618

1719
def get_solvers(problem):
1820
"""Get valid solvers.

analyzer/version.py

+3
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ def check_version():
2727
else:
2828
msg = "The request to pypi returned status code" + str(r.status_code)
2929
raise RuntimeError(msg)
30+
31+
32+
check_version()

tests/test_basics.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ def test_solvers():
2626
prob = cp.Problem(cp.Minimize(cp.sum_squares(x)), [-1 <= x, x <= 1])
2727
solvers = get_solvers(prob)
2828
assert len(solvers) > 0
29+
30+
31+
test_solvers()
32+
test_checker()
33+
test_version()

0 commit comments

Comments
 (0)