Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
166 lines (143 loc) · 4.34 KB

File metadata and controls

166 lines (143 loc) · 4.34 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package symjava.examples;
import symjava.relational.Eq;
import symjava.symbolic.*;
import symjava.symbolic.utils.AddList;
import symjava.symbolic.utils.JIT;
import symjava.symbolic.utils.Utils;
import static symjava.math.SymMath.*;
import static symjava.symbolic.Symbol.*;
public class QPSlover {
public static void main(String[] args) {
test0();
test1();
test2(); //?
test3(); //?
}
public static void test0() {
/**
* http://www.akiti.ca/QuadProgEx0Constr.html
minimize F = (5/2)x1^2 -2x1x2 -x1x3 + 2x2^2 + 3x2x3 + (5/2)x3^2 + 2x1 - 35x2 - 47x3 + 5
True Solution: (x1,x2,x3) = (3,5,7)
*/
Symbol x1 = new Symbol("x_1");
Symbol x2 = new Symbol("x_2");
Symbol x3 = new Symbol("x_3");
Expr F = 2.5*x1*x1 -2*x1*x2 -x1*x3 + 2*x2*x2 + 3*x2*x3 + 2.5*x3*x3 + 2*x1 - 35*x2 - 47*x3 + 5 ;
System.out.println(F);
Expr[] freeVars = new Expr[]{ x1, x2, x3 };
Eq eq = new Eq(F, C0, freeVars);
System.out.println(eq);
double[] guess = new double[freeVars.length];
NewtonOptimization.solve(eq, guess, 10, 1e-3, false);
//Verifiy:
double[][] A = {
{ 5,-2,-1},
{-2, 4, 3},
{-1, 3, 5},
};
double[] b = {-2,35,47};
double[] x = {0, 0, 0};
Solver.solveCG2(A, b, x);
for(double i : x)
System.out.println(i);
}
public static void test1() {
/**
* http://doc.cgal.org/latest/QP_solver/index.html
minimize x^2 + 4(y-4)^2
subject to
x + y <= 7
-x+2y <=4
x>=0
y>=0
y<=4
Solution (x,y)=(2,3)
*/
Expr f = x*x + 4*(y-4)*(y-4);
Symbols lmd = new Symbols("\\lambda");
Symbols c = new Symbols("c");
AddList addList = new AddList();
addList.add(lmd[1]*( x+y-7+c[1]*c[1] ));
addList.add(lmd[2]*(-x+2*y-4+c[2]*c[2] ));
addList.add(lmd[3]*( x-c[3]*c[3] ));
addList.add(lmd[4]*( y-c[4]*c[4] ));
addList.add(lmd[5]*( y-4+c[5]*c[5] ));
Expr L = f + addList.toExpr();
System.out.println(L);
Expr[] freeVars = Utils.joinArrays(new Expr[]{x, y}, lmd.get(1, 5), c.get(1, 5));
Eq eq = new Eq(L, C0, freeVars);
System.out.println(eq);
double[] guess = new double[freeVars.length];
NewtonOptimization.solve(eq, guess, 10, 1e-3, false);
double xx = guess[0];
double yy = guess[1];
double objValue = JIT.compile(f).apply(xx,yy);
System.out.println("Objective Value = "+objValue); //=8
}
public static void test2() {
/**
* http://doc.cgal.org/latest/QP_solver/index.html
minimize -32y+64
subject to
x + y <= 7
-x+2y <=4
x>=0
y>=0
y<=4
Solution (x,y)=(10/3, 11/3)
*/
Expr f = -32*y+64;
Symbols lmd = new Symbols("\\lambda");
Symbols c = new Symbols("c");
AddList addList = new AddList();
addList.add(lmd[1]*( x+y-7+c[1]*c[1] ));
addList.add(lmd[2]*(-x+2*y-4+c[2]*c[2] ));
addList.add(lmd[3]*( x-c[3]*c[3] ));
addList.add(lmd[4]*( y-c[4]*c[4] ));
addList.add(lmd[5]*( y-4+c[5]*c[5] ));
Expr L = f + addList.toExpr();
System.out.println(L);
Expr[] freeVars = Utils.joinArrays(new Expr[]{x, y}, lmd.get(1, 5), c.get(1, 5));
Eq eq = new Eq(L, C0, freeVars);
System.out.println(eq);
double[] guess = new double[freeVars.length];
NewtonOptimization.solve(eq, guess, 10, 1e-3, false);
double xx = guess[0];
double yy = guess[1];
double objValue = JIT.compile(new Expr[]{x,y},f).apply(xx,yy);
System.out.println("Objective Value = "+objValue); //=-160/3
}
public static void test3() {
/**
* http://doc.cgal.org/latest/QP_solver/index.html
minimize x^2 + 4(y-4)^2
subject to
x + y <= 7
-x+2y <=4
x>=0
y>=0
//y<=4
Solution (x,y)=(2,3)
*/
Expr f = x*x + 4*(y-4)*(y-4);
Symbols lmd = new Symbols("\\lambda");
Symbols c = new Symbols("c");
AddList addList = new AddList();
addList.add(lmd[1]*( x+y-7+c[1]*c[1] ));
addList.add(lmd[2]*(-x+2*y-4+c[2]*c[2] ));
addList.add(lmd[3]*( x-c[3]*c[3] ));
addList.add(lmd[4]*( y-c[4]*c[4] ));
//addList.add(lmd[5]*( y-4+c[5]*c[5] ));
Expr L = f + addList.toExpr();
System.out.println(L);
Expr[] freeVars = Utils.joinArrays(new Expr[]{x, y}, lmd.get(1, 4), c.get(1, 4));
Eq eq = new Eq(L, C0, freeVars);
System.out.println(eq);
double[] guess = new double[freeVars.length];
NewtonOptimization.solve(eq, guess, 10, 1e-3, false);
double xx = guess[0];
double yy = guess[1];
double objValue = JIT.compile(new Expr[]{x,y},f).apply(xx,yy);
System.out.println("Objective Value = "+objValue); //=8
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.