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
c9bdb9b2
"tools/python/vscode:/vscode.git/clone" did not exist on "6b581d91f6b9b847a8163420630ef947e7cc88db"
Commit
c9bdb9b2
authored
Aug 09, 2013
by
Davis King
Browse files
Added python bindings for the max_cost_assignment() and assignment_cost() routines.
parent
b68e5a37
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
tools/python/src/other.cpp
tools/python/src/other.cpp
+62
-0
No files found.
tools/python/src/other.cpp
View file @
c9bdb9b2
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
#include <dlib/sparse_vector.h>
#include <dlib/sparse_vector.h>
#include <boost/python/args.hpp>
#include <boost/python/args.hpp>
#include "pyassert.h"
#include "pyassert.h"
#include <dlib/optimization.h>
#include "boost_python_utils.h"
using
namespace
dlib
;
using
namespace
dlib
;
using
namespace
std
;
using
namespace
std
;
...
@@ -50,10 +52,70 @@ void _save_libsvm_formatted_data (
...
@@ -50,10 +52,70 @@ void _save_libsvm_formatted_data (
save_libsvm_formatted_data
(
file_name
,
samples
,
labels
);
save_libsvm_formatted_data
(
file_name
,
samples
,
labels
);
}
}
// ----------------------------------------------------------------------------------------
list
_max_cost_assignment
(
const
matrix
<
double
>&
cost
)
{
// max_cost_assignment() only works with integer matrices, so convert from
// double to integer.
const
double
scale
=
(
std
::
numeric_limits
<
dlib
::
int64
>::
max
()
/
1000
)
/
max
(
abs
(
cost
));
matrix
<
dlib
::
int64
>
int_cost
=
matrix_cast
<
dlib
::
int64
>
(
round
(
cost
*
scale
));
return
vector_to_python_list
(
max_cost_assignment
(
int_cost
));
}
double
_assignment_cost
(
const
matrix
<
double
>&
cost
,
const
list
&
assignment
)
{
return
assignment_cost
(
cost
,
python_list_to_vector
<
long
>
(
assignment
));
}
// ----------------------------------------------------------------------------------------
void
bind_other
()
void
bind_other
()
{
{
using
boost
::
python
::
arg
;
using
boost
::
python
::
arg
;
def
(
"max_cost_assignment"
,
_max_cost_assignment
,
(
arg
(
"cost"
)),
"requires
\n
\
- cost.nr() == cost.nc()
\n
\
(i.e. the input must be a square matrix)
\n
\
ensures
\n
\
- Finds and returns the solution to the following optimization problem:
\n
\
\n
\
Maximize: f(A) == assignment_cost(cost, A)
\n
\
Subject to the following constraints:
\n
\
- The elements of A are unique. That is, there aren't any
\n
\
elements of A which are equal.
\n
\
- len(A) == cost.nr()
\n
\
\n
\
- Note that this function converts the input cost matrix into a 64bit fixed
\n
\
point representation. Therefore, you should make sure that the values in
\n
\
your cost matrix can be accurately represented by 64bit fixed point values.
\n
\
If this is not the case then the solution my become inaccurate due to
\n
\
rounding error. In general, this function will work properly when the ratio
\n
\
of the largest to the smallest value in cost is no more than about 1e16. "
);
def
(
"assignment_cost"
,
_assignment_cost
,
(
arg
(
"cost"
),
arg
(
"assignment"
)),
"requires
\n
\
- cost.nr() == cost.nc()
\n
\
(i.e. the input must be a square matrix)
\n
\
- for all valid i:
\n
\
- 0 <= assignment[i] < cost.nr()
\n
\
ensures
\n
\
- Interprets cost as a cost assignment matrix. That is, cost[i][j]
\n
\
represents the cost of assigning i to j.
\n
\
- Interprets assignment as a particular set of assignments. That is,
\n
\
i is assigned to assignment[i].
\n
\
- returns the cost of the given assignment. That is, returns
\n
\
a number which is:
\n
\
sum over i: cost[i][assignment[i]] "
);
def
(
"make_sparse_vector"
,
_make_sparse_vector
,
def
(
"make_sparse_vector"
,
_make_sparse_vector
,
"This function modifies its argument so that it is a properly sorted sparse vector.
\n
\
"This function modifies its argument so that it is a properly sorted sparse vector.
\n
\
This means that the elements of the sparse vector will be ordered so that pairs
\n
\
This means that the elements of the sparse vector will be ordered so that pairs
\n
\
...
...
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