├── .gitignore ├── .travis.yml ├── AUTHORS.txt ├── CMakeLists.txt ├── COPYING ├── README.md ├── benchmarks ├── CMakeLists.txt ├── tests │ ├── CMakeLists.txt │ ├── main_celero.cpp │ ├── main_google.cpp │ ├── main_gprof.cpp │ ├── main_hayai.cpp │ └── main_rdtsc.cpp └── tools │ └── generator │ ├── CMakeLists.txt │ ├── main.cpp │ └── matrixutils.h ├── examples └── main.cpp ├── src ├── adapters │ ├── CMakeLists.txt │ ├── adapter.cpp │ ├── adapter.h │ ├── boostmatrixadapter.cpp │ ├── boostmatrixadapter.h │ ├── std2darrayadapter.cpp │ ├── std2darrayadapter.h │ ├── std2dvectordapter.cpp │ └── std2dvectordapter.h ├── matrix.cpp ├── matrix.h ├── munkres.cpp └── munkres.h └── tests ├── CMakeLists.txt ├── adapters ├── boost_matrixtest.cpp ├── std_2d_arraytest.cpp └── std_2d_vectortest.cpp ├── matrixtest.cpp ├── matrixtest.h └── munkrestest.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | src/munkrestest 2 | *.so 3 | *.o 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | sudo: false 3 | compiler: gcc 4 | install: 5 | - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8"; fi 6 | addons: 7 | apt: 8 | sources: 9 | - ubuntu-toolchain-r-test 10 | - boost-latest 11 | - kalakris-cmake 12 | packages: 13 | - g++-4.8 14 | - gcov-4.8 15 | - cmake 16 | - libboost1.55-all-dev 17 | before_script: 18 | - BUILD_DIR=`pwd`/build 19 | - mkdir -p ${BUILD_DIR} 20 | - cd ${BUILD_DIR} 21 | - cmake -DCMAKE_BUILD_TYPE=Debug -DMUNKRESCPP_DEVEL_MODE=ON ${BUILD_DIR}/.. 22 | script: 23 | - cd ${BUILD_DIR} 24 | - make && make tests 25 | - cd ${BUILD_DIR}/tests && ./munkrestest > /dev/null 26 | after_success: 27 | - cd ${BUILD_DIR} 28 | - mkdir coverage 29 | - cp -v ../src/*.cpp coverage 30 | - mv tests/CMakeFiles/munkrestest.dir/__/src/*.gcda coverage 31 | - mv tests/CMakeFiles/munkrestest.dir/__/src/*.gcno coverage 32 | - cd coverage 33 | - find . -type f -name '*.gcda' -exec gcov-4.8 {} + 34 | - bash <(curl -s https://codecov.io/bash) 35 | -------------------------------------------------------------------------------- /AUTHORS.txt: -------------------------------------------------------------------------------- 1 | 2 | The following is a list of much appreciated contributors: 3 | 4 | 5 | John Weaver 6 | Gluttton 7 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Common variables. 2 | cmake_minimum_required (VERSION 2.8) 3 | project (munkres-cpp) 4 | set (munkres-cpp_VERSION_MAJOR 2) 5 | set (munkres-cpp_VERSION_MINOR 0) 6 | 7 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 8 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") 9 | set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -ggdb3 -DDEBUG") 10 | set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 11 | 12 | include_directories (${PROJECT_SOURCE_DIR}/src) 13 | include_directories (${PROJECT_SOURCE_DIR}/src/adapters) 14 | 15 | # Sources. 16 | set ( 17 | MunkresCppLib_SOURCES 18 | ${PROJECT_SOURCE_DIR}/src/munkres.cpp 19 | ) 20 | 21 | # Headers. 22 | set ( 23 | MunkresCppLib_HEADERS 24 | ${PROJECT_SOURCE_DIR}/src/matrix.h 25 | ${PROJECT_SOURCE_DIR}/src/matrix.cpp 26 | ${PROJECT_SOURCE_DIR}/src/munkres.h 27 | 28 | ) 29 | 30 | add_subdirectory(${PROJECT_SOURCE_DIR}/src/adapters) 31 | 32 | # Library. 33 | add_library ( 34 | munkres STATIC 35 | ${MunkresCppLib_SOURCES} 36 | ) 37 | 38 | install (TARGETS munkres DESTINATION lib PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) 39 | install (FILES ${MunkresCppLib_HEADERS} DESTINATION include/munkres PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) 40 | 41 | 42 | # Binary example 43 | set (MunkresCppBin_SOURCES ${PROJECT_SOURCE_DIR}/examples/main.cpp) 44 | add_executable (munkres.bin EXCLUDE_FROM_ALL ${MunkresCppBin_SOURCES}) 45 | target_link_libraries (munkres.bin munkres) 46 | add_custom_target (example) 47 | add_dependencies (example munkres.bin) 48 | 49 | option(MUNKRESCPP_DEVEL_MODE "Configure project in development mode." OFF) 50 | if (MUNKRESCPP_DEVEL_MODE) 51 | # Enable the ExternalProject_Add directive. 52 | # Which used for getting external tools for the Project. 53 | include (ExternalProject) 54 | 55 | 56 | # Testing 57 | add_subdirectory (tests) 58 | 59 | 60 | # Benchmarking 61 | add_subdirectory (benchmarks) 62 | 63 | 64 | # Custom target to build everything. 65 | add_custom_target (full) 66 | add_dependencies ( 67 | full 68 | munkres 69 | example 70 | tests 71 | benchmarks 72 | ) 73 | 74 | # celero needs curses library 75 | #find_package(Curses) 76 | #include_directories(${CURSES_INCLUDE_DIR}) 77 | 78 | # Static code analyse. 79 | set (CppCheck_REPORT ${PROJECT_BINARY_DIR}/cppcheck.report) 80 | add_custom_command ( 81 | OUTPUT ${CppCheck_REPORT} 82 | COMMAND cppcheck ${MunkresCppLib_SOURCES} ${MunkresCppBin_SOURCES} -I${PROJECT_SOURCE_DIR}/src -I${PROJECT_SOURCE_DIR} --enable=all --force --inconclusive > cppcheck.report 2>&1 83 | ) 84 | add_custom_target (cppcheck DEPENDS ${CppCheck_REPORT}) 85 | set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CppCheck_REPORT}) 86 | endif (MUNKRESCPP_DEVEL_MODE) 87 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | munkres-cpp 2 | =========== 3 | 4 | [![build](https://travis-ci.org/saebyn/munkres-cpp.svg?branch=master)](https://travis-ci.org/saebyn/munkres-cpp) 5 | [![codecov.io](http://codecov.io/github/saebyn/munkres-cpp/coverage.svg?branch=master)](http://codecov.io/github/saebyn/munkres-cpp?branch=master) 6 | 7 | 8 | An implementation of the Kuhn–Munkres algorithm. 9 | 10 | 11 | 12 | License 13 | ------- 14 | 15 | Copyright (c) 2007-2013 John Weaver and contributors. 16 | Licensed under the GPLv2. See the file COPYING for details. 17 | 18 | 19 | 20 | Requirements 21 | ------------ 22 | 23 | For use: 24 | - C++ compiler with C++11 support. 25 | 26 | 27 | For development: 28 | - [GCC](https://gcc.gnu.org/) (tested on 4.6.3, 4.8); 29 | - [GNU Make](https://www.gnu.org/software/make/); 30 | - [CMake](http://www.cmake.org/) (2.8.12); 31 | - the test suite requires the [Google C++ Test Framework](http://code.google.com/p/googletest/); 32 | - microbenchmarking requires [Benchmark](https://github.com/google/benchmark), [Celero](https://github.com/DigitalInBlue/Celero), [Hayai](https://github.com/nickbruun/hayai) and [gprof](http://www.gnu.org/software/binutils/); 33 | - code coverage requires [gcov](https://gcc.gnu.org/onlinedocs/gcc/Gcov.html) and lcov; 34 | - static code analyzer requires [cppcheck](https://github.com/danmar/cppcheck). 35 | 36 | 37 | 38 | Portability 39 | ----------- 40 | 41 | The project is developed under the GNU/Linux OS with the gcc compiler and usually not tested under other OSs and compilers. 42 | But the project does not use OS or compiler specific features (types, attributes, etc) so it's expected that the project will be normally work under other platforms. 43 | 44 | 45 | 46 | Usage 47 | ----- 48 | 49 | These steps are the easiest way to get started: 50 | - download: ```$ git clone https://github.com/saebyn/munkres-cpp.git && cd munkres-cpp``` 51 | - build: ```$ mkdir build && cd build && cmake .. && make``` 52 | - install: ``` $ make install``` 53 | 54 | 55 | 56 | Example 57 | ------- 58 | 59 | TBD 60 | 61 | 62 | 63 | Development 64 | ----------- 65 | 66 | For development purpose, the project implements a variety of build targets. 67 | All of them help to continuously check correctness of algorithm implementation, performance, memory management, etc. 68 | Pass the```-DMUNKRESCPP_DEVEL_MODE=ON``` option to CMake to enable development mode. 69 | 70 | # Running unit tests: 71 | 72 | Build and execute the test suite with these commands: 73 | ``` 74 | $ git clone https://github.com/saebyn/munkres-cpp.git 75 | $ cd munkres-cpp 76 | $ mkdir build && cd build 77 | $ cmake -DCMAKE_BUILD_TYPE=Debug -DMUNKRESCPP_DEVEL_MODE=ON .. 78 | $ make tests 79 | $ tests/munkrestest 80 | ``` 81 | 82 | 83 | # Running code coverage analyzer: 84 | 85 | You must compile unit tests in debug mode to get a correct code coverage report. 86 | ``` 87 | $ 88 | $ make coverage 89 | $ firefox coverage/index.html & 90 | ``` 91 | 92 | 93 | # Running the memory profiler: 94 | 95 | Since the unit tests call all functions which implement the algorithm, using valgrind on the unit test runner is an appropriate way to check memory management. 96 | ``` 97 | $ 98 | $ valgrind tests/munkrestest 99 | ``` 100 | 101 | 102 | # Running the microbenchmarks: 103 | 104 | First, build them: 105 | ``` 106 | $ git clone https://github.com/saebyn/munkres-cpp.git 107 | $ cd munkres-cpp 108 | $ mkdir build && cd build 109 | $ cmake -DCMAKE_BUILD_TYPE=Release -DMUNKRESCPP_DEVEL_MODE=ON .. 110 | $ make benchmarks 111 | ``` 112 | 113 | To get comparable results it's required to generate the data set wich will be used for all benchmarks: 114 | ``` 115 | $ benchmarks/tools/generator/matrixgenerator.bin {dim_1 dim_2 ... dim_n} 116 | ``` 117 | Where every ```dim_x``` parameter generates a square matrix with ```dim_x``` dimension. 118 | To launch microbenchmarks, perform the following commands: 119 | ``` 120 | $ benchmarks/tests/munkresbenchmark_celero.bin 121 | $ benchmarks/tests/munkresbenchmark_google.bin 122 | $ benchmarks/tests/munkresbenchmark_hayai.bin 123 | $ benchmarks/tests/munkresbenchmark_rdtsc.bin 124 | ``` 125 | 126 | 127 | # Getting performance profiling: 128 | 129 | ``` 130 | $ 131 | $ benchmarks/tests/munkresbenchmark_gprof.bin 132 | $ gprof benchmarks/tests/munkresbenchmark_gprof.bin gmon.out -p -b 133 | ``` 134 | 135 | 136 | # Running the static code analyzer: 137 | 138 | ``` 139 | $ make cppcheck 140 | ``` 141 | 142 | 143 | # Running the code formatter: 144 | 145 | TBD 146 | 147 | 148 | 149 | Bug reporting and work to be done 150 | --------------------------------- 151 | 152 | Check the [issues list at GitHub](https://github.com/saebyn/munkres-cpp/issues?state=open). 153 | -------------------------------------------------------------------------------- /benchmarks/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Common variables. 2 | add_subdirectory (tools/generator) 3 | add_subdirectory (tests) 4 | -------------------------------------------------------------------------------- /benchmarks/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Common variable. 2 | include_directories (${PROJECT_SOURCE_DIR}/benchmarks/tools/generator) 3 | 4 | 5 | # Benchmark based on the Google benchmark Framework. 6 | ExternalProject_Add ( 7 | benchmark 8 | GIT_REPOSITORY "https://github.com/google/benchmark.git" 9 | CMAKE_ARGS "-DBENCHMARK_ENABLE_TESTING=OFF;-DCMAKE_BUILD_TYPE=Release" 10 | SOURCE_DIR "${PROJECT_BINARY_DIR}/benchmark" 11 | BINARY_DIR "${PROJECT_BINARY_DIR}/benchmark" 12 | INSTALL_COMMAND "" 13 | TEST_COMMAND "" 14 | ) 15 | set_target_properties (benchmark PROPERTIES EXCLUDE_FROM_ALL TRUE) 16 | set (GBENCHMARK_FOUND true) 17 | set (GBENCHMARK_INCLUDE_DIR "${PROJECT_BINARY_DIR}/benchmark/include") 18 | set (GBENCHMARK_LIBRARY "${PROJECT_BINARY_DIR}/benchmark/src/libbenchmark.a") 19 | 20 | set (MunkresCppBenchmarkGoogle_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main_google.cpp) 21 | add_executable (munkresbenchmark_google.bin EXCLUDE_FROM_ALL ${MunkresCppBenchmarkGoogle_SOURCES}) 22 | include_directories (${GBENCHMARK_INCLUDE_DIR}) 23 | target_link_libraries (munkresbenchmark_google.bin munkres ${GBENCHMARK_LIBRARY} pthread) 24 | add_dependencies (munkresbenchmark_google.bin benchmark) 25 | 26 | 27 | # Benchmark based on the hayai Framework. 28 | ExternalProject_Add ( 29 | hayai 30 | GIT_REPOSITORY "https://github.com/nickbruun/hayai.git" 31 | SOURCE_DIR "${PROJECT_BINARY_DIR}/hayai" 32 | BUILD_COMMAND "" 33 | INSTALL_COMMAND "" 34 | TEST_COMMAND "" 35 | ) 36 | set_target_properties (hayai PROPERTIES EXCLUDE_FROM_ALL TRUE) 37 | set (HAYAI_FOUND true) 38 | set (HAYAI_INCLUDE_DIR "${PROJECT_BINARY_DIR}/hayai/src") 39 | 40 | set (MunkresCppBenchmarkHayai_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main_hayai.cpp) 41 | add_executable (munkresbenchmark_hayai.bin EXCLUDE_FROM_ALL ${MunkresCppBenchmarkHayai_SOURCES}) 42 | include_directories (${HAYAI_INCLUDE_DIR}) 43 | target_link_libraries (munkresbenchmark_hayai.bin munkres) 44 | add_dependencies (munkresbenchmark_hayai.bin hayai) 45 | 46 | 47 | # Benchmark based on the Celero Framework. 48 | ExternalProject_Add ( 49 | Celero 50 | GIT_REPOSITORY "https://github.com/DigitalInBlue/Celero.git" 51 | CMAKE_ARGS "-DCELERO_ENABLE_EXPERIMENTS=OFF;-DCMAKE_BUILD_TYPE=Release" 52 | SOURCE_DIR "${PROJECT_BINARY_DIR}/Celero" 53 | BINARY_DIR "${PROJECT_BINARY_DIR}/CeleroBuild" 54 | INSTALL_COMMAND "" 55 | TEST_COMMAND "" 56 | ) 57 | set_target_properties (Celero PROPERTIES EXCLUDE_FROM_ALL TRUE) 58 | set (CELERO_FOUND true) 59 | set (CELERO_INCLUDE_DIR "${PROJECT_BINARY_DIR}/Celero/include") 60 | set (CELERO_LIBRARY "${PROJECT_BINARY_DIR}/CeleroBuild/libcelero.so") 61 | 62 | set (MunkresCppBenchmarkCelero_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main_celero.cpp) 63 | add_executable (munkresbenchmark_celero.bin EXCLUDE_FROM_ALL ${MunkresCppBenchmarkCelero_SOURCES}) 64 | include_directories (${CELERO_INCLUDE_DIR}) 65 | target_link_libraries (munkresbenchmark_celero.bin munkres ${CELERO_LIBRARY}) 66 | add_dependencies (munkresbenchmark_celero.bin Celero) 67 | 68 | 69 | # Benchmark based on the RDTSC. 70 | set (MunkresCppBenchmarkRdtsc_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main_rdtsc.cpp) 71 | add_executable (munkresbenchmark_rdtsc.bin EXCLUDE_FROM_ALL ${MunkresCppBenchmarkRdtsc_SOURCES}) 72 | target_link_libraries (munkresbenchmark_rdtsc.bin munkres) 73 | 74 | 75 | # Test for generate profiler (gprof) data. 76 | set (MunkresCppBenchmarkGprof_SOURCES 77 | ${PROJECT_SOURCE_DIR}/src/munkres.cpp 78 | ${CMAKE_CURRENT_SOURCE_DIR}/main_gprof.cpp 79 | ) 80 | add_executable (munkresbenchmark_gprof.bin EXCLUDE_FROM_ALL ${MunkresCppBenchmarkGprof_SOURCES}) 81 | set_target_properties (munkresbenchmark_gprof.bin PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -pg" LINK_FLAGS "-pg") 82 | 83 | 84 | # Prevent launch tests before will be generated test data set. 85 | set (BenchmarkDataSet ${PROJECT_BINARY_DIR}/matrices.txt) 86 | add_custom_command ( 87 | OUTPUT ${BenchmarkDataSet} 88 | COMMAND cd ${PROJECT_BINARY_DIR} && ./benchmarks/tools/generator/matrixgenerator.bin 20 89 | ) 90 | add_custom_target (generate_benchmark_dataset DEPENDS ${BenchmarkDataSet}) 91 | add_dependencies (generate_benchmark_dataset matrixgenerator.bin) 92 | 93 | add_dependencies (munkresbenchmark_google.bin generate_benchmark_dataset) 94 | add_dependencies (munkresbenchmark_hayai.bin generate_benchmark_dataset) 95 | add_dependencies (munkresbenchmark_celero.bin generate_benchmark_dataset) 96 | add_dependencies (munkresbenchmark_rdtsc.bin generate_benchmark_dataset) 97 | add_dependencies (munkresbenchmark_gprof.bin generate_benchmark_dataset) 98 | 99 | 100 | add_custom_target (benchmarks) 101 | add_dependencies ( 102 | benchmarks 103 | munkresbenchmark_google.bin 104 | munkresbenchmark_celero.bin 105 | munkresbenchmark_hayai.bin 106 | munkresbenchmark_rdtsc.bin 107 | munkresbenchmark_gprof.bin 108 | matrixgenerator.bin 109 | ) 110 | -------------------------------------------------------------------------------- /benchmarks/tests/main_celero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "matrixutils.h" 5 | #include "munkres.h" 6 | 7 | 8 | 9 | std::vector *> matrices; 10 | 11 | size_t i {0}; 12 | 13 | 14 | 15 | class MunkresFixture : public celero::TestFixture 16 | { 17 | public: 18 | void setUp (int64_t) override 19 | { 20 | matrix = * matrices [i]; 21 | } 22 | 23 | Munkres munkres; 24 | Matrix matrix; 25 | }; 26 | 27 | 28 | 29 | BASELINE_F (Munkres, Solve, MunkresFixture, 5000, 1) 30 | { 31 | munkres.solve (matrix); 32 | } 33 | 34 | 35 | 36 | BENCHMARK_F (Munkres, Solve, MunkresFixture, 5000, 1) 37 | { 38 | munkres.solve (matrix); 39 | } 40 | 41 | 42 | 43 | // Main function. 44 | int main (int argc, char * argv []) 45 | { 46 | read (matrices); 47 | 48 | for (const auto x : matrices) { 49 | celero::Run (argc, argv); 50 | ++i; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /benchmarks/tests/main_google.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | 5 | #include "matrixutils.h" 6 | #include "munkres.h" 7 | 8 | 9 | 10 | std::vector *> matrices; 11 | 12 | constexpr int maxReasonableDataSetCount {100}; 13 | 14 | 15 | 16 | static void BM_solve (benchmark::State & state) 17 | { 18 | state.PauseTiming (); 19 | if (state.range_x () < matrices.size () ) { 20 | Munkres munkres; 21 | while (state.KeepRunning () ) { 22 | auto matrix = * matrices [state.range_x ()]; 23 | state.ResumeTiming (); 24 | munkres.solve (matrix); 25 | state.PauseTiming (); 26 | } 27 | } 28 | } 29 | BENCHMARK (BM_solve)->DenseRange (0, maxReasonableDataSetCount); 30 | 31 | 32 | 33 | // Main function. 34 | int main (int argc, char * argv []) 35 | { 36 | read (matrices); 37 | 38 | benchmark::Initialize (& argc, const_cast (argv) ); 39 | benchmark::RunSpecifiedBenchmarks (); 40 | } 41 | -------------------------------------------------------------------------------- /benchmarks/tests/main_gprof.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "matrixutils.h" 4 | #include "munkres.h" 5 | 6 | 7 | 8 | // Main function. 9 | int main (int argc, char * argv []) 10 | { 11 | std::vector *> matrices; 12 | read (matrices); 13 | 14 | 15 | for (size_t i = 0; i < matrices.size (); ++i) { 16 | std::cout << "Test case " << i + 1 << " from " << matrices.size () << std::endl; 17 | Munkres munkres; 18 | auto matrix = * matrices [i]; 19 | munkres.solve (matrix); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /benchmarks/tests/main_hayai.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "matrixutils.h" 5 | #include "munkres.h" 6 | 7 | 8 | 9 | std::vector *> matrices; 10 | 11 | size_t i {0}; 12 | 13 | 14 | 15 | class MunkresFixture : public ::hayai::Fixture 16 | { 17 | public: 18 | void SetUp () override 19 | { 20 | matrix = * matrices [i]; 21 | } 22 | 23 | Munkres munkres; 24 | Matrix matrix; 25 | }; 26 | 27 | 28 | 29 | BENCHMARK_F (MunkresFixture, Solve, 5000, 1) 30 | { 31 | munkres.solve (matrix); 32 | } 33 | 34 | 35 | 36 | // Main function. 37 | int main (int argc, char * argv []) 38 | { 39 | read (matrices); 40 | 41 | hayai::ConsoleOutputter consoleOutputter; 42 | hayai::Benchmarker::AddOutputter (consoleOutputter); 43 | for (const auto x : matrices) { 44 | hayai::Benchmarker::RunAllTests (); 45 | ++i; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /benchmarks/tests/main_rdtsc.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "matrixutils.h" 7 | #include "munkres.h" 8 | 9 | 10 | 11 | // Main function. 12 | int main (int argc, char * argv []) 13 | { 14 | std::vector *> matrices; 15 | read (matrices); 16 | 17 | 18 | size_t iterations = 1000; 19 | size_t runs = 10; 20 | if (3 == argc) { 21 | runs = std::stoi (argv [1]); 22 | iterations = std::stoi (argv [2]); 23 | } 24 | std::cout << "Prepare to launch " << runs << " runs with " << iterations << " iterations each." << std::endl; 25 | 26 | for (size_t i = 0; i < matrices.size (); ++i) { 27 | std::cout << "Test case " << i + 1 << " from " << matrices.size () << std::endl; 28 | uint64_t rdtscMin = std::numeric_limits ::max (); 29 | for (size_t j = 0; j < runs; ++j) { 30 | uint64_t rdtscMinRun = std::numeric_limits ::max (); 31 | for (size_t k = 0; k < iterations; ++k) { 32 | Munkres munkres; 33 | auto matrix = * matrices [i]; 34 | uint64_t rdtsc = __rdtsc (); 35 | munkres.solve (matrix); 36 | rdtsc = __rdtsc () - rdtsc; 37 | rdtscMinRun = std::min (rdtscMinRun, rdtsc); 38 | } 39 | std::cout << "Run " << std::setw (4) << j << ": " << rdtscMinRun << std::endl; 40 | rdtscMin = std::min (rdtscMin, rdtscMinRun); 41 | } 42 | std::cout << "The best: " << rdtscMin << std::endl; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /benchmarks/tools/generator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Data generator. 2 | set (MunkresCppMatrixGenerator_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) 3 | include_directories (${PROJECT_SOURCE_DIR}/benchmarks/tools/generator) 4 | add_executable (matrixgenerator.bin EXCLUDE_FROM_ALL ${MunkresCppMatrixGenerator_SOURCES}) 5 | -------------------------------------------------------------------------------- /benchmarks/tools/generator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "matrixutils.h" 3 | #include 4 | 5 | 6 | 7 | int main (int argc, char * argv []) 8 | { 9 | std::vector *> matrices; 10 | 11 | for (int i = 1; i < argc; ++i) { 12 | const size_t size = std::stoi (argv [i]); 13 | Matrix * matrix = new Matrix ; 14 | * matrix = generate_random_matrix (size, size); 15 | matrices.push_back (matrix); 16 | } 17 | write (matrices); 18 | 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /benchmarks/tools/generator/matrixutils.h: -------------------------------------------------------------------------------- 1 | #if !defined(_MATRIX_UTILS_H_) 2 | #define _MATRIX_UTILS_H_ 3 | 4 | #include "matrix.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | 13 | const std::string fileName = "matrices.txt"; 14 | 15 | 16 | 17 | template 18 | std::ostream & operator << (std::ostream & os, const Matrix & m) 19 | { 20 | const std::string indent (" "); 21 | os << "Matrix (" << & m << ") of " << m.rows () << "x" << m.columns () << std::endl; 22 | for (unsigned int row = 0; row < m.rows (); ++row) { 23 | os << indent; 24 | for (unsigned int col = 0; col < m.columns (); ++col) { 25 | os << std::setw (4) << std::setfill (' ') << m (row, col) << " "; 26 | } 27 | os << std::endl; 28 | } 29 | 30 | return os; 31 | } 32 | 33 | 34 | 35 | template 36 | std::istream & operator >> (std::istream & is, Matrix & m) 37 | { 38 | std::string marker; 39 | is >> marker; 40 | if ("Matrix" == marker) { 41 | std::string unused; 42 | std::string size; 43 | is >> unused; 44 | is >> unused; 45 | is >> size; 46 | const size_t delimiter = size.find ("x"); 47 | if (std::string::npos != delimiter) { 48 | const size_t rows = stoi (size.substr (0, delimiter) ); 49 | const size_t columns = stoi (size.substr (delimiter + 1) ); 50 | if (rows && columns) { 51 | m.resize (rows, columns); 52 | for (size_t row = 0; row < rows; ++row) { 53 | for (size_t col = 0; col < columns; ++col) { 54 | is >> m (row, col); 55 | } 56 | } 57 | } 58 | } 59 | } 60 | 61 | return is; 62 | } 63 | 64 | 65 | 66 | template 67 | bool write (const std::vector *> & matrices) 68 | { 69 | std::fstream os; 70 | os.open (fileName, std::fstream::out/* | std::fstream::binary*/); 71 | 72 | for (size_t i = 0; i < matrices.size (); ++i) { 73 | os << * matrices [i]; 74 | } 75 | os.close (); 76 | 77 | return true; 78 | } 79 | 80 | 81 | 82 | template 83 | bool read (std::vector *> & matrices) 84 | { 85 | std::fstream is; 86 | is.open (fileName, std::fstream::in/* | std::fstream::binary*/); 87 | 88 | Matrix * matrix = new Matrix ; 89 | while (is >> * matrix) { 90 | matrices.push_back (matrix); 91 | matrix = new Matrix ; 92 | } 93 | delete matrix; 94 | is.close (); 95 | 96 | return true; 97 | } 98 | 99 | 100 | 101 | template 102 | Matrix generate_random_matrix (const size_t nrows, const size_t ncols) 103 | { 104 | Matrix matrix(nrows, ncols); 105 | 106 | // Prepare random generator. 107 | std::default_random_engine generator; 108 | std::uniform_real_distribution distribution (0.0, std::numeric_limits::max () ); 109 | 110 | // Initialize matrix with random values. 111 | for ( size_t row = 0 ; row < matrix.rows() ; row++ ) { 112 | for ( size_t col = 0 ; col < matrix.columns() ; col++ ) { 113 | matrix(row,col) = distribution (generator); 114 | } 115 | } 116 | 117 | return matrix; 118 | } 119 | 120 | #endif /* !defined(_MATRIX_UTILS_H_) */ 121 | -------------------------------------------------------------------------------- /examples/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* 20 | * Some example code. 21 | * 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "munkres.h" 29 | #include "adapters/boostmatrixadapter.h" 30 | 31 | int 32 | main(int argc, char *argv[]) { 33 | int nrows = 101; 34 | int ncols = 101; 35 | 36 | if ( argc == 3 ) { 37 | nrows = atoi(argv[1]); 38 | ncols = atoi(argv[2]); 39 | } 40 | 41 | Matrix matrix(nrows, ncols); 42 | 43 | srandom(time(nullptr)); // Seed random number generator. 44 | 45 | // Initialize matrix with random values. 46 | for ( int row = 0 ; row < nrows ; row++ ) { 47 | for ( int col = 0 ; col < ncols ; col++ ) { 48 | matrix(row,col) = (double)random(); 49 | } 50 | } 51 | 52 | // Display begin matrix state. 53 | for ( int row = 0 ; row < nrows ; row++ ) { 54 | for ( int col = 0 ; col < ncols ; col++ ) { 55 | std::cout.width(2); 56 | std::cout << matrix(row,col) << ","; 57 | } 58 | std::cout << std::endl; 59 | } 60 | std::cout << std::endl; 61 | 62 | // Apply Munkres algorithm to matrix. 63 | Munkres m; 64 | m.solve(matrix); 65 | 66 | // Display solved matrix. 67 | for ( int row = 0 ; row < nrows ; row++ ) { 68 | for ( int col = 0 ; col < ncols ; col++ ) { 69 | std::cout.width(2); 70 | std::cout << matrix(row,col) << ","; 71 | } 72 | std::cout << std::endl; 73 | } 74 | 75 | std::cout << std::endl; 76 | 77 | 78 | for ( int row = 0 ; row < nrows ; row++ ) { 79 | int rowcount = 0; 80 | for ( int col = 0 ; col < ncols ; col++ ) { 81 | if ( matrix(row,col) == 0 ) 82 | rowcount++; 83 | } 84 | if ( rowcount != 1 ) 85 | std::cerr << "Row " << row << " has " << rowcount << " columns that have been matched." << std::endl; 86 | } 87 | 88 | for ( int col = 0 ; col < ncols ; col++ ) { 89 | int colcount = 0; 90 | for ( int row = 0 ; row < nrows ; row++ ) { 91 | if ( matrix(row,col) == 0 ) 92 | colcount++; 93 | } 94 | if ( colcount != 1 ) 95 | std::cerr << "Column " << col << " has " << colcount << " rows that have been matched." << std::endl; 96 | } 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /src/adapters/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set ( 2 | MunkresAdapters_HEADERS 3 | ${PROJECT_SOURCE_DIR}/src/adapters/adapter.h 4 | ) 5 | 6 | set ( 7 | MunkresCppLib_SOURCES 8 | ${MunkresCppLib_SOURCES} 9 | ${PROJECT_SOURCE_DIR}/src/adapters/adapter.cpp 10 | ) 11 | 12 | option(STD_ADAPTERS "Build 2D array, std::array and std::vector adapters" OFF) 13 | if(STD_ADAPTERS) 14 | set (MunkresCppLib_HEADERS 15 | ${MunkresCppLib_HEADERS} 16 | ${PROJECT_SOURCE_DIR}/src/adapters/std2dvectoradapter.h 17 | ${PROJECT_SOURCE_DIR}/src/adapters/std2darrayadapter.h 18 | ) 19 | 20 | set ( 21 | MunkresCppLib_SOURCES 22 | ${MunkresCppLib_SOURCES} 23 | ${PROJECT_SOURCE_DIR}/src/adapters/std2dvectordapter.cpp 24 | ${PROJECT_SOURCE_DIR}/src/adapters/std2darrayadapter.cpp 25 | ) 26 | endif(STD_ADAPTERS) 27 | 28 | option(BOOST_MATRIX_ADAPTER "Build boost::numeric::ublas::matrix adapter" OFF) 29 | if(BOOST_MATRIX_ADAPTER) 30 | find_package (Boost REQUIRED) 31 | set (MunkresCppLib_HEADERS ${MunkresCppLib_HEADERS} 32 | ${PROJECT_SOURCE_DIR}/src/adapters/boostmatrixadapter.h 33 | ) 34 | 35 | set ( 36 | MunkresCppLib_SOURCES 37 | ${MunkresCppLib_SOURCES} 38 | ${PROJECT_SOURCE_DIR}/src/adapters/boostmatrixadapter.cpp 39 | ) 40 | endif(BOOST_MATRIX_ADAPTER) 41 | 42 | #propagate upward edited sources 43 | set(MunkresCppLib_SOURCES ${MunkresCppLib_SOURCES} PARENT_SCOPE) 44 | 45 | #install all selected adapters 46 | install (FILES ${MunkresAdapters_HEADERS} DESTINATION include/munkres/adapters PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) 47 | 48 | -------------------------------------------------------------------------------- /src/adapters/adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include "adapter.h" 20 | -------------------------------------------------------------------------------- /src/adapters/adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef _ADAPTER_H_ 20 | #define _ADAPTER_H_ 21 | 22 | #include "matrix.h" 23 | #include "munkres.h" 24 | 25 | template class Adapter 26 | { 27 | public: 28 | virtual Matrix convertToMatrix(const Container &con) const = 0; 29 | virtual void convertFromMatrix(Container &con, const Matrix &matrix) const = 0; 30 | virtual void solve(Container &con) 31 | { 32 | auto matrix = convertToMatrix(con); 33 | m_munkres.solve(matrix); 34 | convertFromMatrix(con, matrix); 35 | } 36 | protected: 37 | Munkres m_munkres; 38 | }; 39 | 40 | #endif /* _ADAPTER_H_ */ 41 | -------------------------------------------------------------------------------- /src/adapters/boostmatrixadapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include "boostmatrixadapter.h" 20 | 21 | template class BoostMatrixAdapter; 22 | template class BoostMatrixAdapter; 23 | template class BoostMatrixAdapter; 24 | -------------------------------------------------------------------------------- /src/adapters/boostmatrixadapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef _BOOSTMATRIXADAPTER_H_ 20 | #define _BOOSTMATRIXADAPTER_H_ 21 | 22 | #include "adapter.h" 23 | #include 24 | 25 | template class BoostMatrixAdapter : public Adapter > 26 | { 27 | public: 28 | virtual Matrix convertToMatrix(const boost::numeric::ublas::matrix &boost_matrix) const override 29 | { 30 | const auto rows = boost_matrix.size1 (); 31 | const auto columns = boost_matrix.size2 (); 32 | Matrix matrix (rows, columns); 33 | for (int i = 0; i < rows; ++i) { 34 | for (int j = 0; j < columns; ++j) { 35 | matrix (i, j) = boost_matrix (i, j); 36 | } 37 | } 38 | return matrix; 39 | } 40 | 41 | virtual void convertFromMatrix(boost::numeric::ublas::matrix &boost_matrix,const Matrix &matrix) const override 42 | { 43 | const auto rows = matrix.rows(); 44 | const auto columns = matrix.columns(); 45 | for (int i = 0; i < rows; ++i) { 46 | for (int j = 0; j < columns; ++j) { 47 | boost_matrix (i, j) = matrix (i, j); 48 | } 49 | } 50 | } 51 | }; 52 | 53 | #endif /* _BOOSTMATRIXADAPTER_H_ */ 54 | -------------------------------------------------------------------------------- /src/adapters/std2darrayadapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include "std2darrayadapter.h" 20 | -------------------------------------------------------------------------------- /src/adapters/std2darrayadapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef STD2DARRAYADAPTER_H 20 | #define STD2DARRAYADAPTER_H 21 | 22 | #include "adapter.h" 23 | 24 | template class Std2dArrayAdapter : public Adapter, rows>> 25 | { 26 | public: 27 | virtual Matrix convertToMatrix(const std::array , rows> &array) const override 28 | { 29 | Matrix matrix(rows, columns); 30 | for (int i = 0; i < rows; ++i) 31 | { 32 | for (int j = 0; j < columns; ++j) 33 | { 34 | matrix (i, j) = array [i][j]; 35 | } 36 | } 37 | return matrix; 38 | } 39 | 40 | virtual void convertFromMatrix(std::array , rows> &array,const Matrix &matrix) const override 41 | { 42 | for (int i = 0; i < rows; ++i) 43 | { 44 | for (int j = 0; j < columns; ++j) 45 | { 46 | array [i][j] = matrix (i, j); 47 | } 48 | } 49 | } 50 | }; 51 | 52 | #endif // STD2DARRAYADAPTER_H 53 | -------------------------------------------------------------------------------- /src/adapters/std2dvectordapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include "std2dvectordapter.h" 20 | 21 | template class Std2dVectorAdapter; 22 | template class Std2dVectorAdapter; 23 | template class Std2dVectorAdapter; 24 | -------------------------------------------------------------------------------- /src/adapters/std2dvectordapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Miroslav Krajicek 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef RAW2DARRAY_H 20 | #define RAW2DARRAY_H 21 | 22 | #include "adapter.h" 23 | 24 | template class Std2dVectorAdapter : public Adapter>> 25 | { 26 | public: 27 | virtual Matrix convertToMatrix(const std::vector> &vector) const override 28 | { 29 | const int rows = vector.size(); 30 | const int cols = vector[0].size(); 31 | Matrix matrix (rows, cols); 32 | for (int i = 0; i < rows; ++i) 33 | { 34 | for (int j = 0; j < cols; ++j) 35 | { 36 | matrix(i, j) = vector[i][j]; 37 | } 38 | } 39 | return matrix; 40 | } 41 | 42 | virtual void convertFromMatrix(std::vector> &vector,const Matrix &matrix) const override 43 | { 44 | vector.clear(); 45 | 46 | const int rows = matrix.rows(); 47 | const int cols = matrix.columns(); 48 | for (int i = 0; i < rows; ++i) 49 | { 50 | std::vector temp; 51 | for (int j = 0; j < cols; ++j) 52 | { 53 | temp.push_back(matrix(i,j)); 54 | } 55 | 56 | vector.push_back(temp); 57 | } 58 | } 59 | }; 60 | 61 | #endif // RAW2DARRAY_H 62 | -------------------------------------------------------------------------------- /src/matrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include "matrix.h" 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | /*export*/ template 26 | Matrix::Matrix() { 27 | m_rows = 0; 28 | m_columns = 0; 29 | m_matrix = nullptr; 30 | } 31 | 32 | 33 | /*export*/ template 34 | Matrix::Matrix(const std::initializer_list> init) { 35 | m_matrix = nullptr; 36 | m_rows = init.size(); 37 | if ( m_rows == 0 ) { 38 | m_columns = 0; 39 | } else { 40 | m_columns = init.begin()->size(); 41 | if ( m_columns > 0 ) { 42 | resize(m_rows, m_columns); 43 | } 44 | } 45 | 46 | size_t i = 0, j; 47 | for ( auto row = init.begin() ; row != init.end() ; ++row, ++i ) { 48 | assert ( row->size() == m_columns && "All rows must have the same number of columns." ); 49 | j = 0; 50 | for ( auto value = row->begin() ; value != row->end() ; ++value, ++j ) { 51 | m_matrix[i][j] = *value; 52 | } 53 | } 54 | } 55 | 56 | /*export*/ template 57 | Matrix::Matrix(const Matrix &other) { 58 | if ( other.m_matrix != nullptr ) { 59 | // copy arrays 60 | m_matrix = nullptr; 61 | resize(other.m_rows, other.m_columns); 62 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 63 | for ( size_t j = 0 ; j < m_columns ; j++ ) { 64 | m_matrix[i][j] = other.m_matrix[i][j]; 65 | } 66 | } 67 | } else { 68 | m_matrix = nullptr; 69 | m_rows = 0; 70 | m_columns = 0; 71 | } 72 | } 73 | 74 | /*export*/ template 75 | Matrix::Matrix(const size_t rows, const size_t columns) { 76 | m_matrix = nullptr; 77 | resize(rows, columns); 78 | } 79 | 80 | /*export*/ template 81 | Matrix & 82 | Matrix::operator= (const Matrix &other) { 83 | if ( other.m_matrix != nullptr ) { 84 | // copy arrays 85 | resize(other.m_rows, other.m_columns); 86 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 87 | for ( size_t j = 0 ; j < m_columns ; j++ ) { 88 | m_matrix[i][j] = other.m_matrix[i][j]; 89 | } 90 | } 91 | } else { 92 | // free arrays 93 | for ( size_t i = 0 ; i < m_columns ; i++ ) { 94 | delete [] m_matrix[i]; 95 | } 96 | 97 | delete [] m_matrix; 98 | 99 | m_matrix = nullptr; 100 | m_rows = 0; 101 | m_columns = 0; 102 | } 103 | 104 | return *this; 105 | } 106 | 107 | /*export*/ template 108 | Matrix::~Matrix() { 109 | if ( m_matrix != nullptr ) { 110 | // free arrays 111 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 112 | delete [] m_matrix[i]; 113 | } 114 | 115 | delete [] m_matrix; 116 | } 117 | m_matrix = nullptr; 118 | } 119 | 120 | /*export*/ template 121 | void 122 | Matrix::resize(const size_t rows, const size_t columns, const T default_value) { 123 | assert ( rows > 0 && columns > 0 && "Columns and rows must exist." ); 124 | 125 | if ( m_matrix == nullptr ) { 126 | // alloc arrays 127 | m_matrix = new T*[rows]; // rows 128 | for ( size_t i = 0 ; i < rows ; i++ ) { 129 | m_matrix[i] = new T[columns]; // columns 130 | } 131 | 132 | m_rows = rows; 133 | m_columns = columns; 134 | clear(); 135 | } else { 136 | // save array pointer 137 | T **new_matrix; 138 | // alloc new arrays 139 | new_matrix = new T*[rows]; // rows 140 | for ( size_t i = 0 ; i < rows ; i++ ) { 141 | new_matrix[i] = new T[columns]; // columns 142 | for ( size_t j = 0 ; j < columns ; j++ ) { 143 | new_matrix[i][j] = default_value; 144 | } 145 | } 146 | 147 | // copy data from saved pointer to new arrays 148 | size_t minrows = std::min(rows, m_rows); 149 | size_t mincols = std::min(columns, m_columns); 150 | for ( size_t x = 0 ; x < minrows ; x++ ) { 151 | for ( size_t y = 0 ; y < mincols ; y++ ) { 152 | new_matrix[x][y] = m_matrix[x][y]; 153 | } 154 | } 155 | 156 | // delete old arrays 157 | if ( m_matrix != nullptr ) { 158 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 159 | delete [] m_matrix[i]; 160 | } 161 | 162 | delete [] m_matrix; 163 | } 164 | 165 | m_matrix = new_matrix; 166 | } 167 | 168 | m_rows = rows; 169 | m_columns = columns; 170 | } 171 | 172 | /*export*/ template 173 | void 174 | Matrix::clear() { 175 | assert( m_matrix != nullptr ); 176 | 177 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 178 | for ( size_t j = 0 ; j < m_columns ; j++ ) { 179 | m_matrix[i][j] = 0; 180 | } 181 | } 182 | } 183 | 184 | /*export*/ template 185 | inline T& 186 | Matrix::operator ()(const size_t x, const size_t y) { 187 | assert ( x < m_rows ); 188 | assert ( y < m_columns ); 189 | assert ( m_matrix != nullptr ); 190 | return m_matrix[x][y]; 191 | } 192 | 193 | 194 | /*export*/ template 195 | inline const T& 196 | Matrix::operator ()(const size_t x, const size_t y) const { 197 | assert ( x < m_rows ); 198 | assert ( y < m_columns ); 199 | assert ( m_matrix != nullptr ); 200 | return m_matrix[x][y]; 201 | } 202 | 203 | 204 | /*export*/ template 205 | const T 206 | Matrix::min() const { 207 | assert( m_matrix != nullptr ); 208 | assert ( m_rows > 0 ); 209 | assert ( m_columns > 0 ); 210 | T min = m_matrix[0][0]; 211 | 212 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 213 | for ( size_t j = 0 ; j < m_columns ; j++ ) { 214 | min = std::min(min, m_matrix[i][j]); 215 | } 216 | } 217 | 218 | return min; 219 | } 220 | 221 | 222 | /*export*/ template 223 | const T 224 | Matrix::max() const { 225 | assert( m_matrix != nullptr ); 226 | assert ( m_rows > 0 ); 227 | assert ( m_columns > 0 ); 228 | T max = m_matrix[0][0]; 229 | 230 | for ( size_t i = 0 ; i < m_rows ; i++ ) { 231 | for ( size_t j = 0 ; j < m_columns ; j++ ) { 232 | max = std::max(max, m_matrix[i][j]); 233 | } 234 | } 235 | 236 | return max; 237 | } 238 | -------------------------------------------------------------------------------- /src/matrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef _MATRIX_H_ 20 | #define _MATRIX_H_ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | template 27 | class Matrix { 28 | public: 29 | Matrix(); 30 | Matrix(const size_t rows, const size_t columns); 31 | Matrix(const std::initializer_list> init); 32 | Matrix(const Matrix &other); 33 | Matrix & operator= (const Matrix &other); 34 | ~Matrix(); 35 | // all operations modify the matrix in-place. 36 | void resize(const size_t rows, const size_t columns, const T default_value = 0); 37 | void clear(); 38 | T& operator () (const size_t x, const size_t y); 39 | const T& operator () (const size_t x, const size_t y) const; 40 | const T min() const; 41 | const T max() const; 42 | inline size_t minsize() { return ((m_rows < m_columns) ? m_rows : m_columns); } 43 | inline size_t columns() const { return m_columns;} 44 | inline size_t rows() const { return m_rows;} 45 | 46 | friend std::ostream& operator<<(std::ostream& os, const Matrix &matrix) 47 | { 48 | os << "Matrix:" << std::endl; 49 | for (size_t row = 0 ; row < matrix.rows() ; row++ ) 50 | { 51 | for (size_t col = 0 ; col < matrix.columns() ; col++ ) 52 | { 53 | os.width(8); 54 | os << matrix(row, col) << ","; 55 | } 56 | os << std::endl; 57 | } 58 | return os; 59 | } 60 | 61 | private: 62 | T **m_matrix; 63 | size_t m_rows; 64 | size_t m_columns; 65 | }; 66 | 67 | #ifndef USE_EXPORT_KEYWORD 68 | #include "matrix.cpp" 69 | //#define export /*export*/ 70 | #endif 71 | 72 | #endif /* !defined(_MATRIX_H_) */ 73 | 74 | -------------------------------------------------------------------------------- /src/munkres.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * Copyright (c) 2015 Miroslav Krajicek 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #include "munkres.h" 21 | 22 | template class Munkres; 23 | template class Munkres; 24 | template class Munkres; 25 | 26 | -------------------------------------------------------------------------------- /src/munkres.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * Copyright (c) 2015 Miroslav Krajicek 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #if !defined(_MUNKRES_H_) 21 | #define _MUNKRES_H_ 22 | 23 | #include "matrix.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | template class Munkres 32 | { 33 | static constexpr int NORMAL = 0; 34 | static constexpr int STAR = 1; 35 | static constexpr int PRIME = 2; 36 | public: 37 | 38 | /* 39 | * 40 | * Linear assignment problem solution 41 | * [modifies matrix in-place.] 42 | * matrix(row,col): row major format assumed. 43 | * 44 | * Assignments are remaining 0 values 45 | * (extra 0 values are replaced with -1) 46 | * 47 | */ 48 | void solve(Matrix &m) { 49 | const size_t rows = m.rows(), 50 | columns = m.columns(), 51 | size = std::max(rows, columns); 52 | 53 | #ifdef DEBUG 54 | std::cout << "Munkres input: " << m << std::endl; 55 | #endif 56 | 57 | // Copy input matrix 58 | this->matrix = m; 59 | 60 | if ( rows != columns ) { 61 | // If the input matrix isn't square, make it square 62 | // and fill the empty values with the largest value present 63 | // in the matrix. 64 | matrix.resize(size, size, matrix.max()); 65 | } 66 | 67 | 68 | // STAR == 1 == starred, PRIME == 2 == primed 69 | mask_matrix.resize(size, size); 70 | 71 | row_mask = new bool[size]; 72 | col_mask = new bool[size]; 73 | for ( size_t i = 0 ; i < size ; i++ ) { 74 | row_mask[i] = false; 75 | } 76 | 77 | for ( size_t i = 0 ; i < size ; i++ ) { 78 | col_mask[i] = false; 79 | } 80 | 81 | // Prepare the matrix values... 82 | 83 | // If there were any infinities, replace them with a value greater 84 | // than the maximum value in the matrix. 85 | replace_infinites(matrix); 86 | 87 | minimize_along_direction(matrix, rows >= columns); 88 | minimize_along_direction(matrix, rows < columns); 89 | 90 | // Follow the steps 91 | int step = 1; 92 | while ( step ) { 93 | switch ( step ) { 94 | case 1: 95 | step = step1(); 96 | // step is always 2 97 | break; 98 | case 2: 99 | step = step2(); 100 | // step is always either 0 or 3 101 | break; 102 | case 3: 103 | step = step3(); 104 | // step in [3, 4, 5] 105 | break; 106 | case 4: 107 | step = step4(); 108 | // step is always 2 109 | break; 110 | case 5: 111 | step = step5(); 112 | // step is always 3 113 | break; 114 | } 115 | } 116 | 117 | // Store results 118 | for ( size_t row = 0 ; row < size ; row++ ) { 119 | for ( size_t col = 0 ; col < size ; col++ ) { 120 | if ( mask_matrix(row, col) == STAR ) { 121 | matrix(row, col) = 0; 122 | } else { 123 | matrix(row, col) = -1; 124 | } 125 | } 126 | } 127 | 128 | #ifdef DEBUG 129 | std::cout << "Munkres output: " << matrix << std::endl; 130 | #endif 131 | // Remove the excess rows or columns that we added to fit the 132 | // input to a square matrix. 133 | matrix.resize(rows, columns); 134 | 135 | m = matrix; 136 | 137 | delete [] row_mask; 138 | delete [] col_mask; 139 | } 140 | 141 | static void replace_infinites(Matrix &matrix) { 142 | const size_t rows = matrix.rows(), 143 | columns = matrix.columns(); 144 | assert( rows > 0 && columns > 0 ); 145 | double max = matrix(0, 0); 146 | constexpr auto infinity = std::numeric_limits::infinity(); 147 | 148 | // Find the greatest value in the matrix that isn't infinity. 149 | for ( size_t row = 0 ; row < rows ; row++ ) { 150 | for ( size_t col = 0 ; col < columns ; col++ ) { 151 | if ( matrix(row, col) != infinity ) { 152 | if ( max == infinity ) { 153 | max = matrix(row, col); 154 | } else { 155 | max = std::max(max, matrix(row, col)); 156 | } 157 | } 158 | } 159 | } 160 | 161 | // a value higher than the maximum value present in the matrix. 162 | if ( max == infinity ) { 163 | // This case only occurs when all values are infinite. 164 | max = 0; 165 | } else { 166 | max++; 167 | } 168 | 169 | for ( size_t row = 0 ; row < rows ; row++ ) { 170 | for ( size_t col = 0 ; col < columns ; col++ ) { 171 | if ( matrix(row, col) == infinity ) { 172 | matrix(row, col) = max; 173 | } 174 | } 175 | } 176 | 177 | } 178 | 179 | static void minimize_along_direction(Matrix &matrix, const bool over_columns) { 180 | const size_t outer_size = over_columns ? matrix.columns() : matrix.rows(), 181 | inner_size = over_columns ? matrix.rows() : matrix.columns(); 182 | 183 | // Look for a minimum value to subtract from all values along 184 | // the "outer" direction. 185 | for ( size_t i = 0 ; i < outer_size ; i++ ) { 186 | double min = over_columns ? matrix(0, i) : matrix(i, 0); 187 | 188 | // As long as the current minimum is greater than zero, 189 | // keep looking for the minimum. 190 | // Start at one because we already have the 0th value in min. 191 | for ( size_t j = 1 ; j < inner_size && min > 0 ; j++ ) { 192 | min = std::min( 193 | min, 194 | over_columns ? matrix(j, i) : matrix(i, j)); 195 | } 196 | 197 | if ( min > 0 ) { 198 | for ( size_t j = 0 ; j < inner_size ; j++ ) { 199 | if ( over_columns ) { 200 | matrix(j, i) -= min; 201 | } else { 202 | matrix(i, j) -= min; 203 | } 204 | } 205 | } 206 | } 207 | } 208 | 209 | private: 210 | 211 | inline bool find_uncovered_in_matrix(const double item, size_t &row, size_t &col) const { 212 | const size_t rows = matrix.rows(), 213 | columns = matrix.columns(); 214 | 215 | for ( row = 0 ; row < rows ; row++ ) { 216 | if ( !row_mask[row] ) { 217 | for ( col = 0 ; col < columns ; col++ ) { 218 | if ( !col_mask[col] ) { 219 | if ( matrix(row,col) == item ) { 220 | return true; 221 | } 222 | } 223 | } 224 | } 225 | } 226 | 227 | return false; 228 | } 229 | 230 | bool pair_in_list(const std::pair &needle, const std::list > &haystack) { 231 | for ( std::list >::const_iterator i = haystack.begin() ; i != haystack.end() ; i++ ) { 232 | if ( needle == *i ) { 233 | return true; 234 | } 235 | } 236 | 237 | return false; 238 | } 239 | 240 | int step1() { 241 | const size_t rows = matrix.rows(), 242 | columns = matrix.columns(); 243 | 244 | for ( size_t row = 0 ; row < rows ; row++ ) { 245 | for ( size_t col = 0 ; col < columns ; col++ ) { 246 | if ( 0 == matrix(row, col) ) { 247 | for ( size_t nrow = 0 ; nrow < row ; nrow++ ) 248 | if ( STAR == mask_matrix(nrow,col) ) 249 | goto next_column; 250 | 251 | mask_matrix(row,col) = STAR; 252 | goto next_row; 253 | } 254 | next_column:; 255 | } 256 | next_row:; 257 | } 258 | 259 | return 2; 260 | } 261 | 262 | int step2() { 263 | const size_t rows = matrix.rows(), 264 | columns = matrix.columns(); 265 | size_t covercount = 0; 266 | 267 | for ( size_t row = 0 ; row < rows ; row++ ) 268 | for ( size_t col = 0 ; col < columns ; col++ ) 269 | if ( STAR == mask_matrix(row, col) ) { 270 | col_mask[col] = true; 271 | covercount++; 272 | } 273 | 274 | if ( covercount >= matrix.minsize() ) { 275 | #ifdef DEBUG 276 | std::cout << "Final cover count: " << covercount << std::endl; 277 | #endif 278 | return 0; 279 | } 280 | 281 | #ifdef DEBUG 282 | std::cout << "Munkres matrix has " << covercount << " of " << matrix.minsize() << " Columns covered:" << std::endl; 283 | std::cout << matrix << std::endl; 284 | #endif 285 | 286 | 287 | return 3; 288 | } 289 | 290 | int step3() { 291 | /* 292 | Main Zero Search 293 | 294 | 1. Find an uncovered Z in the distance matrix and prime it. If no such zero exists, go to Step 5 295 | 2. If No Z* exists in the row of the Z', go to Step 4. 296 | 3. If a Z* exists, cover this row and uncover the column of the Z*. Return to Step 3.1 to find a new Z 297 | */ 298 | if ( find_uncovered_in_matrix(0, saverow, savecol) ) { 299 | mask_matrix(saverow,savecol) = PRIME; // prime it. 300 | } else { 301 | return 5; 302 | } 303 | 304 | for ( size_t ncol = 0 ; ncol < matrix.columns() ; ncol++ ) { 305 | if ( mask_matrix(saverow,ncol) == STAR ) { 306 | row_mask[saverow] = true; //cover this row and 307 | col_mask[ncol] = false; // uncover the column containing the starred zero 308 | return 3; // repeat 309 | } 310 | } 311 | 312 | return 4; // no starred zero in the row containing this primed zero 313 | } 314 | 315 | int step4() { 316 | const size_t rows = matrix.rows(), 317 | columns = matrix.columns(); 318 | 319 | // seq contains pairs of row/column values where we have found 320 | // either a star or a prime that is part of the ``alternating sequence``. 321 | std::list > seq; 322 | // use saverow, savecol from step 3. 323 | std::pair z0(saverow, savecol); 324 | seq.insert(seq.end(), z0); 325 | 326 | // We have to find these two pairs: 327 | std::pair z1(-1, -1); 328 | std::pair z2n(-1, -1); 329 | 330 | size_t row, col = savecol; 331 | /* 332 | Increment Set of Starred Zeros 333 | 334 | 1. Construct the ``alternating sequence'' of primed and starred zeros: 335 | 336 | Z0 : Unpaired Z' from Step 4.2 337 | Z1 : The Z* in the column of Z0 338 | Z[2N] : The Z' in the row of Z[2N-1], if such a zero exists 339 | Z[2N+1] : The Z* in the column of Z[2N] 340 | 341 | The sequence eventually terminates with an unpaired Z' = Z[2N] for some N. 342 | */ 343 | bool madepair; 344 | do { 345 | madepair = false; 346 | for ( row = 0 ; row < rows ; row++ ) { 347 | if ( mask_matrix(row,col) == STAR ) { 348 | z1.first = row; 349 | z1.second = col; 350 | if ( pair_in_list(z1, seq) ) { 351 | continue; 352 | } 353 | 354 | madepair = true; 355 | seq.insert(seq.end(), z1); 356 | break; 357 | } 358 | } 359 | 360 | if ( !madepair ) 361 | break; 362 | 363 | madepair = false; 364 | 365 | for ( col = 0 ; col < columns ; col++ ) { 366 | if ( mask_matrix(row, col) == PRIME ) { 367 | z2n.first = row; 368 | z2n.second = col; 369 | if ( pair_in_list(z2n, seq) ) { 370 | continue; 371 | } 372 | madepair = true; 373 | seq.insert(seq.end(), z2n); 374 | break; 375 | } 376 | } 377 | } while ( madepair ); 378 | 379 | for ( std::list >::iterator i = seq.begin() ; 380 | i != seq.end() ; 381 | i++ ) { 382 | // 2. Unstar each starred zero of the sequence. 383 | if ( mask_matrix(i->first,i->second) == STAR ) 384 | mask_matrix(i->first,i->second) = NORMAL; 385 | 386 | // 3. Star each primed zero of the sequence, 387 | // thus increasing the number of starred zeros by one. 388 | if ( mask_matrix(i->first,i->second) == PRIME ) 389 | mask_matrix(i->first,i->second) = STAR; 390 | } 391 | 392 | // 4. Erase all primes, uncover all columns and rows, 393 | for ( size_t row = 0 ; row < mask_matrix.rows() ; row++ ) { 394 | for ( size_t col = 0 ; col < mask_matrix.columns() ; col++ ) { 395 | if ( mask_matrix(row,col) == PRIME ) { 396 | mask_matrix(row,col) = NORMAL; 397 | } 398 | } 399 | } 400 | 401 | for ( size_t i = 0 ; i < rows ; i++ ) { 402 | row_mask[i] = false; 403 | } 404 | 405 | for ( size_t i = 0 ; i < columns ; i++ ) { 406 | col_mask[i] = false; 407 | } 408 | 409 | // and return to Step 2. 410 | return 2; 411 | } 412 | 413 | int step5() { 414 | const size_t rows = matrix.rows(), 415 | columns = matrix.columns(); 416 | /* 417 | New Zero Manufactures 418 | 419 | 1. Let h be the smallest uncovered entry in the (modified) distance matrix. 420 | 2. Add h to all covered rows. 421 | 3. Subtract h from all uncovered columns 422 | 4. Return to Step 3, without altering stars, primes, or covers. 423 | */ 424 | double h = std::numeric_limits::max(); 425 | for ( size_t row = 0 ; row < rows ; row++ ) { 426 | if ( !row_mask[row] ) { 427 | for ( size_t col = 0 ; col < columns ; col++ ) { 428 | if ( !col_mask[col] ) { 429 | if ( h > matrix(row, col) && matrix(row, col) != 0 ) { 430 | h = matrix(row, col); 431 | } 432 | } 433 | } 434 | } 435 | } 436 | 437 | for ( size_t row = 0 ; row < rows ; row++ ) { 438 | if ( row_mask[row] ) { 439 | for ( size_t col = 0 ; col < columns ; col++ ) { 440 | matrix(row, col) += h; 441 | } 442 | } 443 | } 444 | 445 | for ( size_t col = 0 ; col < columns ; col++ ) { 446 | if ( !col_mask[col] ) { 447 | for ( size_t row = 0 ; row < rows ; row++ ) { 448 | matrix(row, col) -= h; 449 | } 450 | } 451 | } 452 | 453 | return 3; 454 | } 455 | 456 | Matrix mask_matrix; 457 | Matrix matrix; 458 | bool *row_mask; 459 | bool *col_mask; 460 | size_t saverow = 0, savecol = 0; 461 | }; 462 | 463 | 464 | #endif /* !defined(_MUNKRES_H_) */ 465 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package (Boost COMPONENTS system REQUIRED) 2 | 3 | enable_testing () 4 | 5 | # Framework for writing tests. 6 | ExternalProject_Add ( 7 | googletest 8 | GIT_REPOSITORY "https://github.com/google/googletest.git" 9 | UPDATE_COMMAND cd ${PROJECT_BINARY_DIR}/googletest && mv googletest gtest && mv gtest/googletest . 10 | CMAKE_ARGS "-DCMAKE_BUILD_TYPE=Release" 11 | SOURCE_DIR "${PROJECT_BINARY_DIR}/googletest/googletest" 12 | BINARY_DIR "${PROJECT_BINARY_DIR}/googletest/googletest" 13 | INSTALL_COMMAND "" 14 | TEST_COMMAND "" 15 | ) 16 | set_target_properties (googletest PROPERTIES EXCLUDE_FROM_ALL TRUE) 17 | set (GTEST_ROOT "${PROJECT_BINARY_DIR}/googletest/googletest") 18 | set (GTEST_FOUND true) 19 | set (GTEST_INCLUDE_DIRS "${PROJECT_BINARY_DIR}/googletest/googletest/include") 20 | set (GTEST_LIBRARIES "${PROJECT_BINARY_DIR}/googletest/googletest/libgtest.a") 21 | set (GTEST_MAIN_LIBRARIES "${PROJECT_BINARY_DIR}/googletest/googletest/libgtest_main.a") 22 | set (GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) 23 | 24 | include_directories (${GTEST_INCLUDE_DIRS}) 25 | 26 | # Special flags fo generating code coverage. 27 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") 28 | set (CMAKE_SHARED_LINKER_FLAGS "-fprofile-arcs -ftest-coverage") 29 | 30 | # Special warning options for more attentive checking. 31 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wmissing-include-dirs -Wundef -Wfloat-equal -Wunsafe-loop-optimizations") 32 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wdouble-promotion -Winit-self -Wvector-operation-performance -Wnoexcept -Weffc++ -Wstrict-null-sentinel") 33 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Woverloaded-virtual -Wsign-promo") 34 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wvla -Winvalid-pch -Winline -Wredundant-decls") 35 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wlogical-op -Wcast-align") 36 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wcast-qual -Wpointer-arith -Wtrampolines") 37 | set (SPECIAL_CXX_WARNING_FLAGS "${SPECIAL_CXX_WARNING_FLAGS} -Wold-style-cast") 38 | 39 | set_source_files_properties ( 40 | ${MunkresCppLib_SOURCES} 41 | PROPERTIES COMPILE_FLAGS ${SPECIAL_CXX_WARNING_FLAGS} 42 | ) 43 | 44 | 45 | # Test suites. 46 | set ( 47 | MunkresCppTest_SOURCES 48 | ${PROJECT_SOURCE_DIR}/tests/munkrestest.cpp 49 | ${PROJECT_SOURCE_DIR}/tests/matrixtest.cpp 50 | ${PROJECT_SOURCE_DIR}/tests/adapters/std_2d_arraytest.cpp 51 | ${PROJECT_SOURCE_DIR}/tests/adapters/std_2d_vectortest.cpp 52 | ${PROJECT_SOURCE_DIR}/tests/adapters/boost_matrixtest.cpp 53 | ) 54 | add_executable (munkrestest EXCLUDE_FROM_ALL ${MunkresCppLib_SOURCES} ${MunkresCppTest_SOURCES}) 55 | target_link_libraries (munkrestest ${GTEST_BOTH_LIBRARIES} gcov pthread) 56 | add_custom_target (tests) 57 | add_dependencies (tests munkrestest) 58 | add_dependencies (munkrestest googletest) 59 | add_test (MunkresCppTest muknrestest) 60 | 61 | 62 | # Test coverage report. 63 | set (Coverage_REPORT ${PROJECT_BINARY_DIR}/coverage.info) 64 | set (Coverage_DIR ${PROJECT_BINARY_DIR}/coverage) 65 | add_custom_command ( 66 | OUTPUT ${Coverage_REPORT} 67 | COMMAND lcov -q -c -f -b . -d ${PROJECT_BINARY_DIR}/tests -o ${Coverage_REPORT} 68 | COMMAND lcov -e ${Coverage_REPORT} '${PROJECT_SOURCE_DIR}/src/*' -o ${Coverage_REPORT} 69 | COMMAND genhtml ${Coverage_REPORT} --legend --demangle-cpp -f -q -o ${Coverage_DIR} 70 | DEPENDS munkrestest 71 | ) 72 | add_custom_target (coverage DEPENDS ${Coverage_REPORT}) 73 | 74 | 75 | # Adding test coverage artifacts to the "make clean" rule. 76 | macro (determine_coverage_data Sources TestName Artifacts Suffix) 77 | set (CoverageDirectory "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${TestName}.dir") 78 | foreach (File ${Sources}) 79 | string (REGEX MATCH "^${CMAKE_CURRENT_SOURCE_DIR}*" Directory "${File}") 80 | if (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR) 81 | string (REGEX REPLACE "^${CMAKE_CURRENT_SOURCE_DIR}*" "${CoverageDirectory}" File "${File}") 82 | else (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR) 83 | string (REGEX REPLACE "/" ";" A "${CMAKE_CURRENT_SOURCE_DIR}") 84 | string (REGEX REPLACE "/" ";" B "${File}") 85 | list (LENGTH A DeepDirectory) 86 | list (LENGTH B DeepFile) 87 | set (File "${CoverageDirectory}") 88 | set (I 1) 89 | while (I less DeepDirectory) 90 | list (GET A ${I} AI) 91 | list (GET B ${I} BI) 92 | if (AI STREQUAL BI) 93 | math (EXPR I "${I} + 1") 94 | else (AI STREQUAL BI) 95 | math (EXPR DeepDiff "${DeepFile} - ${I} - 1") 96 | while (DeepDiff GREATER 0) 97 | set (File "${File}/__") 98 | math (EXPR DeepDiff "${DeepDiff} - 1") 99 | endwhile (DeepDiff GREATER 0) 100 | while (I less DeepFile) 101 | list (GET B ${I} BI) 102 | set (File "${File}/${BI}") 103 | math (EXPR I "${I} + 1") 104 | endwhile (I less DeepFile) 105 | endif (AI STREQUAL BI) 106 | endwhile (I less DeepDirectory) 107 | endif (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR) 108 | set (${Artifacts} ${${Artifacts}} "${File}${Suffix}") 109 | endforeach (File) 110 | endmacro (determine_coverage_data) 111 | 112 | determine_coverage_data ("${MunkresCppTest_SOURCES};${MunkresCppLib_SOURCES}" munkrestest Coverage_GCNO ".gcno") 113 | determine_coverage_data ("${MunkresCppTest_SOURCES};${MunkresCppLib_SOURCES}" munkrestest Coverage_GCDA ".gcda") 114 | 115 | list (APPEND Coverage_DATA "${Coverage_REPORT}") 116 | list (APPEND Coverage_DATA "${Coverage_DIR}") 117 | list (APPEND Coverage_DATA "${Coverage_GCNO}") 118 | list (APPEND Coverage_DATA "${Coverage_GCDA}") 119 | set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${Coverage_DATA}") 120 | -------------------------------------------------------------------------------- /tests/adapters/boost_matrixtest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "adapters/boostmatrixadapter.h" 3 | #include 4 | #include 5 | 6 | 7 | 8 | class Adapters_boost_matrix_Test : public ::testing::Test 9 | { 10 | }; 11 | 12 | 13 | 14 | TEST_F (Adapters_boost_matrix_Test, convert_boost_matrix_to_munkres_matrix_Success) 15 | { 16 | // Arrange. 17 | constexpr unsigned int dimension {3}; 18 | boost::numeric::ublas::matrix test_boost_matrix (dimension, dimension); 19 | // {1, 2, 3}, 20 | // {4, 5, 6}, 21 | // {7, 8, 9} 22 | test_boost_matrix (0, 0) = 1; test_boost_matrix (0, 1) = 2; test_boost_matrix (0, 2) = 3; 23 | test_boost_matrix (1, 0) = 4; test_boost_matrix (1, 1) = 5; test_boost_matrix (1, 2) = 6; 24 | test_boost_matrix (2, 0) = 7; test_boost_matrix (2, 1) = 8; test_boost_matrix (2, 2) = 9; 25 | const Matrix etalon_matrix { 26 | {1, 2, 3}, 27 | {4, 5, 6}, 28 | {7, 8, 9} 29 | }; 30 | 31 | BoostMatrixAdapter adapter; 32 | 33 | // Act. 34 | const auto test_matrix = adapter.convertToMatrix(test_boost_matrix); 35 | 36 | // Assert. 37 | for (unsigned int row = 0; row < dimension; ++row) { 38 | for (unsigned int col = 0; col < dimension; ++col) { 39 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 40 | } 41 | } 42 | } 43 | 44 | TEST_F (Adapters_boost_matrix_Test, convert_non_square_boost_matrix_to_munkres_matrix_Success) 45 | { 46 | // Arrange. 47 | constexpr unsigned int dimension1 {2}; 48 | constexpr unsigned int dimension2 {3}; 49 | boost::numeric::ublas::matrix test_boost_matrix (dimension1, dimension2); 50 | // {1, 2, 3}, 51 | // {4, 5, 6} 52 | test_boost_matrix (0, 0) = 1; test_boost_matrix (0, 1) = 2; test_boost_matrix (0, 2) = 3; 53 | test_boost_matrix (1, 0) = 4; test_boost_matrix (1, 1) = 5; test_boost_matrix (1, 2) = 6; 54 | const Matrix etalon_matrix { 55 | {1, 2, 3}, 56 | {4, 5, 6} 57 | }; 58 | 59 | BoostMatrixAdapter adapter; 60 | 61 | // Act. 62 | const auto test_matrix = adapter.convertToMatrix(test_boost_matrix); 63 | 64 | // Assert. 65 | for (unsigned int row = 0; row < dimension1; ++row) { 66 | for (unsigned int col = 0; col < dimension2; ++col) { 67 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 68 | } 69 | } 70 | } 71 | 72 | TEST_F (Adapters_boost_matrix_Test, fill_boost_matrix_from_munkres_matrix_Success) 73 | { 74 | // Arrange. 75 | constexpr unsigned int dimension {3}; 76 | boost::numeric::ublas::matrix test_boost_matrix (dimension, dimension); 77 | // {0, 0, 0}, 78 | // {0, 0, 0}, 79 | // {0, 0, 0} 80 | test_boost_matrix (0, 0) = 0; test_boost_matrix (0, 1) = 0; test_boost_matrix (0, 2) = 0; 81 | test_boost_matrix (1, 0) = 0; test_boost_matrix (1, 1) = 0; test_boost_matrix (1, 2) = 0; 82 | test_boost_matrix (2, 0) = 0; test_boost_matrix (2, 1) = 0; test_boost_matrix (2, 2) = 0; 83 | 84 | boost::numeric::ublas::matrix etalon_boost_matrix (dimension, dimension); 85 | // {1, 2, 3}, 86 | // {4, 5, 6}, 87 | // {7, 8, 9} 88 | etalon_boost_matrix (0, 0) = 1; etalon_boost_matrix (0, 1) = 2; etalon_boost_matrix (0, 2) = 3; 89 | etalon_boost_matrix (1, 0) = 4; etalon_boost_matrix (1, 1) = 5; etalon_boost_matrix (1, 2) = 6; 90 | etalon_boost_matrix (2, 0) = 7; etalon_boost_matrix (2, 1) = 8; etalon_boost_matrix (2, 2) = 9; 91 | 92 | const Matrix etalon_matrix { 93 | {1, 2, 3}, 94 | {4, 5, 6}, 95 | {7, 8, 9} 96 | }; 97 | 98 | BoostMatrixAdapter adapter; 99 | 100 | // Act. 101 | adapter.convertFromMatrix(test_boost_matrix, etalon_matrix); 102 | 103 | // Assert. 104 | for (unsigned int row = 0; row < dimension; ++row) { 105 | for (unsigned int col = 0; col < dimension; ++col) { 106 | EXPECT_EQ (etalon_boost_matrix (row, col), test_boost_matrix (row, col) ); 107 | } 108 | } 109 | } 110 | 111 | TEST_F (Adapters_boost_matrix_Test, fill_non_square_boost_matrix_from_munkres_matrix_Success) 112 | { 113 | // Arrange. 114 | constexpr unsigned int dimension1 {2}; 115 | constexpr unsigned int dimension2 {3}; 116 | boost::numeric::ublas::matrix test_boost_matrix (dimension1, dimension2); 117 | // {0, 0, 0}, 118 | // {0, 0, 0} 119 | test_boost_matrix (0, 0) = 0; test_boost_matrix (0, 1) = 0; test_boost_matrix (0, 2) = 0; 120 | test_boost_matrix (1, 0) = 0; test_boost_matrix (1, 1) = 0; test_boost_matrix (1, 2) = 0; 121 | 122 | boost::numeric::ublas::matrix etalon_boost_matrix (dimension1, dimension2); 123 | // {1, 2, 3}, 124 | // {4, 5, 6} 125 | etalon_boost_matrix (0, 0) = 1; etalon_boost_matrix (0, 1) = 2; etalon_boost_matrix (0, 2) = 3; 126 | etalon_boost_matrix (1, 0) = 4; etalon_boost_matrix (1, 1) = 5; etalon_boost_matrix (1, 2) = 6; 127 | 128 | const Matrix etalon_matrix { 129 | {1, 2, 3}, 130 | {4, 5, 6} 131 | }; 132 | 133 | BoostMatrixAdapter adapter; 134 | 135 | // Act. 136 | adapter.convertFromMatrix(test_boost_matrix, etalon_matrix); 137 | 138 | // Assert. 139 | for (unsigned int row = 0; row < dimension1; ++row) { 140 | for (unsigned int col = 0; col < dimension2; ++col) { 141 | EXPECT_EQ (etalon_boost_matrix (row, col), test_boost_matrix (row, col) ); 142 | } 143 | } 144 | } 145 | 146 | TEST_F (Adapters_boost_matrix_Test, solve_boost_matrix_Success) 147 | { 148 | // Arrange. 149 | constexpr unsigned int dimension {3}; 150 | boost::numeric::ublas::matrix etalon_boost_matrix (dimension, dimension); 151 | // {-1.0, 0.0, -1.0}, 152 | // { 0.0, -1.0, -1.0}, 153 | // {-1.0, -1.0, 0.0} 154 | etalon_boost_matrix (0, 0) = -1.0; etalon_boost_matrix (0, 1) = 0.0; etalon_boost_matrix (0, 2) = -1.0; 155 | etalon_boost_matrix (1, 0) = 0.0; etalon_boost_matrix (1, 1) = -1.0; etalon_boost_matrix (1, 2) = -1.0; 156 | etalon_boost_matrix (2, 0) = -1.0; etalon_boost_matrix (2, 1) = -1.0; etalon_boost_matrix (2, 2) = 0.0; 157 | 158 | boost::numeric::ublas::matrix test_boost_matrix (dimension, dimension); 159 | // {1.0, 0.0, 1.0}, 160 | // {0.0, 1.0, 1.0}, 161 | // {1.0, 1.0, 0.0} 162 | test_boost_matrix (0, 0) = 1.0; test_boost_matrix (0, 1) = 0.0; test_boost_matrix (0, 2) = 1.0; 163 | test_boost_matrix (1, 0) = 0.0; test_boost_matrix (1, 1) = 1.0; test_boost_matrix (1, 2) = 1.0; 164 | test_boost_matrix (2, 0) = 1.0; test_boost_matrix (2, 1) = 1.0; test_boost_matrix (2, 2) = 0.0; 165 | 166 | BoostMatrixAdapter adapter; 167 | 168 | // Act. 169 | adapter.solve(test_boost_matrix); 170 | 171 | // Assert. 172 | for (unsigned int row = 0; row < dimension; ++row) { 173 | for (unsigned int col = 0; col < dimension; ++col) { 174 | EXPECT_EQ (etalon_boost_matrix (row, col), test_boost_matrix (row, col) ); 175 | } 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /tests/adapters/std_2d_arraytest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "adapters/std2darrayadapter.h" 3 | #include 4 | #include 5 | 6 | 7 | 8 | class Adapters_std_2d_array_Test : public ::testing::Test 9 | { 10 | }; 11 | 12 | 13 | 14 | TEST_F (Adapters_std_2d_array_Test, convert_std_2d_array_to_munkres_matrix_Success) 15 | { 16 | // Arrange. 17 | constexpr unsigned int dimension {3}; 18 | const std::array , dimension> test_array {{ 19 | {1, 2, 3}, 20 | {4, 5, 6}, 21 | {7, 8, 9} 22 | }}; 23 | const Matrix etalon_matrix { 24 | {1, 2, 3}, 25 | {4, 5, 6}, 26 | {7, 8, 9} 27 | }; 28 | 29 | Std2dArrayAdapter adapter; 30 | 31 | // Act. 32 | const auto test_matrix = adapter.convertToMatrix(test_array); 33 | 34 | // Assert. 35 | for (unsigned int row = 0; row < dimension; ++row) { 36 | for (unsigned int col = 0; col < dimension; ++col) { 37 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 38 | } 39 | } 40 | } 41 | 42 | TEST_F (Adapters_std_2d_array_Test, convert_non_square_std_2d_array_to_munkres_matrix_Success) 43 | { 44 | // Arrange. 45 | constexpr unsigned int dimension1 {2}; 46 | constexpr unsigned int dimension2 {3}; 47 | const std::array , dimension1> test_array {{ 48 | {1, 2, 3}, 49 | {4, 5, 6} 50 | }}; 51 | const Matrix etalon_matrix { 52 | {1, 2, 3}, 53 | {4, 5, 6} 54 | }; 55 | 56 | Std2dArrayAdapter adapter; 57 | 58 | // Act. 59 | const auto test_matrix = adapter.convertToMatrix(test_array); 60 | 61 | // Assert. 62 | for (unsigned int row = 0; row < dimension1; ++row) { 63 | for (unsigned int col = 0; col < dimension2; ++col) { 64 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 65 | } 66 | } 67 | } 68 | 69 | TEST_F (Adapters_std_2d_array_Test, fill_std_2d_array_from_munkres_matrix_Success) 70 | { 71 | // Arrange. 72 | constexpr unsigned int dimension {3}; 73 | std::array , dimension> test_array {{ 74 | {0, 0, 0}, 75 | {0, 0, 0}, 76 | {0, 0, 0} 77 | }}; 78 | const std::array , dimension> etalon_array {{ 79 | {1, 2, 3}, 80 | {4, 5, 6}, 81 | {7, 8, 9} 82 | }}; 83 | const Matrix etalon_matrix { 84 | {1, 2, 3}, 85 | {4, 5, 6}, 86 | {7, 8, 9} 87 | }; 88 | 89 | Std2dArrayAdapter adapter; 90 | 91 | // Act. 92 | adapter.convertFromMatrix(test_array, etalon_matrix); 93 | 94 | // Assert. 95 | for (unsigned int row = 0; row < dimension; ++row) { 96 | for (unsigned int col = 0; col < dimension; ++col) { 97 | EXPECT_EQ (etalon_array [row][col], test_array [row][col]); 98 | } 99 | } 100 | } 101 | 102 | TEST_F (Adapters_std_2d_array_Test, fill_non_square_std_2d_array_from_munkres_matrix_Success) 103 | { 104 | // Arrange. 105 | constexpr unsigned int dimension1 {2}; 106 | constexpr unsigned int dimension2 {3}; 107 | std::array , dimension1> test_array {{ 108 | {0, 0, 0}, 109 | {0, 0, 0} 110 | }}; 111 | const std::array , dimension1> etalon_array {{ 112 | {1, 2, 3}, 113 | {4, 5, 6} 114 | }}; 115 | const Matrix etalon_matrix { 116 | {1, 2, 3}, 117 | {4, 5, 6} 118 | }; 119 | 120 | Std2dArrayAdapter adapter; 121 | 122 | // Act. 123 | adapter.convertFromMatrix(test_array, etalon_matrix); 124 | 125 | // Assert. 126 | for (unsigned int row = 0; row < dimension1; ++row) { 127 | for (unsigned int col = 0; col < dimension2; ++col) { 128 | EXPECT_EQ (etalon_array [row][col], test_array [row][col]); 129 | } 130 | } 131 | } 132 | 133 | TEST_F (Adapters_std_2d_array_Test, solve_std_2d_array_Success) 134 | { 135 | // Arrange. 136 | constexpr unsigned int dimension {3}; 137 | std::array , dimension> etalon_array{{ 138 | {-1.0, 0.0, -1.0}, 139 | { 0.0, -1.0, -1.0}, 140 | {-1.0, -1.0, 0.0} 141 | }}; 142 | std::array , dimension> test_array{{ 143 | {1.0, 0.0, 1.0}, 144 | {0.0, 1.0, 1.0}, 145 | {1.0, 1.0, 0.0} 146 | }}; 147 | 148 | Std2dArrayAdapter adapter; 149 | 150 | // Act. 151 | adapter.solve(test_array); 152 | 153 | // Assert. 154 | for (unsigned int row = 0; row < dimension; ++row) { 155 | for (unsigned int col = 0; col < dimension; ++col) { 156 | EXPECT_EQ (etalon_array [row][col], test_array [row][col]); 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /tests/adapters/std_2d_vectortest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "adapters/std2dvectordapter.h" 3 | 4 | 5 | 6 | class Adapters_std_2d_vector_Test : public ::testing::Test 7 | { 8 | }; 9 | 10 | TEST_F (Adapters_std_2d_vector_Test, convert_std_2d_vector_to_munkres_matrix_Success) 11 | { 12 | // Arrange. 13 | constexpr unsigned int dimension {3}; 14 | const std::vector > test_vector {{ 15 | {1, 2, 3}, 16 | {4, 5, 6}, 17 | {7, 8, 9} 18 | }}; 19 | const Matrix etalon_matrix { 20 | {1, 2, 3}, 21 | {4, 5, 6}, 22 | {7, 8, 9} 23 | }; 24 | 25 | Std2dVectorAdapter adapter; 26 | 27 | // Act. 28 | const auto test_matrix = adapter.convertToMatrix(test_vector); 29 | 30 | // Assert. 31 | for (unsigned int row = 0; row < dimension; ++row) { 32 | for (unsigned int col = 0; col < dimension; ++col) { 33 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 34 | } 35 | } 36 | } 37 | 38 | TEST_F (Adapters_std_2d_vector_Test, convert_non_square_std_2d_vector_to_munkres_matrix_Success) 39 | { 40 | // Arrange. 41 | constexpr unsigned int dimension1 {2}; 42 | constexpr unsigned int dimension2 {3}; 43 | const std::vector > test_vector {{ 44 | {1, 2, 3}, 45 | {4, 5, 6} 46 | }}; 47 | const Matrix etalon_matrix { 48 | {1, 2, 3}, 49 | {4, 5, 6} 50 | }; 51 | 52 | Std2dVectorAdapter adapter; 53 | 54 | // Act. 55 | const auto test_matrix = adapter.convertToMatrix(test_vector); 56 | 57 | // Assert. 58 | for (unsigned int row = 0; row < dimension1; ++row) { 59 | for (unsigned int col = 0; col < dimension2; ++col) { 60 | EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) ); 61 | } 62 | } 63 | } 64 | 65 | TEST_F (Adapters_std_2d_vector_Test, fill_std_2d_vector_from_munkres_matrix_Success) 66 | { 67 | // Arrange. 68 | constexpr unsigned int dimension {3}; 69 | std::vector > test_vector {{ 70 | {0, 0, 0}, 71 | {0, 0, 0}, 72 | {0, 0, 0} 73 | }}; 74 | const std::vector > etalon_vector {{ 75 | {1, 2, 3}, 76 | {4, 5, 6}, 77 | {7, 8, 9} 78 | }}; 79 | const Matrix etalon_matrix { 80 | {1, 2, 3}, 81 | {4, 5, 6}, 82 | {7, 8, 9} 83 | }; 84 | 85 | Std2dVectorAdapter adapter; 86 | 87 | // Act. 88 | adapter.convertFromMatrix(test_vector, etalon_matrix); 89 | 90 | // Assert. 91 | for (unsigned int row = 0; row < dimension; ++row) { 92 | for (unsigned int col = 0; col < dimension; ++col) { 93 | EXPECT_EQ (etalon_vector [row][col], test_vector [row][col]); 94 | } 95 | } 96 | } 97 | 98 | TEST_F (Adapters_std_2d_vector_Test, fill_non_square_std_2d_vector_from_munkres_matrix_Success) 99 | { 100 | // Arrange. 101 | constexpr unsigned int dimension1 {2}; 102 | constexpr unsigned int dimension2 {3}; 103 | std::vector > test_vector {{ 104 | {0, 0, 0}, 105 | {0, 0, 0} 106 | }}; 107 | const std::vector > etalon_vector {{ 108 | {1, 2, 3}, 109 | {4, 5, 6} 110 | }}; 111 | const Matrix etalon_matrix { 112 | {1, 2, 3}, 113 | {4, 5, 6} 114 | }; 115 | 116 | Std2dVectorAdapter adapter; 117 | 118 | // Act. 119 | adapter.convertFromMatrix(test_vector, etalon_matrix); 120 | 121 | // Assert. 122 | for (unsigned int row = 0; row < dimension1; ++row) { 123 | for (unsigned int col = 0; col < dimension2; ++col) { 124 | EXPECT_EQ (etalon_vector [row][col], test_vector [row][col]); 125 | } 126 | } 127 | } 128 | 129 | TEST_F (Adapters_std_2d_vector_Test, solve_std_2d_vector_Success) 130 | { 131 | // Arrange. 132 | constexpr unsigned int dimension {3}; 133 | std::vector > etalon_vector {{ 134 | {-1.0, 0.0, -1.0}, 135 | { 0.0, -1.0, -1.0}, 136 | {-1.0, -1.0, 0.0} 137 | }}; 138 | std::vector > test_vector {{ 139 | {1.0, 0.0, 1.0}, 140 | {0.0, 1.0, 1.0}, 141 | {1.0, 1.0, 0.0} 142 | }}; 143 | 144 | Std2dVectorAdapter adapter; 145 | 146 | // Act. 147 | adapter.solve (test_vector); 148 | 149 | // Assert. 150 | for (unsigned int row = 0; row < dimension; ++row) { 151 | for (unsigned int col = 0; col < dimension; ++col) { 152 | EXPECT_EQ (etalon_vector [row][col], test_vector [row][col]); 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /tests/matrixtest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "matrixtest.h" 3 | #include 4 | #include 5 | 6 | 7 | 8 | class MatrixTest : public ::testing::Test 9 | { 10 | protected: 11 | virtual void SetUp (); 12 | virtual void TearDown (); 13 | }; 14 | 15 | 16 | 17 | void MatrixTest::SetUp () 18 | { 19 | } 20 | 21 | 22 | 23 | void MatrixTest::TearDown () 24 | { 25 | } 26 | 27 | 28 | 29 | TEST_F (MatrixTest, resize_From1x1To2x2WithDefaultValueDefaulted_Success) 30 | { 31 | // Arrange. 32 | Matrix etalon_matrix{ 33 | {7.0, 0.0}, 34 | {0.0, 0.0} 35 | }; 36 | Matrix test_matrix{ 37 | {7.0} 38 | }; 39 | 40 | // Act. 41 | test_matrix.resize (2, 2); 42 | 43 | // Assert. 44 | EXPECT_EQ (etalon_matrix, test_matrix); 45 | } 46 | 47 | 48 | 49 | TEST_F (MatrixTest, resize_From1x1To5x5WithDefaultValueDefaulted_Success) 50 | { 51 | // Arrange. 52 | Matrix etalon_matrix{ 53 | {7.0, 0.0, 0.0, 0.0, 0.0}, 54 | {0.0, 0.0, 0.0, 0.0, 0.0}, 55 | {0.0, 0.0, 0.0, 0.0, 0.0}, 56 | {0.0, 0.0, 0.0, 0.0, 0.0}, 57 | {0.0, 0.0, 0.0, 0.0, 0.0} 58 | }; 59 | Matrix test_matrix{ 60 | {7.0} 61 | }; 62 | 63 | // Act. 64 | test_matrix.resize (5, 5); 65 | 66 | // Assert. 67 | EXPECT_EQ (etalon_matrix, test_matrix); 68 | } 69 | 70 | 71 | 72 | TEST_F (MatrixTest, resize_From5x5To3x3WithDefaultValueDefaulted_Success) 73 | { 74 | // Arrange. 75 | Matrix etalon_matrix{ 76 | {0.0, 0.1, 0.2}, 77 | {1.0, 1.1, 1.2}, 78 | {2.0, 2.1, 2.2} 79 | }; 80 | Matrix test_matrix{ 81 | {0.0, 0.1, 0.2, 0.3, 0.4}, 82 | {1.0, 1.1, 1.2, 1.3, 1.4}, 83 | {2.0, 2.1, 2.2, 2.3, 2.4}, 84 | {3.0, 3.1, 3.2, 3.3, 3.4}, 85 | {4.0, 4.1, 4.2, 4.3, 4.4} 86 | }; 87 | 88 | // Act. 89 | test_matrix.resize (3, 3); 90 | 91 | // Assert. 92 | EXPECT_EQ (etalon_matrix, test_matrix); 93 | } 94 | 95 | 96 | 97 | TEST_F (MatrixTest, resize_From2x2To4x4WithDefaultValueExplicit_Success) 98 | { 99 | // Arrange. 100 | Matrix etalon_matrix{ 101 | {0.0, 0.1, 9.9, 9.9}, 102 | {1.0, 1.1, 9.9, 9.9}, 103 | {9.9, 9.9, 9.9, 9.9}, 104 | {9.9, 9.9, 9.9, 9.9} 105 | }; 106 | Matrix test_matrix{ 107 | {0.0, 0.1}, 108 | {1.0, 1.1} 109 | }; 110 | 111 | // Act. 112 | test_matrix.resize (4, 4, 9.9); 113 | 114 | // Assert. 115 | EXPECT_EQ (etalon_matrix, test_matrix); 116 | } 117 | 118 | 119 | 120 | TEST_F (MatrixTest, clear_1x1_Success) 121 | { 122 | // Arrange. 123 | Matrix etalon_matrix{ 124 | {0.0} 125 | }; 126 | Matrix test_matrix{ 127 | {7.0} 128 | }; 129 | 130 | // Act. 131 | test_matrix.clear (); 132 | 133 | // Assert. 134 | EXPECT_EQ (etalon_matrix, test_matrix); 135 | } 136 | 137 | 138 | 139 | TEST_F (MatrixTest, clear_4x4_Success) 140 | { 141 | // Arrange. 142 | Matrix etalon_matrix{ 143 | {0.0, 0.0, 0.0, 0.0}, 144 | {0.0, 0.0, 0.0, 0.0}, 145 | {0.0, 0.0, 0.0, 0.0}, 146 | {0.0, 0.0, 0.0, 0.0} 147 | }; 148 | Matrix test_matrix{ 149 | {1.1, 1.2, 1.3, 1.4}, 150 | {2.1, 2.2, 2.3, 2.4}, 151 | {3.1, 3.2, 3.3, 3.4}, 152 | {4.1, 4.2, 4.3, 4.4} 153 | }; 154 | 155 | // Act. 156 | test_matrix.clear (); 157 | 158 | // Assert. 159 | EXPECT_EQ (etalon_matrix, test_matrix); 160 | } 161 | 162 | 163 | 164 | TEST_F (MatrixTest, operatorAssignment_0x0_Success) 165 | { 166 | // Arrange. 167 | Matrix etalon_matrix {std::initializer_list > () }; 168 | Matrix test_matrix{ 169 | {0.0, 0.1, 0.2}, 170 | {3.0, 3.1, 3.2}, 171 | {4.0, 4.1, 4.2} 172 | }; 173 | 174 | // Act. 175 | test_matrix = etalon_matrix; 176 | 177 | // Assert. 178 | EXPECT_EQ (etalon_matrix, test_matrix); 179 | } 180 | 181 | 182 | 183 | TEST_F (MatrixTest, operatorAssignment_3x3_Success) 184 | { 185 | // Arrange. 186 | Matrix etalon_matrix{ 187 | {0.0, 0.1, 0.2}, 188 | {1.0, 1.1, 1.2}, 189 | {2.0, 2.1, 2.2} 190 | }; 191 | Matrix test_matrix; 192 | 193 | // Act. 194 | test_matrix = etalon_matrix; 195 | 196 | // Assert. 197 | EXPECT_EQ (etalon_matrix, test_matrix); 198 | } 199 | 200 | 201 | 202 | TEST_F (MatrixTest, operatorSubscript_Success) 203 | { 204 | // Arrange. 205 | Matrix test_matrix{ 206 | {0.0, 0.1, 0.2}, 207 | {1.0, 1.1, 1.2}, 208 | {2.0, 2.1, 2.2} 209 | }; 210 | 211 | // Act, Assert. 212 | EXPECT_FLOAT_EQ (0.0, test_matrix (0, 0) ); 213 | EXPECT_FLOAT_EQ (0.1, test_matrix (0, 1) ); 214 | EXPECT_FLOAT_EQ (0.2, test_matrix (0, 2) ); 215 | EXPECT_FLOAT_EQ (1.0, test_matrix (1, 0) ); 216 | EXPECT_FLOAT_EQ (1.1, test_matrix (1, 1) ); 217 | EXPECT_FLOAT_EQ (1.2, test_matrix (1, 2) ); 218 | EXPECT_FLOAT_EQ (2.0, test_matrix (2, 0) ); 219 | EXPECT_FLOAT_EQ (2.1, test_matrix (2, 1) ); 220 | EXPECT_FLOAT_EQ (2.2, test_matrix (2, 2) ); 221 | } 222 | 223 | 224 | 225 | TEST_F (MatrixTest, max_1x1_Success) 226 | { 227 | // Arrange. 228 | Matrix test_matrix{ 229 | {0.0} 230 | }; 231 | constexpr double etalon_result {0.0}; 232 | 233 | // Act. 234 | const double test_result = test_matrix.max (); 235 | 236 | // Assert. 237 | EXPECT_FLOAT_EQ (etalon_result, test_result); 238 | } 239 | 240 | 241 | 242 | TEST_F (MatrixTest, max_2x2MinusInfinity_Success) 243 | { 244 | // Arrange. 245 | constexpr auto minusInfinity = - std::numeric_limits ::infinity (); 246 | Matrix test_matrix{ 247 | {minusInfinity, minusInfinity}, 248 | {minusInfinity, minusInfinity} 249 | }; 250 | constexpr double etalon_result {minusInfinity}; 251 | 252 | // Act. 253 | const double test_result = test_matrix.max (); 254 | 255 | // Assert. 256 | EXPECT_FLOAT_EQ (etalon_result, test_result); 257 | } 258 | 259 | 260 | 261 | TEST_F (MatrixTest, max_2x2Negative_Success) 262 | { 263 | // Arrange. 264 | constexpr auto minusInfinity = - std::numeric_limits ::infinity (); 265 | constexpr auto negativeValue = std::numeric_limits ::min (); 266 | Matrix test_matrix{ 267 | {negativeValue, minusInfinity}, 268 | {minusInfinity, negativeValue} 269 | }; 270 | constexpr double etalon_result {negativeValue}; 271 | 272 | // Act. 273 | const double test_result = test_matrix.max (); 274 | 275 | // Assert. 276 | EXPECT_FLOAT_EQ (etalon_result, test_result); 277 | } 278 | 279 | 280 | 281 | TEST_F (MatrixTest, max_2x2Zero_Success) 282 | { 283 | // Arrange. 284 | constexpr auto minusInfinity = - std::numeric_limits ::infinity (); 285 | constexpr auto negativeValue = std::numeric_limits ::min (); 286 | Matrix test_matrix{ 287 | { 0.0, minusInfinity}, 288 | {minusInfinity, negativeValue} 289 | }; 290 | constexpr double etalon_result {0.0}; 291 | 292 | // Act. 293 | const double test_result = test_matrix.max (); 294 | 295 | // Assert. 296 | EXPECT_FLOAT_EQ (etalon_result, test_result); 297 | } 298 | 299 | 300 | 301 | TEST_F (MatrixTest, max_2x2Positive_Success) 302 | { 303 | // Arrange. 304 | constexpr auto minusInfinity = - std::numeric_limits ::infinity (); 305 | constexpr auto negativeValue = std::numeric_limits ::min (); 306 | constexpr auto positiveValue = std::numeric_limits ::max (); 307 | Matrix test_matrix{ 308 | {negativeValue, minusInfinity}, 309 | {positiveValue, 0.0} 310 | }; 311 | constexpr double etalon_result {positiveValue}; 312 | 313 | // Act. 314 | const double test_result = test_matrix.max (); 315 | 316 | // Assert. 317 | EXPECT_FLOAT_EQ (etalon_result, test_result); 318 | } 319 | 320 | 321 | 322 | TEST_F (MatrixTest, max_2x2PlusInfinity_Success) 323 | { 324 | // Arrange. 325 | constexpr auto minusInfinity = - std::numeric_limits ::infinity (); 326 | constexpr auto negativeValue = std::numeric_limits ::min (); 327 | constexpr auto positiveValue = std::numeric_limits ::max (); 328 | constexpr auto plusInfinity = std::numeric_limits ::infinity (); 329 | Matrix test_matrix{ 330 | {minusInfinity, positiveValue}, 331 | {negativeValue, plusInfinity} 332 | }; 333 | constexpr double etalon_result {plusInfinity}; 334 | 335 | // Act. 336 | const double test_result = test_matrix.max (); 337 | 338 | // Assert. 339 | EXPECT_FLOAT_EQ (etalon_result, test_result); 340 | } 341 | 342 | 343 | 344 | TEST_F (MatrixTest, minsize_RowsCountIsMin_Success) 345 | { 346 | // Arrange. 347 | Matrix test_matrix (1u, 2u); 348 | constexpr unsigned int etalon_result {1u}; 349 | 350 | // Act. 351 | const unsigned int test_result = test_matrix.minsize (); 352 | 353 | // Assert. 354 | EXPECT_EQ (etalon_result, test_result); 355 | } 356 | 357 | 358 | 359 | TEST_F (MatrixTest, minsize_ColumnsCountIsMin_Success) 360 | { 361 | // Arrange. 362 | Matrix test_matrix (2u, 1u); 363 | constexpr unsigned int etalon_result {1u}; 364 | 365 | // Act. 366 | const unsigned int test_result = test_matrix.minsize (); 367 | 368 | // Assert. 369 | EXPECT_EQ (etalon_result, test_result); 370 | } 371 | 372 | 373 | 374 | TEST_F (MatrixTest, minsize_RowsCountAndColumnsCountAreEqual_Success) 375 | { 376 | // Arrange. 377 | Matrix test_matrix (3u, 3u); 378 | constexpr unsigned int etalon_result {3u}; 379 | 380 | // Act. 381 | const unsigned int test_result = test_matrix.minsize (); 382 | 383 | // Assert. 384 | EXPECT_EQ (etalon_result, test_result); 385 | } 386 | 387 | 388 | 389 | TEST_F (MatrixTest, columns_Success) 390 | { 391 | // Arrange. 392 | Matrix test_matrix{ 393 | {0.0, 0.1, 0.2}, 394 | {1.0, 1.1, 1.2}, 395 | {2.0, 2.1, 2.2} 396 | }; 397 | constexpr unsigned int etalon_result {3u}; 398 | 399 | // Act. 400 | const unsigned int test_result = test_matrix.columns (); 401 | 402 | // Assert. 403 | EXPECT_EQ (etalon_result, test_result); 404 | } 405 | 406 | 407 | 408 | TEST_F (MatrixTest, rows_Success) 409 | { 410 | // Arrange. 411 | Matrix test_matrix{ 412 | {0.0, 0.1, 0.2}, 413 | {1.0, 1.1, 1.2}, 414 | {2.0, 2.1, 2.2} 415 | }; 416 | constexpr unsigned int etalon_result {3u}; 417 | 418 | // Act. 419 | const unsigned int test_result = test_matrix.rows (); 420 | 421 | // Assert. 422 | EXPECT_EQ (etalon_result, test_result); 423 | } 424 | -------------------------------------------------------------------------------- /tests/matrixtest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007 John Weaver 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #if !defined(_MATRIX_TEST_H_) 20 | #define _MATRIX_TEST_H_ 21 | 22 | #include "matrix.h" 23 | #include 24 | 25 | template 26 | bool operator == (const Matrix & a, const Matrix & b) 27 | { 28 | if (a.rows () != b.rows () || a.columns () != b.columns () ) { 29 | return false; 30 | } 31 | 32 | for (unsigned int row = 0; row < a.rows (); ++row) { 33 | for (unsigned int col = 0; col < a.columns (); ++col) { 34 | if (a (row, col) != b (row, col) ) { 35 | return false; 36 | } 37 | } 38 | } 39 | 40 | return true; 41 | } 42 | 43 | 44 | 45 | template 46 | bool operator != (const Matrix & a, const Matrix & b) 47 | { 48 | return ! (a == b); 49 | } 50 | 51 | 52 | 53 | template 54 | std::ostream & operator << (std::ostream & os, const Matrix & m) 55 | { 56 | const std::string indent (" "); 57 | os << "Matrix (" << & m << ") of " << m.rows () << "x" << m.columns () << std::endl; 58 | for (unsigned int row = 0; row < m.rows (); ++row) { 59 | os << indent; 60 | for (unsigned int col = 0; col < m.columns (); ++col) { 61 | os << std::setw (4) << std::setfill (' ') << m (row, col) << " "; 62 | } 63 | os << std::endl; 64 | } 65 | 66 | return os; 67 | } 68 | 69 | #endif /* !defined(_MATRIX_TEST_H_) */ 70 | 71 | -------------------------------------------------------------------------------- /tests/munkrestest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "munkres.h" 3 | #include "matrixtest.h" 4 | #include 5 | #include 6 | 7 | class MunkresTest : public ::testing::Test 8 | { 9 | protected: 10 | virtual void SetUp (); 11 | virtual void TearDown (); 12 | 13 | Matrix generateRandomMatrix (const int, const int); 14 | void isSingleSolution (Matrix &); 15 | void isValidOutput (Matrix &); 16 | }; 17 | 18 | 19 | 20 | void MunkresTest::SetUp () 21 | { 22 | } 23 | 24 | 25 | 26 | void MunkresTest::TearDown () 27 | { 28 | } 29 | 30 | 31 | 32 | Matrix MunkresTest::generateRandomMatrix(const int nrows, const int ncols) 33 | { 34 | Matrix matrix(nrows, ncols); 35 | 36 | srandom(time(nullptr)); // Seed random number generator. 37 | 38 | // Initialize matrix with random values. 39 | for ( unsigned int row = 0 ; row < matrix.rows() ; row++ ) 40 | for ( unsigned int col = 0 ; col < matrix.columns() ; col++ ) 41 | matrix(row,col) = (double)random(); 42 | 43 | return matrix; 44 | } 45 | 46 | 47 | 48 | void MunkresTest::isSingleSolution (Matrix & matrix) 49 | { 50 | for ( unsigned int row = 0 ; row < matrix.rows() ; row++ ) { 51 | int columnsolutioncount = 0; 52 | for ( unsigned int col = 0 ; col < matrix.columns() ; col++ ) 53 | if ( matrix(row,col) == 0 ) 54 | columnsolutioncount++; 55 | EXPECT_EQ ( columnsolutioncount, 1 ); 56 | } 57 | 58 | for ( unsigned int col = 0 ; col < matrix.columns() ; col++ ) { 59 | int rowsolutioncount = 0; 60 | for ( unsigned int row = 0 ; row < matrix.rows() ; row++ ) 61 | if ( matrix(row,col) == 0 ) 62 | rowsolutioncount++; 63 | EXPECT_EQ ( rowsolutioncount, 1 ); 64 | } 65 | } 66 | 67 | 68 | 69 | void MunkresTest::isValidOutput (Matrix & matrix) 70 | { 71 | for ( unsigned int row = 0 ; row < matrix.rows() ; row++ ) 72 | for ( unsigned int col = 0 ; col < matrix.columns() ; col++ ) 73 | EXPECT_TRUE ( matrix(row,col) == 0 || matrix(row,col) == -1 ); 74 | } 75 | 76 | 77 | 78 | TEST_F (MunkresTest, replace_infinites_4x4Case001_Success) 79 | { 80 | // Arrange. 81 | const auto infinity = std::numeric_limits::infinity(); 82 | Matrix etalon_matrix{ 83 | { 1.0, 0.0, 3.0, 2.0}, 84 | { 3.0, -2.0, -1.0, 0.0}, 85 | {-1.0, 3.0, 2.0, 0.0}, 86 | {-1.0, 0.0, 2.0, 3.0} 87 | }; 88 | Matrix test_matrix{ 89 | { 1.0, 0.0, infinity, 2.0}, 90 | {infinity, -2.0, -1.0, 0.0}, 91 | {-1.0, infinity, 2.0, 0.0}, 92 | {-1.0, 0.0, 2.0, infinity} 93 | }; 94 | 95 | // Act. 96 | Munkres::replace_infinites (test_matrix); 97 | 98 | // Assert. 99 | EXPECT_EQ (etalon_matrix, test_matrix); 100 | } 101 | 102 | 103 | 104 | TEST_F (MunkresTest, replace_infinites_4x4Case002_Success) 105 | { 106 | // Arrange. 107 | const auto infinity = std::numeric_limits::infinity(); 108 | Matrix etalon_matrix{ 109 | { 3.0, 0.0, 3.0, 2.0}, 110 | { 3.0, -2.0, -1.0, 3.0}, 111 | {-1.0, 3.0, 2.0, 3.0}, 112 | {-1.0, 3.0, 2.0, 3.0} 113 | }; 114 | Matrix test_matrix{ 115 | {infinity, 0.0, infinity, 2.0}, 116 | {infinity, -2.0, -1.0, infinity}, 117 | {-1.0, infinity, 2.0, infinity}, 118 | {-1.0, infinity, 2.0, infinity} 119 | }; 120 | 121 | // Act. 122 | Munkres::replace_infinites(test_matrix); 123 | 124 | // Assert. 125 | EXPECT_EQ (etalon_matrix, test_matrix); 126 | } 127 | 128 | 129 | 130 | TEST_F (MunkresTest, replace_infinites_4x4Case003_Success) 131 | { 132 | // Arrange. 133 | const auto infinity = std::numeric_limits::infinity(); 134 | Matrix etalon_matrix{ 135 | {-5.0, -4.0, -1.0, -2.0}, 136 | {-1.0, -2.0, -5.0, -4.0}, 137 | {-5.0, -1.0, -2.0, -4.0}, 138 | {-5.0, -4.0, -2.0, -1.0} 139 | }; 140 | Matrix test_matrix{ 141 | {-5.0, -4.0, infinity, -2.0}, 142 | {infinity, -2.0, -5.0, -4.0}, 143 | {-5.0, infinity, -2.0, -4.0}, 144 | {-5.0, -4.0, -2.0, infinity} 145 | }; 146 | 147 | // Act. 148 | Munkres::replace_infinites (test_matrix); 149 | 150 | // Assert. 151 | EXPECT_EQ (etalon_matrix, test_matrix); 152 | } 153 | 154 | 155 | 156 | TEST_F (MunkresTest, replace_infinites_4x4Case004_Success) 157 | { 158 | // Arrange. 159 | const auto infinity = std::numeric_limits::infinity(); 160 | Matrix etalon_matrix{ 161 | { 1.0, 0.0, 3.0, 2.0}, 162 | { 3.0, -2.0, -1.0, 0.0}, 163 | {-1.0, 3.0, 0.0, 0.0}, 164 | {-1.0, 0.0, 0.0, 3.0} 165 | }; 166 | Matrix test_matrix{ 167 | { 1.0, 0.0, infinity, 2.0}, 168 | {infinity, -2.0, -1.0, 0.0}, 169 | {-1.0, infinity, 0.0, 0.0}, 170 | {-1.0, 0.0, 0.0, infinity} 171 | }; 172 | 173 | // Act. 174 | Munkres::replace_infinites (test_matrix); 175 | 176 | // Assert. 177 | EXPECT_EQ (etalon_matrix, test_matrix); 178 | } 179 | 180 | 181 | 182 | TEST_F (MunkresTest, replace_infinites_4x4Case005_Success) 183 | { 184 | // Arrange. 185 | const auto infinity = std::numeric_limits::infinity(); 186 | Matrix etalon_matrix{ 187 | { 0.0, 0.0, 0.0, 0.0}, 188 | { 0.0, 0.0, 0.0, 0.0}, 189 | { 0.0, 0.0, 0.0, 0.0}, 190 | { 0.0, 0.0, 0.0, 0.0} 191 | }; 192 | Matrix test_matrix{ 193 | {infinity, infinity, infinity, infinity}, 194 | {infinity, infinity, infinity, infinity}, 195 | {infinity, infinity, infinity, infinity}, 196 | {infinity, infinity, infinity, infinity} 197 | }; 198 | 199 | // Act. 200 | Munkres::replace_infinites (test_matrix); 201 | 202 | // Assert. 203 | EXPECT_EQ (etalon_matrix, test_matrix); 204 | } 205 | 206 | 207 | 208 | TEST_F (MunkresTest, minimize_along_direction_5x5_OverRowsOnly_Success) 209 | { 210 | // Arrange. 211 | Matrix etalon_matrix{ 212 | { 1.0, 0.0, 3.0, 2.0, 4.0}, 213 | { 3.0, -2.0, -1.0, 0.0, 4.0}, 214 | {-1.0, 3.0, 2.0, 1.0, 2.0}, 215 | { 0.0, 2.0, 1.0, 0.0, 3.0}, 216 | { 0.0, 1.0, 1.0, 0.0, 2.0} 217 | }; 218 | Matrix test_matrix{ 219 | { 1.0, 0.0, 3.0, 2.0, 4.0}, 220 | { 3.0, -2.0, -1.0, 0.0, 4.0}, 221 | {-1.0, 3.0, 2.0, 1.0, 2.0}, 222 | { 1.0, 3.0, 2.0, 1.0, 4.0}, 223 | { 2.0, 3.0, 3.0, 2.0, 4.0} 224 | }; 225 | 226 | // Act. 227 | Munkres::minimize_along_direction(test_matrix, false); 228 | 229 | // Assert. 230 | EXPECT_EQ (etalon_matrix, test_matrix); 231 | } 232 | 233 | 234 | 235 | TEST_F (MunkresTest, minimize_along_direction_5x5_OverColumnsOnly_Success) 236 | { 237 | // Arrange. 238 | Matrix etalon_matrix{ 239 | { 1.0, 0.0, 3.0, 2.0, 2.0}, 240 | { 3.0, -2.0, -1.0, 0.0, 2.0}, 241 | {-1.0, 3.0, 2.0, 1.0, 0.0}, 242 | { 1.0, 3.0, 2.0, 1.0, 2.0}, 243 | { 2.0, 3.0, 3.0, 2.0, 2.0} 244 | }; 245 | Matrix test_matrix{ 246 | { 1.0, 0.0, 3.0, 2.0, 4.0}, 247 | { 3.0, -2.0, -1.0, 0.0, 4.0}, 248 | {-1.0, 3.0, 2.0, 1.0, 2.0}, 249 | { 1.0, 3.0, 2.0, 1.0, 4.0}, 250 | { 2.0, 3.0, 3.0, 2.0, 4.0} 251 | }; 252 | 253 | // Act. 254 | Munkres::minimize_along_direction(test_matrix, true); 255 | 256 | // Assert. 257 | EXPECT_EQ (etalon_matrix, test_matrix); 258 | } 259 | 260 | 261 | 262 | TEST_F (MunkresTest, minimize_along_direction_5x5_OverRowsAndColumns_Success) 263 | { 264 | // Arrange. 265 | Matrix etalon_matrix{ 266 | { 1.0, 0.0, 3.0, 2.0, 2.0}, 267 | { 3.0, -2.0, -1.0, 0.0, 2.0}, 268 | {-1.0, 3.0, 2.0, 1.0, 0.0}, 269 | { 0.0, 2.0, 1.0, 0.0, 1.0}, 270 | { 0.0, 1.0, 1.0, 0.0, 0.0} 271 | }; 272 | Matrix test_matrix{ 273 | { 1.0, 0.0, 3.0, 2.0, 4.0}, 274 | { 3.0, -2.0, -1.0, 0.0, 4.0}, 275 | {-1.0, 3.0, 2.0, 1.0, 2.0}, 276 | { 1.0, 3.0, 2.0, 1.0, 4.0}, 277 | { 2.0, 3.0, 3.0, 2.0, 4.0} 278 | }; 279 | 280 | // Act. 281 | Munkres::minimize_along_direction(test_matrix, false); 282 | Munkres::minimize_along_direction(test_matrix, true); 283 | 284 | // Assert. 285 | EXPECT_EQ (etalon_matrix, test_matrix); 286 | } 287 | 288 | 289 | 290 | TEST_F (MunkresTest, solve_5x5_IsSingleSolution_Success) 291 | { 292 | // Arrange. 293 | Matrix matrix = generateRandomMatrix(5, 5); 294 | Munkres munkres; 295 | 296 | // Act. 297 | munkres.solve(matrix); 298 | 299 | // Assert. 300 | isSingleSolution(matrix); 301 | } 302 | 303 | 304 | 305 | TEST_F (MunkresTest, solve_10x10_IsSingleSolution_Success) 306 | { 307 | // Arrange. 308 | Matrix matrix = generateRandomMatrix(10, 10); 309 | Munkres munkres; 310 | 311 | // Act. 312 | munkres.solve(matrix); 313 | 314 | // Assert. 315 | isSingleSolution(matrix); 316 | } 317 | 318 | 319 | 320 | TEST_F (MunkresTest, solve_50x50_IsSingleSolution_Success) 321 | { 322 | // Arrange. 323 | Matrix matrix = generateRandomMatrix(50, 50); 324 | Munkres munkres; 325 | 326 | // Act. 327 | munkres.solve(matrix); 328 | 329 | // Assert. 330 | isSingleSolution(matrix); 331 | } 332 | 333 | 334 | 335 | TEST_F (MunkresTest, solve_100x100_IsSingleSolution_Success) 336 | { 337 | // Arrange. 338 | Matrix matrix = generateRandomMatrix(100, 100); 339 | Munkres munkres; 340 | 341 | // Act. 342 | munkres.solve(matrix); 343 | 344 | // Assert. 345 | isSingleSolution(matrix); 346 | } 347 | 348 | 349 | 350 | TEST_F (MunkresTest, solve_200x200_IsSingleSolution_Success) 351 | { 352 | // Arrange. 353 | Matrix matrix = generateRandomMatrix(200, 200); 354 | Munkres munkres; 355 | 356 | // Act. 357 | munkres.solve(matrix); 358 | 359 | // Assert. 360 | isSingleSolution(matrix); 361 | } 362 | 363 | 364 | 365 | TEST_F (MunkresTest, solve_10x10_IsValideOutput_Success) 366 | { 367 | // Arrange. 368 | Matrix matrix = generateRandomMatrix(10, 10); 369 | Munkres munkres; 370 | 371 | // Act. 372 | munkres.solve(matrix); 373 | 374 | // Assert. 375 | isValidOutput (matrix); 376 | } 377 | 378 | 379 | 380 | TEST_F (MunkresTest, solve_1x1_ObviousSolution_Success) 381 | { 382 | // Arrange. 383 | Matrix etalon_matrix{ 384 | {0.0} 385 | }; 386 | Matrix test_matrix{ 387 | {0.0} 388 | }; 389 | 390 | Munkres munkres; 391 | 392 | // Act. 393 | munkres.solve(test_matrix); 394 | 395 | // Assert. 396 | EXPECT_EQ (etalon_matrix, test_matrix); 397 | } 398 | 399 | 400 | 401 | TEST_F (MunkresTest, solve_2x2_ObviousSolution_Success) 402 | { 403 | // Arrange. 404 | Matrix etalon_matrix{ 405 | {-1.0, 0.0}, 406 | { 0.0, -1.0} 407 | }; 408 | Matrix test_matrix{ 409 | {1.0, 0.0}, 410 | {0.0, 1.0} 411 | }; 412 | 413 | Munkres munkres; 414 | 415 | // Act. 416 | munkres.solve(test_matrix); 417 | 418 | // Assert. 419 | EXPECT_EQ (etalon_matrix, test_matrix); 420 | } 421 | 422 | 423 | 424 | TEST_F (MunkresTest, solve_3x3_ObviousSolution_Success) 425 | { 426 | // Arrange. 427 | Matrix etalon_matrix{ 428 | {-1.0, 0.0, -1.0}, 429 | { 0.0, -1.0, -1.0}, 430 | {-1.0, -1.0, 0.0} 431 | }; 432 | Matrix test_matrix{ 433 | {1.0, 0.0, 1.0}, 434 | {0.0, 1.0, 1.0}, 435 | {1.0, 1.0, 0.0} 436 | }; 437 | 438 | Munkres munkres; 439 | 440 | // Act. 441 | munkres.solve(test_matrix); 442 | 443 | // Assert. 444 | EXPECT_EQ (etalon_matrix, test_matrix); 445 | } 446 | 447 | 448 | 449 | TEST_F (MunkresTest, solve_3x2_NonObviousSolutionCase001_Success) 450 | { 451 | // Arrange. 452 | Matrix etalon_matrix{ 453 | {-1.0, 0.0}, 454 | { 0.0, -1.0}, 455 | {-1.0, -1.0} 456 | }; 457 | Matrix test_matrix{ 458 | {1.0, 2.0}, 459 | {0.0, 9.0}, 460 | {9.0, 9.0} 461 | }; 462 | 463 | Munkres munkres; 464 | 465 | // Act. 466 | munkres.solve(test_matrix); 467 | 468 | // Assert. 469 | EXPECT_EQ (etalon_matrix, test_matrix); 470 | } 471 | 472 | 473 | 474 | // This is simplified version of test case #008. 475 | TEST_F (MunkresTest, solve_3x2_NonObviousSolutionCase002_Success) 476 | { 477 | // Arrange. 478 | Matrix etalon_matrix{ 479 | {-1.0, -1.0}, 480 | { 0.0, -1.0}, 481 | {-1.0, 0.0} 482 | }; 483 | Matrix test_matrix{ 484 | {1.0e+17, 3}, 485 | {2, 1.0e+17}, 486 | {4, 1} 487 | }; 488 | 489 | Munkres munkres; 490 | 491 | // Act. 492 | munkres.solve(test_matrix); 493 | 494 | // Assert. 495 | EXPECT_EQ (etalon_matrix, test_matrix); 496 | } 497 | 498 | 499 | 500 | // This is simplified version of test case #009 (transposed version of test case 002). 501 | TEST_F (MunkresTest, solve_2x3_NonObviousSolutionCase003_Success) 502 | { 503 | // Arrange. 504 | Matrix etalon_matrix{ 505 | {-1.0, 0.0, -1.0}, 506 | {-1.0, -1.0, 0.0} 507 | }; 508 | Matrix test_matrix{ 509 | {1.0e+17, 2, 4}, 510 | {3, 1.0e+17, 1} 511 | }; 512 | 513 | Munkres munkres; 514 | 515 | // Act. 516 | munkres.solve(test_matrix); 517 | 518 | // Assert. 519 | EXPECT_EQ (etalon_matrix, test_matrix); 520 | } 521 | 522 | 523 | 524 | // This is test case based on test case #002, but extended by one "impossible" task and one "lazy" worker. 525 | TEST_F (MunkresTest, solve_4x3_NonObviousSolutionCase004_Success) 526 | { 527 | // Arrange. 528 | Matrix etalon_matrix{ 529 | {-1.0, -1.0, -1.0}, 530 | { 0.0, -1.0, -1.0}, 531 | {-1.0, -1.0, 0.0}, 532 | {-1.0, 0.0, -1.0} 533 | }; 534 | Matrix test_matrix{ 535 | {1.0e+17, 3, 1.0e+17}, 536 | {2, 1.0e+17, 1.0e+17}, 537 | {1.0e+17, 1.0e+17, 1.0e+17}, 538 | {4, 1, 1.0e+17} 539 | }; 540 | 541 | Munkres munkres; 542 | 543 | // Act. 544 | munkres.solve(test_matrix); 545 | 546 | // Assert. 547 | EXPECT_EQ (etalon_matrix (1, 0), test_matrix (1, 0) ); 548 | EXPECT_EQ (etalon_matrix (3, 1), test_matrix (3, 1) ); 549 | } 550 | 551 | 552 | 553 | // This is test case based on test case #003, but extended by one "impossible" task and one "lazy" worker. 554 | TEST_F (MunkresTest, solve_3x4_NonObviousSolutionCase005_Success) 555 | { 556 | // Arrange. 557 | Matrix etalon_matrix{ 558 | {-1.0, 0.0, -1.0, -1.0}, 559 | {-1.0, -1.0, -1.0, 0.0}, 560 | {-1.0, -1.0, 0.0, -1.0} 561 | }; 562 | Matrix test_matrix{ 563 | {1.0e+17, 2, 1.0e17, 4}, 564 | {3, 1.0e+17, 1.0e17, 1}, 565 | {1.0e+17, 1.0e+17, 1.0e17, 1.0e+17} 566 | }; 567 | 568 | Munkres munkres; 569 | 570 | // Act. 571 | munkres.solve(test_matrix); 572 | 573 | // Assert. 574 | EXPECT_EQ (etalon_matrix (0, 1), test_matrix (0, 1) ); 575 | EXPECT_EQ (etalon_matrix (1, 3), test_matrix (1, 3) ); 576 | } 577 | 578 | 579 | 580 | TEST_F (MunkresTest, solve_3x3_NonObviousSolutionCase006_Success) 581 | { 582 | // Arrange. 583 | Matrix etalon_matrix{ 584 | {-1.0, 0.0, -1.0}, 585 | { 0.0, -1.0, -1.0}, 586 | {-1.0, -1.0, 0.0} 587 | }; 588 | Matrix test_matrix{ 589 | {1.0, 2.0, 1.0}, 590 | {0.0, 9.0, 9.0}, 591 | {9.0, 9.0, 0.0} 592 | }; 593 | 594 | Munkres munkres; 595 | 596 | // Act. 597 | munkres.solve(test_matrix); 598 | 599 | // Assert. 600 | EXPECT_EQ (etalon_matrix, test_matrix); 601 | } 602 | 603 | 604 | 605 | TEST_F (MunkresTest, solve_3x3_NonObviousSolutionCase007_Success) 606 | { 607 | // Arrange. 608 | Matrix etalon_matrix{ 609 | {-1.0, -1.0, 0.0}, 610 | {-1.0, 0.0, -1.0}, 611 | { 0.0, -1.0, -1.0} 612 | }; 613 | Matrix test_matrix{ 614 | {0.0, 0.0, 4.0}, 615 | {4.0, 3.0, 9.0}, 616 | {3.0, 4.0, 9.0} 617 | }; 618 | 619 | Munkres munkres; 620 | 621 | // Act. 622 | munkres.solve(test_matrix); 623 | 624 | // Assert. 625 | EXPECT_EQ (etalon_matrix, test_matrix); 626 | } 627 | 628 | 629 | 630 | TEST_F (MunkresTest, solve_6x4_NonObviousSolutionCase008_Success) 631 | { 632 | // Arrange. 633 | Matrix etalon_matrix{ 634 | {-1.0, -1.0, -1.0, -1.0}, 635 | { 0.0, -1.0, -1.0, -1.0}, 636 | {-1.0, 0.0, -1.0, -1.0}, 637 | {-1.0, -1.0, -1.0, -1.0}, 638 | {-1.0, -1.0, -1.0, 0.0}, 639 | {-1.0, -1.0, 0.0, -1.0} 640 | }; 641 | Matrix test_matrix{ 642 | {1.79769e+308, 7.33184e+08, 9.41561e+08, 2.79247e+08}, 643 | {3.06449e+08, 1.79769e+308, 3.3464e+08, 7.06878e+08}, 644 | {9.93296e+08, 1.9414e+08, 1.79769e+308, 1.14174e+08}, 645 | {3.51623e+08, 2.48635e+08, 7.81242e+08, 1.79769e+308}, 646 | {7.02639e+08, 8.51663e+08, 9.37382e+08, 4.96945e+07}, 647 | {7.58851e+08, 8.58445e+08, 8.7235e+07, 5.47076e+08} 648 | }; 649 | 650 | Munkres munkres; 651 | 652 | // Act. 653 | munkres.solve(test_matrix); 654 | 655 | // Assert. 656 | EXPECT_EQ (etalon_matrix, test_matrix); 657 | } 658 | 659 | 660 | 661 | TEST_F (MunkresTest, solve_4x6_NonObviousSolutionCase009_Success) 662 | { 663 | // Arrange. 664 | Matrix etalon_matrix{ 665 | {-1.0, 0.0, -1.0, -1.0, -1.0, -1.0}, 666 | {-1.0, -1.0, 0.0, -1.0, -1.0, -1.0}, 667 | {-1.0, -1.0, -1.0, -1.0, -1.0, 0.0}, 668 | {-1.0, -1.0, -1.0, -1.0, 0.0, -1.0} 669 | }; 670 | Matrix test_matrix{ 671 | {1.79769e+308, 3.06449e+08, 9.93296e+08, 3.51623e+08, 7.02639e+08, 7.58851e+08}, 672 | {7.33184e+08, 1.79769e+308, 1.9414e+08, 2.48635e+08, 8.51663e+08, 8.58445e+08}, 673 | {9.41561e+08, 3.3464e+08, 1.79769e+308, 7.81242e+08, 9.37382e+08, 8.7235e+07}, 674 | {2.79247e+08, 7.06878e+08, 1.14174e+08, 1.79769e+308, 4.96945e+07, 5.47076e+08} 675 | }; 676 | 677 | Munkres munkres; 678 | 679 | // Act. 680 | munkres.solve(test_matrix); 681 | 682 | // Assert. 683 | EXPECT_EQ (etalon_matrix, test_matrix); 684 | } 685 | 686 | 687 | 688 | TEST_F (MunkresTest, solve_3x3_IsValide_Fail) 689 | { 690 | // Arrange. 691 | Matrix etalon_matrix{ 692 | { 0.0, -1.0, -1.0}, 693 | // ^ ^ 694 | // | | 695 | // Wrong Wrong 696 | { 0.0, -1.0, -1.0}, 697 | {-1.0, -1.0, 0.0} 698 | }; 699 | Matrix test_matrix{ 700 | {1.0, 0.0, 1.0}, 701 | {0.0, 1.0, 1.0}, 702 | {1.0, 1.0, 0.0} 703 | }; 704 | 705 | Munkres munkres; 706 | 707 | // Act. 708 | munkres.solve(test_matrix); 709 | 710 | // Assert. 711 | EXPECT_NE (etalon_matrix, test_matrix); 712 | } 713 | --------------------------------------------------------------------------------