|
| 1 | + |
| 2 | +-- |
| 3 | +-- Copyright (C) 2018 <fastrgv@gmail.com> |
| 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 3 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 may read the full text of the GNU General Public License |
| 16 | +-- at <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +-- This algorithm was copied on 20sep18 from: |
| 19 | +-- https://users.cs.duke.edu/~brd/Teaching/ |
| 20 | +-- Bio/asmb/current/Handouts/munkres.html |
| 21 | +-- and modified to correct some errors. It has now been tested |
| 22 | +-- on thousands of testcases and seems to be working properly. |
| 23 | +-- Please send any improvements or further corrections back to: |
| 24 | +-- <fastrgv@gmail.com> |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +with text_io; |
| 30 | +with munkres; |
| 31 | + |
| 32 | + |
| 33 | +procedure munktest is |
| 34 | + |
| 35 | + use text_io; |
| 36 | + |
| 37 | + d: constant integer := 11; |
| 38 | + |
| 39 | + use munkres; |
| 40 | + |
| 41 | + assn: iatype(1..d); |
| 42 | + |
| 43 | + x: constant integer := 65534; |
| 44 | + cost: iatype(1..d*d) := ( |
| 45 | + |
| 46 | + 0, 2, 3, 3, x, 4, 5, 5, 6, 6, x, |
| 47 | + 2, 0, 3, 1, x, 2, 5, 3, 6, 4, x, |
| 48 | + 3, 1, 4, 2, 1, 3, 6, 4, 7, 5, x, |
| 49 | + x, x, 2, 4, x, 5, 6, 6, 7, 7, x, |
| 50 | + x, x, 1, 3, x, 4, 5, 5, 6, 6, x, |
| 51 | + x, x, 3, 1, 0, 2, 5, 3, 6, 4, x, |
| 52 | + x, x, 2, 4, x, 5, 6, 6, 7, 7, x, |
| 53 | + x, x, 3, 1, x, 0, 3, 1, 4, 2, x, |
| 54 | + x, x, 4, 2, 1, 1, 4, 2, 5, 3, x, |
| 55 | + x, x, 8, 8, x, 7, 6, 6, 3, 5, 4, |
| 56 | + x, x, 7, 7, x, 6, 5, 5, 2, 4, 3 |
| 57 | + |
| 58 | + ); |
| 59 | + |
| 60 | + |
| 61 | + j,r,c,total: integer := 0; |
| 62 | + |
| 63 | + function indx(r,c: integer) return integer is |
| 64 | + begin |
| 65 | + return (r-1)*d+c; |
| 66 | + end indx; |
| 67 | + |
| 68 | + Ok: boolean; |
| 69 | + |
| 70 | +begin |
| 71 | + |
| 72 | + munkres.hungarian(cost,assn,Ok); |
| 73 | + |
| 74 | + for i in 1..d loop |
| 75 | + r:=i; |
| 76 | + c:=assn(i); |
| 77 | + put("row="&integer'image(r)); |
| 78 | + put(" matches col="&integer'image(c)); |
| 79 | + new_line; |
| 80 | + if c>d or c<1 then |
| 81 | + put_line("col is bogus"); |
| 82 | + if Ok then |
| 83 | + put_line("Ok=true ???"); |
| 84 | + else |
| 85 | + put_line("Ok=FALSE !!!"); |
| 86 | + end if; |
| 87 | + raise program_error; |
| 88 | + end if; |
| 89 | + total := total + cost( indx(r,c) ); |
| 90 | + end loop; |
| 91 | + put_line("Total Cost: "&integer'image(total)); |
| 92 | + |
| 93 | +end munktest; |
| 94 | + |
0 commit comments