Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
963bc0fe
Commit
963bc0fe
authored
Mar 30, 2018
by
Davis King
Browse files
merged
parents
973a3da3
9f53eac2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
23 deletions
+80
-23
dlib/optimization/optimization_solve_qp_using_smo.h
dlib/optimization/optimization_solve_qp_using_smo.h
+27
-9
dlib/optimization/optimization_solve_qp_using_smo_abstract.h
dlib/optimization/optimization_solve_qp_using_smo_abstract.h
+12
-12
dlib/test/mpc.cpp
dlib/test/mpc.cpp
+2
-2
dlib/test/opt_qp_solver.cpp
dlib/test/opt_qp_solver.cpp
+39
-0
No files found.
dlib/optimization/optimization_solve_qp_using_smo.h
View file @
963bc0fe
...
@@ -419,8 +419,8 @@ namespace dlib
...
@@ -419,8 +419,8 @@ namespace dlib
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
alpha
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
alpha
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
lower
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
lower
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
upper
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
upper
,
T
eps
,
T
eps
=
1e-10
,
unsigned
long
max_iter
unsigned
long
max_iter
=
30000
)
)
{
{
// make sure requires clause is not broken
// make sure requires clause is not broken
...
@@ -521,26 +521,35 @@ namespace dlib
...
@@ -521,26 +521,35 @@ namespace dlib
v_old
=
v
;
v_old
=
v
;
df
=
Q
*
alpha
+
b
;
// now take a projected gradient step using Nesterov's method.
// now take a projected gradient step using Nesterov's method.
v
=
clamp
(
alpha
-
1.0
/
lipschitz_bound
*
df
,
lower
,
upper
);
v
=
clamp
(
alpha
-
1.0
/
lipschitz_bound
*
df
,
lower
,
upper
);
alpha
=
dlib
::
clamp
((
1
-
gamma
)
*
v
+
gamma
*
v_old
,
lower
,
upper
);
alpha
=
dlib
::
clamp
((
1
-
gamma
)
*
v
+
gamma
*
v_old
,
lower
,
upper
);
df
=
Q
*
alpha
+
b
;
// check for convergence every 10 iterations
// check for convergence every 10 iterations
if
(
iter
%
10
==
0
)
if
(
iter
%
10
==
0
)
{
{
max_df
=
0
;
max_df
=
0
;
double
absalpha
=
0
;
double
thealpha
=
0
;
for
(
long
r
=
0
;
r
<
Q
.
nr
();
++
r
)
for
(
long
r
=
0
;
r
<
Q
.
nr
();
++
r
)
{
{
absalpha
+=
std
::
abs
(
alpha
(
r
));
if
(
alpha
(
r
)
<=
lower
(
r
)
&&
df
(
r
)
>
0
)
if
(
alpha
(
r
)
<=
lower
(
r
)
&&
df
(
r
)
>
0
)
;
//alpha(r) = lower(r);
;
//alpha(r) = lower(r);
else
if
(
alpha
(
r
)
>=
upper
(
r
)
&&
df
(
r
)
<
0
)
else
if
(
alpha
(
r
)
>=
upper
(
r
)
&&
df
(
r
)
<
0
)
;
//alpha(r) = upper(r);
;
//alpha(r) = upper(r);
else
if
(
std
::
abs
(
df
(
r
))
>
max_df
)
else
if
(
std
::
abs
(
df
(
r
))
>
max_df
)
{
max_df
=
std
::
abs
(
df
(
r
));
max_df
=
std
::
abs
(
df
(
r
));
thealpha
=
std
::
abs
(
alpha
(
r
));
}
}
}
if
(
max_df
<
eps
)
absalpha
/=
Q
.
nr
();
// Stop when the magnitude of the changes we are making to alpha are eps
// smaller than the typical alpha.
if
(
max_df
/
lipschitz_bound
<=
eps
*
std
::
min
(
thealpha
,
absalpha
))
break
;
break
;
}
}
}
}
...
@@ -613,8 +622,8 @@ namespace dlib
...
@@ -613,8 +622,8 @@ namespace dlib
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
alphas
,
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
alphas
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
lowers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
lowers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
uppers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
uppers
,
T
eps
,
T
eps
=
1e-10
,
unsigned
long
max_iter
unsigned
long
max_iter
=
30000
)
)
{
{
// make sure requires clause is not broken
// make sure requires clause is not broken
...
@@ -834,8 +843,6 @@ namespace dlib
...
@@ -834,8 +843,6 @@ namespace dlib
v_old
.
swap
(
v
);
v_old
.
swap
(
v
);
//df = Q*alpha + b;
compute_df
();
// now take a projected gradient step using Nesterov's method.
// now take a projected gradient step using Nesterov's method.
for
(
size_t
j
=
0
;
j
<
alphas
.
size
();
++
j
)
for
(
size_t
j
=
0
;
j
<
alphas
.
size
();
++
j
)
...
@@ -844,11 +851,15 @@ namespace dlib
...
@@ -844,11 +851,15 @@ namespace dlib
alphas
[
j
]
=
clamp
((
1
-
gamma
)
*
v
[
j
]
+
gamma
*
v_old
[
j
],
lowers
[
j
],
uppers
[
j
]);
alphas
[
j
]
=
clamp
((
1
-
gamma
)
*
v
[
j
]
+
gamma
*
v_old
[
j
],
lowers
[
j
],
uppers
[
j
]);
}
}
//df = Q*alpha + b;
compute_df
();
// check for convergence every 10 iterations
// check for convergence every 10 iterations
if
(
iter
%
10
==
0
)
if
(
iter
%
10
==
0
)
{
{
max_df
=
0
;
max_df
=
0
;
double
absalpha
=
0
;
double
thealpha
=
0
;
for
(
size_t
r2
=
0
;
r2
<
alphas
.
size
();
++
r2
)
for
(
size_t
r2
=
0
;
r2
<
alphas
.
size
();
++
r2
)
{
{
auto
&
alpha
=
alphas
[
r2
];
auto
&
alpha
=
alphas
[
r2
];
...
@@ -857,15 +868,22 @@ namespace dlib
...
@@ -857,15 +868,22 @@ namespace dlib
auto
&
upper
=
uppers
[
r2
];
auto
&
upper
=
uppers
[
r2
];
for
(
long
r
=
0
;
r
<
alpha
.
nr
();
++
r
)
for
(
long
r
=
0
;
r
<
alpha
.
nr
();
++
r
)
{
{
absalpha
+=
std
::
abs
(
alpha
(
r
));
if
(
alpha
(
r
)
<=
lower
(
r
)
&&
df_
(
r
)
>
0
)
if
(
alpha
(
r
)
<=
lower
(
r
)
&&
df_
(
r
)
>
0
)
;
//alpha(r) = lower(r);
;
//alpha(r) = lower(r);
else
if
(
alpha
(
r
)
>=
upper
(
r
)
&&
df_
(
r
)
<
0
)
else
if
(
alpha
(
r
)
>=
upper
(
r
)
&&
df_
(
r
)
<
0
)
;
//alpha(r) = upper(r);
;
//alpha(r) = upper(r);
else
if
(
std
::
abs
(
df_
(
r
))
>
max_df
)
else
if
(
std
::
abs
(
df_
(
r
))
>
max_df
)
{
max_df
=
std
::
abs
(
df_
(
r
));
max_df
=
std
::
abs
(
df_
(
r
));
thealpha
=
std
::
abs
(
alpha
(
r
));
}
}
}
}
}
if
(
max_df
<
eps
)
absalpha
/=
num_variables
;
// Stop when the magnitude of the changes we are making to alpha are eps
// smaller than the typical alpha.
if
(
max_df
/
lipschitz_bound
<=
eps
*
std
::
min
(
thealpha
,
absalpha
))
break
;
break
;
}
}
}
}
...
...
dlib/optimization/optimization_solve_qp_using_smo_abstract.h
View file @
963bc0fe
...
@@ -128,8 +128,8 @@ namespace dlib
...
@@ -128,8 +128,8 @@ namespace dlib
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
alpha
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
alpha
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
lower
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
lower
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
upper
,
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
upper
,
T
eps
,
T
eps
=
1e-10
,
unsigned
long
max_iter
unsigned
long
max_iter
=
30000
);
);
/*!
/*!
requires
requires
...
@@ -155,10 +155,10 @@ namespace dlib
...
@@ -155,10 +155,10 @@ namespace dlib
- The solution to the above QP will be stored in #alpha.
- The solution to the above QP will be stored in #alpha.
- This function uses a combination of a SMO algorithm along with Nesterov's
- This function uses a combination of a SMO algorithm along with Nesterov's
method as the main iteration of the solver. It starts the algorithm with the
method as the main iteration of the solver. It starts the algorithm with the
given alpha and
it
works on the problem until the
derivative of f(alpha) is
given alpha and works on the problem until the
magnitude of the changes we
smaller than eps for each element of alpha or the alpha
value i
s
a
t a box
are making to alpha are eps times smaller than the typical
value
s
i
n
a
lpha.
constraint.
So eps controls how accurate the solution is and smaller values
So eps controls how accurate the solution is and smaller values
result in
result in
better solutions.
better solutions.
- At most max_iter iterations of optimization will be performed.
- At most max_iter iterations of optimization will be performed.
- returns the number of iterations performed. If this method fails to
- returns the number of iterations performed. If this method fails to
converge to eps accuracy then the number returned will be max_iter+1.
converge to eps accuracy then the number returned will be max_iter+1.
...
@@ -176,8 +176,8 @@ namespace dlib
...
@@ -176,8 +176,8 @@ namespace dlib
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
alphas
,
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
alphas
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
lowers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
lowers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
uppers
,
const
std
::
vector
<
matrix
<
T
,
NR
,
NC
,
MM
,
L
>>&
uppers
,
T
eps
,
T
eps
=
1e-10
,
unsigned
long
max_iter
unsigned
long
max_iter
=
30000
);
);
/*!
/*!
requires
requires
...
@@ -224,10 +224,10 @@ namespace dlib
...
@@ -224,10 +224,10 @@ namespace dlib
- The solution to the above QP will be stored in #alphas.
- The solution to the above QP will be stored in #alphas.
- This function uses a combination of a SMO algorithm along with Nesterov's
- This function uses a combination of a SMO algorithm along with Nesterov's
method as the main iteration of the solver. It starts the algorithm with the
method as the main iteration of the solver. It starts the algorithm with the
given alpha and
it
works on the problem until the
derivative of f(alpha) is
given alpha and works on the problem until the
magnitude of the changes we
smaller than eps for each element of alpha or the alpha
value i
s
a
t a box
are making to alpha are eps times smaller than the typical
value
s
i
n
a
lpha.
constraint.
So eps controls how accurate the solution is and smaller values
So eps controls how accurate the solution is and smaller values
result in
result in
better solutions.
better solutions.
- At most max_iter iterations of optimization will be performed.
- At most max_iter iterations of optimization will be performed.
- returns the number of iterations performed. If this method fails to
- returns the number of iterations performed. If this method fails to
converge to eps accuracy then the number returned will be max_iter+1.
converge to eps accuracy then the number returned will be max_iter+1.
...
...
dlib/test/mpc.cpp
View file @
963bc0fe
...
@@ -328,8 +328,8 @@ namespace
...
@@ -328,8 +328,8 @@ namespace
lower
=
-
4
;
lower
=
-
4
;
upper
=
3
;
upper
=
3
;
solve_qp_box_using_smo
(
Q
,
b
,
alpha
,
lower
,
upper
,
0.000000001
,
500000
);
solve_qp_box_using_smo
(
Q
,
b
,
alpha
,
lower
,
upper
,
1e-12
,
500000
);
solve_qp_box_constrained
(
Q
,
b
,
alpha2
,
lower
,
upper
,
0.000000001
,
50000
);
solve_qp_box_constrained
(
Q
,
b
,
alpha2
,
lower
,
upper
,
1e-12
,
50000
);
dlog
<<
LINFO
<<
trans
(
alpha
);
dlog
<<
LINFO
<<
trans
(
alpha
);
dlog
<<
LINFO
<<
trans
(
alpha2
);
dlog
<<
LINFO
<<
trans
(
alpha2
);
dlog
<<
LINFO
<<
"objective value: "
<<
0.5
*
trans
(
alpha
)
*
Q
*
alpha
+
trans
(
b
)
*
alpha
;
dlog
<<
LINFO
<<
"objective value: "
<<
0.5
*
trans
(
alpha
)
*
Q
*
alpha
+
trans
(
b
)
*
alpha
;
...
...
dlib/test/opt_qp_solver.cpp
View file @
963bc0fe
...
@@ -590,6 +590,44 @@ namespace
...
@@ -590,6 +590,44 @@ namespace
}
}
}
}
// ----------------------------------------------------------------------------------------
void
test_solve_qp_box_constrained
()
{
dlog
<<
LINFO
<<
"test_solve_qp_box_constrained()"
;
print_spinner
();
dlib
::
rand
rnd
;
matrix
<
double
>
m
=
randm
(
6
,
6
,
rnd
);
m
=
m
*
trans
(
m
);
matrix
<
double
,
0
,
1
>
b
=
randm
(
6
,
1
,
rnd
)
-
0.5
;
matrix
<
double
,
0
,
1
>
lower
,
upper
,
solution
;
lower
=
-
ones_matrix
<
double
>
(
6
,
1
)
*
1e100
;
upper
=
ones_matrix
<
double
>
(
6
,
1
)
*
1e100
;
solution
=
zeros_matrix
(
lower
);
unsigned
long
iters
=
solve_qp_box_constrained
(
m
,
b
,
solution
,
lower
,
upper
);
dlog
<<
LINFO
<<
"iters: "
<<
iters
;
matrix
<
double
>
true_solution
=
-
pinv
(
m
)
*
b
;
DLIB_TEST_MSG
(
max
(
abs
(
solution
-
true_solution
))
<
1e-6
,
max
(
abs
(
solution
-
true_solution
)));
iters
=
solve_qp_box_constrained
(
m
,
b
,
solution
,
lower
,
upper
,
1e-14
);
dlog
<<
LINFO
<<
"iters: "
<<
iters
;
/*
const double obj1 = 0.5*trans(solution)*m*solution + trans(solution)*b;
const double obj2 = 0.5*trans(true_solution)*m*true_solution + trans(true_solution)*b;
cout << "iters:" << iters << endl;
cout << "obj1: "<< obj1 << endl;
cout << "obj2: "<< obj2 << endl;
cout << "obj1-obj2: "<< obj1-obj2 << endl;
*/
DLIB_TEST_MSG
(
max
(
abs
(
solution
-
true_solution
))
<
1e-10
,
max
(
abs
(
solution
-
true_solution
)));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
void
test_solve_qp_box_constrained_blockdiag_compact
(
dlib
::
rand
&
rnd
,
double
percent_off_diag_present
)
void
test_solve_qp_box_constrained_blockdiag_compact
(
dlib
::
rand
&
rnd
,
double
percent_off_diag_present
)
...
@@ -706,6 +744,7 @@ namespace
...
@@ -706,6 +744,7 @@ namespace
void
perform_test
(
void
perform_test
(
)
)
{
{
test_solve_qp_box_constrained
();
print_spinner
();
print_spinner
();
test_solve_qp4_using_smo
();
test_solve_qp4_using_smo
();
print_spinner
();
print_spinner
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment