1+ import numpy as np
2+ import pandas as pd
3+ import pdb
4+
5+ import cvxpy as cp
6+
7+
8+ x = cp .Variable (4 , name = 'x' )
9+ F = cp .hstack ([x [0 ] + 10 * x [1 ],
10+ np .sqrt (5 ) * (x [2 ] - x [3 ]),
11+ cp .square (x [1 ] - 2 * x [2 ]),
12+ np .sqrt (10 ) * (x [0 ] - x [3 ])])
13+
14+
15+ # formulation 1 - works (95 iterations)
16+ objective = cp .sum_squares (F )
17+ problem = cp .Problem (cp .Minimize (objective ))
18+ x .value = np .array ([3 , - 1 , 0 , 1 ])
19+ problem .solve (solver = cp .IPOPT , nlp = True , verbose = True )
20+
21+ # formulation 2 - works (41 iterations)
22+ objective = cp .square (x [0 ] + 10 * x [1 ]) + \
23+ cp .square (np .sqrt (5 ) * (x [2 ] - x [3 ])) + \
24+ cp .power (x [1 ] - 2 * x [2 ], 4 ) + \
25+ cp .square (np .sqrt (10 ) * (x [0 ] - x [3 ]))
26+
27+ problem = cp .Problem (cp .Minimize (objective ))
28+ x .value = np .array ([3 , - 1 , 0 , 1 ])
29+ problem .solve (solver = cp .IPOPT , nlp = True , verbose = True )
30+
31+
32+ # formulation 3 - works (18 iterations)
33+ objective = cp .square (x [0 ] + 10 * x [1 ]) + \
34+ cp .square (np .sqrt (5 ) * (x [2 ] - x [3 ])) + \
35+ cp .square (cp .square (x [1 ] - 2 * x [2 ])) + \
36+ cp .square (np .sqrt (10 ) * (x [0 ] - x [3 ]))
37+
38+ problem = cp .Problem (cp .Minimize (objective ))
39+ x .value = np .array ([3 , - 1 , 0 , 1 ])
40+ problem .solve (solver = cp .IPOPT , nlp = True , verbose = True )
41+
42+ # formulation 4 - works (18 iterations)
43+ objective = cp .power (x [0 ] + 10 * x [1 ], 2 ) + \
44+ cp .power (np .sqrt (5 ) * (x [2 ] - x [3 ]), 2 ) + \
45+ cp .square (cp .square (x [1 ] - 2 * x [2 ])) + \
46+ cp .power (np .sqrt (10 ) * (x [0 ] - x [3 ]), 2 )
47+
48+ problem = cp .Problem (cp .Minimize (objective ))
49+ x .value = np .array ([3 , - 1 , 0 , 1 ])
50+ problem .solve (solver = cp .IPOPT , nlp = True , verbose = True )
51+
52+ # formulation 5 - canonicalization fails
53+ #objective = cp.norm(F)
54+ #problem = cp.Problem(cp.Minimize(objective))
55+ #problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
56+ #pdb.set_trace()
0 commit comments