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
f00c7e0f
Commit
f00c7e0f
authored
Dec 07, 2014
by
Davis King
Browse files
Added max cost assignment example program
parent
ad19228c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
0 deletions
+48
-0
examples/CMakeLists.txt
examples/CMakeLists.txt
+1
-0
examples/max_cost_assignment_ex.cpp
examples/max_cost_assignment_ex.cpp
+47
-0
No files found.
examples/CMakeLists.txt
View file @
f00c7e0f
...
@@ -56,6 +56,7 @@ add_example(logger_ex)
...
@@ -56,6 +56,7 @@ add_example(logger_ex)
add_example
(
logger_ex_2
)
add_example
(
logger_ex_2
)
add_example
(
matrix_ex
)
add_example
(
matrix_ex
)
add_example
(
matrix_expressions_ex
)
add_example
(
matrix_expressions_ex
)
add_example
(
max_cost_assignment_ex
)
add_example
(
member_function_pointer_ex
)
add_example
(
member_function_pointer_ex
)
add_example
(
mlp_ex
)
add_example
(
mlp_ex
)
add_example
(
model_selection_ex
)
add_example
(
model_selection_ex
)
...
...
examples/max_cost_assignment_ex.cpp
0 → 100755
View file @
f00c7e0f
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This simple example shows how to call dlib's optimal linear assignment problem solver.
It is an implementation of the famous Hungarian algorithm and is quite fast, operating in
O(N^3) time.
*/
#include <dlib/optimization/max_cost_assignment.h>
#include <iostream>
using
namespace
std
;
using
namespace
dlib
;
int
main
()
{
// Let's imagine you need to assign N people to N jobs. Additionally, each person will make
// your company a certain amount of money at each job, but each person has different skills
// so they are better at some jobs and worse at others. You would like to find the best way
// to assign people to these jobs. In particular, you would like to maximize the amount of
// money the group makes as a whole. This is an example of an assignment problem and is
// what is solved by the max_cost_assignment() routine.
//
// So in this example, let's imagine we have 3 people and 3 jobs. We represent the amount of
// money each person will produce at each job with a cost matrix. Each row corresponds to a
// person and each column corresponds to a job. So for example, below we are saying that
// person 0 will make $1 at job 0, $2 at job 1, and $6 at job 2.
matrix
<
int
>
cost
(
3
,
3
);
cost
=
1
,
2
,
6
,
5
,
3
,
6
,
4
,
5
,
0
;
// To find out the best assignment of people to jobs we just need to call this function.
std
::
vector
<
long
>
assignment
=
max_cost_assignment
(
cost
);
// This prints optimal assignments: [2, 0, 1] which indicates that we should assign
// the person from the first row of the cost matrix to job 2, the middle row person to
// job 0, and the bottom row person to job 1.
for
(
unsigned
int
i
=
0
;
i
<
assignment
.
size
();
i
++
)
cout
<<
assignment
[
i
]
<<
std
::
endl
;
// This prints optimal cost: 16.0
// which is correct since our optimal assignment is 6+5+5.
cout
<<
"optimal cost: "
<<
assignment_cost
(
cost
,
assignment
)
<<
endl
;
}
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