-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2.3.py
50 lines (38 loc) · 1.05 KB
/
Q2.3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Q2.3
import cvxpy as cp
# Create two variables and one parameter.
x = cp.Variable()
y = cp.Variable()
t = cp.Parameter(nonneg=True, value=1)
# no constraints.
constraints = None
sqr=cp.square(x-2)
log=cp.log(-4+x+y)
log=log/t
F= sqr + (3*y) - log
# Form objective
obj = cp.Minimize(F)
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve() # Returns the optimal value.
new_v=prob.value
old_v=6
print("it takes few sec to find best value of t")
while(new_v<old_v): # it tries to increase t to find the optimal t
t.value=t.value+100
sqr=cp.square(x-2)
log=cp.log(-4+x+y)
log=log/t
F= sqr + (3*y) - log
# Form objective
obj = cp.Minimize(F)
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve() # Returns the optimal value.
old_v=new_v
new_v=prob.value
print("optimal value of t is:", t.value)
print("The status of the problem is:", prob.status)
print("optimal value of minimization is:", prob.value)
print("optimal value of variable x is :", x.value)
print("optimal value of variable y is :", y.value)