├── .gitmodules ├── tests ├── __init__.py ├── test_measurementmodel.py ├── test_target.py ├── test_motionmodel.py ├── test_utils.py ├── test_kftarget.py ├── test_track.py ├── test_mht.py ├── test_hypgen.py ├── test_clusterhyp.py └── test_cluster.py ├── Tupfile ├── murty ├── __init__.py ├── Tupfile ├── pymod.sh ├── murty.cpp ├── lap.hpp └── murty.hpp ├── Tuprules.tup ├── mht ├── __init__.py ├── models.py ├── hypgen.py ├── sensors.py ├── target.py ├── utils.py ├── plot.py ├── clusterhyp.py ├── kf.py ├── track.py ├── cluster.py └── mht.py ├── README.md ├── setup.py ├── .gitignore ├── plots ├── icetracking.py ├── crosstrack.png.py └── multicluster.png.py └── gpl.txt /.gitmodules: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Tupfile: -------------------------------------------------------------------------------- 1 | include_rules 2 | -------------------------------------------------------------------------------- /murty/__init__.py: -------------------------------------------------------------------------------- 1 | from .murty import Murty 2 | -------------------------------------------------------------------------------- /murty/Tupfile: -------------------------------------------------------------------------------- 1 | include_rules 2 | run $(TOP)/murty/pymod.sh 3 | -------------------------------------------------------------------------------- /murty/pymod.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ext=$(python3-config --extension-suffix) 3 | inc=$(python3 -m pybind11 --includes) 4 | echo ':murty.cpp |> $(COMPILER) $(CFLAGS) $(APP_CFLAGS) $(PY_CFLAGS)' $inc '%f -o %o $(LDFLAGS) $(APP_LDFLAGS) $(PY_LDFLAGS) |>' "%B${ext}" 5 | -------------------------------------------------------------------------------- /murty/murty.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "murty.hpp" 6 | 7 | namespace py = pybind11; 8 | 9 | PYBIND11_MODULE(murty, m) { 10 | py::class_(m, "Murty") 11 | .def(py::init()) 12 | .def("draw", &lap::Murty::draw_tuple); 13 | } 14 | -------------------------------------------------------------------------------- /Tuprules.tup: -------------------------------------------------------------------------------- 1 | TOP = $(TUP_CWD) 2 | 3 | CFLAGS = -std=c++17 4 | CFLAGS += -Wall 5 | CFLAGS += -Werror 6 | CFLAGS += -Wno-unknown-pragmas 7 | CFLAGS += -Wfatal-errors 8 | CFLAGS += -pedantic-errors 9 | CFLAGS += -Wextra 10 | #CFLAGS += -Wcast-align 11 | #CFLAGS += -g 12 | CFLAGS += -O3 13 | CFLAGS += -fPIC 14 | CFLAGS += -isystem/usr/include/eigen3 15 | CFLAGS += -isystem/usr/include/Python3.11 16 | 17 | PY_CFLAGS = -shared -Wno-nested-anon-types -Wno-unused-result -Wsign-compare -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes 18 | 19 | COMPILER = clang++ -Qunused-arguments 20 | 21 | .gitignore 22 | -------------------------------------------------------------------------------- /mht/__init__.py: -------------------------------------------------------------------------------- 1 | """Init module.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from .mht import * 19 | 20 | from . import kf 21 | from . import models 22 | from . import sensors 23 | from . import plot 24 | from .target import Target 25 | 26 | del mht 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multiple Hypothesis Tracking 2 | This is an implementation of the Multiple Hypothesis Tracking filter, 3 | implemented for educational purposes and for the purpose of the article 4 | ''Spatially Indexed Clustering for Scalable Tracking of Remotely Sensed Drift 5 | Ice'' accepted for the IEEE Aerospace 2017 conference, Big Sky, MT. 6 | 7 | In particular, this implementation studies the use of Spatial Indexing in the 8 | MHT clustering process. 9 | 10 | Please be advised that this implementation is educational above efficient, and would require some understanding of the algorithm to tune. 11 | 12 | # Building 13 | The library is built usig the ''[tup](http://gittup.org/tup/)'' buildsystem. Just install tup and execute ''tup'' in the root directory, and a python 3.7 module will be built for python 3.7. If you want to build for a different python version, edit the files in [this commit](https://github.com/jonatanolofsson/mht/commit/c4af9c313c4e44ca23edd418b5281618fc29693d) correspondingly. 14 | 15 | # Examples 16 | Have a look at the [test_mht-file](https://github.com/jonatanolofsson/mht/blob/master/tests/test_mht.py) for a usage example 17 | 18 | # License 19 | This software is released under the GPLv3 license. 20 | -------------------------------------------------------------------------------- /tests/test_measurementmodel.py: -------------------------------------------------------------------------------- 1 | """Test measurement model.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import numpy as np 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | import mht 25 | 26 | 27 | class TestMV2D(unittest.TestCase): 28 | """Test constant velocity update function.""" 29 | 30 | def setUp(self): 31 | """Set up.""" 32 | self.x = np.array([0] * 4) 33 | self.mfn = mht.models.position_measurement 34 | 35 | def test_measure(self): 36 | """Test simple update.""" 37 | z, H = self.mfn(self.x) 38 | self.assertAlmostEqual(z[0], 0) 39 | -------------------------------------------------------------------------------- /tests/test_target.py: -------------------------------------------------------------------------------- 1 | """Test Target methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | from unittest.mock import MagicMock 20 | from unittest.mock import patch 21 | import os 22 | import sys 23 | 24 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 25 | from mht.target import Target 26 | 27 | 28 | class TestTarget(unittest.TestCase): 29 | """Target method tests.""" 30 | 31 | def setUp(self): 32 | """Set up.""" 33 | self.filter = MagicMock() 34 | 35 | @patch('mht.target.Track') 36 | def test_initial(self, trackmock): 37 | """Test creation of new target.""" 38 | t = Target.initial(self.filter, 0) 39 | 40 | self.assertEqual(len(t.tracks), 1) 41 | -------------------------------------------------------------------------------- /tests/test_motionmodel.py: -------------------------------------------------------------------------------- 1 | """Test motion model.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import numpy as np 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | import mht 25 | 26 | 27 | class TestCV2D(unittest.TestCase): 28 | """Test constant velocity update function.""" 29 | 30 | def setUp(self): 31 | """Set up.""" 32 | self.x = np.array([0] * 4) 33 | self.P = np.eye(4) 34 | self.model = mht.models.ConstantVelocityModel(0.1) 35 | 36 | def test_update(self): 37 | """Test simple update.""" 38 | dT = 1 39 | x, P = self.model(self.x, self.P, dT) 40 | self.assertAlmostEqual(x[0], self.x[0] + self.x[2] * dT) 41 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """A setuptools based setup module. 2 | 3 | See: 4 | https://packaging.python.org/en/latest/distributing.html 5 | https://github.com/pypa/sampleproject 6 | """ 7 | 8 | # Always prefer setuptools over distutils 9 | from setuptools import setup, find_packages 10 | # To use a consistent encoding 11 | from codecs import open 12 | from os import path 13 | 14 | here = path.abspath(path.dirname(__file__)) 15 | 16 | setup( 17 | name='mht', 18 | version='1.0.0', 19 | 20 | description='A Multiple Hypothesis Tracker', 21 | 22 | # The project's main homepage. 23 | url='https://github.com/jonatanolofsson/mht', 24 | 25 | # Author details 26 | author='Jonatan Olofsson', 27 | author_email='jonatan.olofsson@gmail.com', 28 | 29 | # Choose your license 30 | license='GPLv3', 31 | 32 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 33 | classifiers=[ 34 | # How mature is this project? Common values are 35 | # 3 - Alpha 36 | # 4 - Beta 37 | # 5 - Production/Stable 38 | 'Development Status :: 4 - Beta', 39 | 40 | # Specify the Python versions you support here. In particular, ensure 41 | # that you indicate whether you support Python 2, Python 3 or both. 42 | 'Programming Language :: Python :: 3.7', 43 | ], 44 | 45 | # What does your project relate to? 46 | keywords='tracking multi-target', 47 | 48 | # You can just specify the packages manually here if your project is 49 | # simple. Or you can use find_packages(). 50 | packages=find_packages(exclude=['contrib', 'docs', 'tests*']), 51 | ) 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/python 2 | 3 | ### Python ### 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | *$py.class 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *,cover 49 | .hypothesis/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # IPython Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # dotenv 82 | .env 83 | 84 | # virtualenv 85 | venv/ 86 | ENV/ 87 | 88 | # Spyder project settings 89 | .spyderproject 90 | 91 | # Rope project settings 92 | .ropeproject 93 | 94 | ##### TUP GITIGNORE ##### 95 | ##### Lines below automatically generated by Tup. 96 | ##### Do not edit. 97 | .tup 98 | /.gitignore 99 | -------------------------------------------------------------------------------- /tests/test_utils.py: -------------------------------------------------------------------------------- 1 | """Test utility functions.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import os 20 | import sys 21 | 22 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 23 | import mht.utils 24 | 25 | 26 | class TestUtils(unittest.TestCase): 27 | """Testshell for utilities.""" 28 | 29 | def test_overlap_p_1(self): 30 | """Test the overlap_p function.""" 31 | a = (0, 1, 0, 1) 32 | b = (0.2, 2, 0.2, 2) 33 | res = mht.utils.overlap_pa(a, b) 34 | 35 | self.assertAlmostEqual(res, 0.64) 36 | 37 | def test_overlap_p_2(self): 38 | """Test the overlap_p function.""" 39 | a = (0, 1, 0, 1) 40 | b = (-0.2, 2, -0.2, 2) 41 | res = mht.utils.overlap_pa(a, b) 42 | 43 | self.assertAlmostEqual(res, 1) 44 | 45 | def test_overlap_p_3(self): 46 | """Test the overlap_p function.""" 47 | a = (0, 1, 0, 1) 48 | b = (0.2, 0.8, 0.2, 0.8) 49 | res = mht.utils.overlap_pa(a, b) 50 | 51 | self.assertAlmostEqual(res, 0.36) 52 | -------------------------------------------------------------------------------- /tests/test_kftarget.py: -------------------------------------------------------------------------------- 1 | """Test Kalman Filter Target class.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import numpy as np 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | import mht 25 | 26 | 27 | class TestKFTarget(unittest.TestCase): 28 | """Testcases for KF Target.""" 29 | 30 | def setUp(self): 31 | """Set up.""" 32 | model = mht.models.ConstantVelocityModel(0.1) 33 | self.x0 = np.array([0.0] * 4) 34 | self.P0 = np.eye(4) 35 | self.target = mht.kf.KFilter(model, self.x0, self.P0) 36 | 37 | def test_predict(self): 38 | """Predict step.""" 39 | dT = 1 40 | self.target.predict(dT) 41 | self.assertAlmostEqual(self.target.x[0], self.x0[0] + self.x0[2] * dT) 42 | 43 | def test_correct(self): 44 | """Correction step.""" 45 | z = np.array([2.0] * 2) 46 | R = np.eye(2) 47 | m = mht.Report(z, R, mht.models.velocity_measurement) 48 | self.target.correct(m) 49 | self.assertAlmostEqual(self.target.x[1], 0.0) 50 | self.assertAlmostEqual(self.target.x[2], 1.0) 51 | self.assertAlmostEqual(self.target.x[3], 1.0) 52 | -------------------------------------------------------------------------------- /mht/models.py: -------------------------------------------------------------------------------- 1 | """Motion and measurement models.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import numpy as np 19 | 20 | 21 | class ConstantVelocityModel: 22 | """Constant velocity motion model.""" 23 | 24 | def __init__(self, q): 25 | """Init.""" 26 | self.q = q 27 | 28 | def __call__(self, xprev, Pprev, dT): 29 | """Step model.""" 30 | x = xprev 31 | F = np.array([[1, 0, dT, 0], 32 | [0, 1, 0, dT], 33 | [0, 0, 1, 0], 34 | [0, 0, 0, 1]]) 35 | Q = np.array([[dT ** 3 / 3, 0, dT ** 2 / 2, 0], 36 | [0, dT ** 3 / 3, 0, dT ** 2 / 2], 37 | [dT ** 2 / 2, 0, dT, 0], 38 | [0, dT ** 2 / 2, 0, dT]]) * self.q 39 | x = F @ xprev 40 | P = F @ Pprev @ F.T + Q 41 | 42 | return (x, P) 43 | 44 | 45 | def position_measurement(x): 46 | """Velocity measurement model.""" 47 | H = np.array([[1, 0, 0, 0], 48 | [0, 1, 0, 0]]) 49 | return (H @ x, H) 50 | 51 | 52 | def velocity_measurement(x): 53 | """Velocity measurement model.""" 54 | H = np.array([[0, 0, 1, 0], 55 | [0, 0, 0, 1]]) 56 | return (H @ x, H) 57 | -------------------------------------------------------------------------------- /mht/hypgen.py: -------------------------------------------------------------------------------- 1 | """Hypothesis generation.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import queue 19 | from copy import copy 20 | import murty as murty_ 21 | 22 | from .utils import LARGE 23 | 24 | 25 | def permgen(lists, presorted=False): 26 | """Generate ordered permutations of lists of (cost, data) tuples.""" 27 | if not presorted: 28 | lists = [sorted(l) for l in lists] 29 | bounds = [len(l) - 1 for l in lists] 30 | N = len(lists) 31 | Q = queue.PriorityQueue() 32 | Q.put((0, [0] * N)) 33 | prev_cost = 1 34 | prev_states = [] 35 | while not Q.empty(): 36 | cost, state = Q.get_nowait() 37 | if cost == prev_cost: 38 | if state in prev_states: 39 | continue 40 | else: 41 | prev_states = [] 42 | prev_states.append(state) 43 | prev_cost = cost 44 | for n in range(N): 45 | if state[n] < bounds[n]: 46 | nstate = copy(state) 47 | nstate[n] += 1 48 | ncost = sum(l[nstate[i]][0] for i, l in enumerate(lists)) 49 | Q.put((ncost, nstate)) 50 | yield ([l[state[n]][1] for n, l in enumerate(lists)], 51 | None if Q.empty() else Q.queue[0][0]) 52 | 53 | 54 | def murty(C): 55 | """Algorithm due to Murty.""" 56 | mgen = murty_.Murty(C) 57 | while True: 58 | ok, cost, sol = mgen.draw() 59 | if not ok: 60 | return None 61 | yield cost, sol 62 | -------------------------------------------------------------------------------- /mht/sensors.py: -------------------------------------------------------------------------------- 1 | """File for sensor-related stuff.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from math import exp, log 19 | 20 | from .utils import LARGE, within 21 | 22 | 23 | class Sensor: 24 | """Sensor base class.""" 25 | 26 | def __init__(self, score_extraneous, score_miss): 27 | """Init.""" 28 | self.score_extraneous = score_extraneous 29 | self.score_miss = score_miss 30 | self.score_found = -log(1 - exp(-score_miss)) \ 31 | if score_miss > 0 else LARGE 32 | 33 | self._id = Sensor._counter 34 | Sensor._counter += 1 35 | Sensor._counter = 0 36 | 37 | 38 | class EyeOfMordor(Sensor): 39 | """Ideal sensor that sees all.""" 40 | 41 | def __init__(self, score_extraneous, score_miss): 42 | """Init.""" 43 | super(EyeOfMordor, self).__init__(score_extraneous, score_miss) 44 | 45 | def bbox(self): 46 | """Return FOV bbox.""" 47 | return (-LARGE, LARGE, -LARGE, LARGE) 48 | 49 | def in_fov(self, state): 50 | """Return nll prob of detection, given fov.""" 51 | return True 52 | 53 | 54 | class Satellite(Sensor): 55 | """Satellite sensor with field-of-view.""" 56 | 57 | def __init__(self, fov, score_extraneous, score_miss): 58 | """Init.""" 59 | super(Satellite, self).__init__(score_extraneous, score_miss) 60 | self.fov = fov 61 | 62 | def bbox(self): 63 | """Return FOV bbox.""" 64 | return self.fov 65 | 66 | def in_fov(self, state): 67 | """Return nll prob of detection, given fov.""" 68 | return within(state, self.fov) 69 | -------------------------------------------------------------------------------- /tests/test_track.py: -------------------------------------------------------------------------------- 1 | """Test Track methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | from unittest.mock import MagicMock 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | from mht.track import Track 25 | 26 | 27 | class TestTrack(unittest.TestCase): 28 | """Track method tests.""" 29 | 30 | def setUp(self): 31 | """Set up.""" 32 | self.target = MagicMock() 33 | self.parent = MagicMock() 34 | self.filter = MagicMock() 35 | self.sensor = MagicMock() 36 | 37 | def test_new(self): 38 | """Test creation of new track.""" 39 | report = MagicMock() 40 | self.sensor.score_extraneous = 5 41 | tr = Track.new(self.target, self.filter, self.sensor, report) 42 | 43 | self.assertEqual(tr.target, self.target) 44 | self.assertIs(tr.parent_id, None) 45 | self.assertEqual(tr.filter, self.filter) 46 | self.assertEqual(tr.score(), 5) 47 | 48 | def test_extend(self): 49 | """Test extending existing track.""" 50 | self.filter.correct = MagicMock(return_value=1) 51 | root_report = MagicMock() 52 | self.sensor.score_extraneous = 10 53 | self.sensor.score_miss = 0 54 | self.sensor.score_found = 0 55 | root = Track.new(self.target, self.filter, self.sensor, root_report) 56 | root._id = 0 57 | report = MagicMock() 58 | 59 | tr = Track.extend(root, report, self.sensor) 60 | 61 | self.assertEqual(tr.target, self.target) 62 | self.assertEqual(tr.parent_id, 0) 63 | self.assertIsNot(tr.filter, self.filter) 64 | # self.assertEqual(tr.score(), 11) 65 | -------------------------------------------------------------------------------- /mht/target.py: -------------------------------------------------------------------------------- 1 | """Methods to handle MHT target.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from .track import Track 19 | 20 | 21 | class Target: 22 | """Class to represent a single MHT target.""" 23 | 24 | def __init__(self, cluster): 25 | """Init.""" 26 | self._id = self.__class__._counter 27 | self.__class__._counter += 1 28 | self.cluster = cluster 29 | self.tracks = {} 30 | self.reset() 31 | 32 | @staticmethod 33 | def initial(cluster, filter): 34 | """Create initial target.""" 35 | self = Target(cluster) 36 | self.tracks = {None: Track.initial(self, filter)} 37 | self.reset() 38 | return self 39 | 40 | @staticmethod 41 | def new(cluster, filter, report, sensor): 42 | """Create new target.""" 43 | self = Target(cluster) 44 | tr = Track.new(self, filter, sensor, report) 45 | self.tracks = {report: tr} 46 | self.new_tracks[report] = tr 47 | return self 48 | 49 | def finalize_assignment(self, new_tracks): 50 | """Finalize assigment.""" 51 | for tr in self.tracks.values(): 52 | tr.children = {r: c for r, c in tr.children.items() 53 | if c in new_tracks} 54 | self.tracks = {tr.report: tr for tr in new_tracks} 55 | self.reset() 56 | 57 | def predict(self, dT): 58 | """Move to next time step.""" 59 | for track in self.tracks.values(): 60 | track.predict(dT) 61 | 62 | def reset(self): 63 | """Reset caches etc.""" 64 | self.new_tracks = {} 65 | 66 | def __repr__(self): 67 | """String representation of object.""" 68 | return "T({})".format(self._id) 69 | Target._counter = 0 70 | -------------------------------------------------------------------------------- /plots/icetracking.py: -------------------------------------------------------------------------------- 1 | """Create grd.png plot.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import os 19 | import sys 20 | import time 21 | import numpy as np 22 | from scipy.cluster.vq import whiten, kmeans, vq 23 | import unittest 24 | 25 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 26 | import mht 27 | 28 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) 29 | GIT_DIR = os.path.dirname(os.path.dirname(THIS_DIR)) 30 | LIB_DIR = '/'.join([GIT_DIR, 'ntnu', 'courses', 31 | 'remote_sensing', 'python']) 32 | sys.path.insert(0, LIB_DIR) 33 | 34 | np.random.seed(1) 35 | 36 | from sarproject import \ 37 | image, label, plot_classes, find_point_objects, get_features 38 | 39 | 40 | def run_kmeans(img): 41 | """Run kmeans and plot result.""" 42 | features, shape = get_features(img) 43 | classified = kmeans_classify(features, shape) 44 | indices, num_objs = label(classified, shape) 45 | 46 | plot_classes(indices, num_objs) 47 | globpos = find_point_objects(img.lat, img.lon, indices, num_objs) 48 | return globpos 49 | 50 | 51 | def kmeans_classify(features, shape, label=True, fill=False): 52 | """Run the k-means algorithm.""" 53 | print("Starting kmeans") 54 | whitened = whiten(features) 55 | init = np.array((whitened.min(0), whitened.mean(0), whitened.max(0))) 56 | codebook, _ = kmeans(whitened, init) 57 | classified, _ = vq(whitened, codebook) 58 | print("Finished kmeans") 59 | return classified 60 | 61 | 62 | class TestIcetracking(unittest.TestCase): 63 | """Test icetracking.""" 64 | 65 | def setUp(self): 66 | """Set up.""" 67 | self.tracker = mht.MHT(k_max=5) 68 | 69 | def test_icetracking(self): 70 | """Icetracking.""" 71 | pos = run_kmeans(image('grd')) 72 | 73 | scan = [ 74 | mht.Report( 75 | np.matrix([[plat], [plon]]), 76 | np.eye(2) * 1e-4, 77 | mht.models.position_measurement) 78 | for plat, plon in zip(pos[0], pos[1]) 79 | ] 80 | 81 | for t in range(2): 82 | t0 = time.time() 83 | self.tracker.register_scan( 84 | mht.Scan(mht.sensors.EyeOfMordor(5, 10, 12), scan)) 85 | t1 = time.time() 86 | print(t, ': Ran tracker in', t1 - t0, 'seconds to generate', 87 | len(self.tracker.global_hypotheses), 'hypotheses') 88 | 89 | 90 | if __name__ == '__main__': 91 | unittest.main() 92 | -------------------------------------------------------------------------------- /mht/utils.py: -------------------------------------------------------------------------------- 1 | """Util methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | LARGE = 10000 19 | import numpy as np 20 | from math import sin, cos, pi, sqrt 21 | 22 | 23 | class PrioItem: 24 | """Item storable in PriorityQueue.""" 25 | 26 | def __init__(self, prio, data): 27 | """Init.""" 28 | self.prio = prio 29 | self.data = data 30 | 31 | def __lt__(self, b): 32 | """lt comparison.""" 33 | return self.prio < b.prio 34 | 35 | 36 | def anyitem(iterable): 37 | """Retrieve 'first' item from set.""" 38 | try: 39 | return next(iter(iterable)) 40 | except StopIteration: 41 | return None 42 | 43 | 44 | def connected_components(connections): 45 | """Get all connected components.""" 46 | seen = set() 47 | 48 | def component(node): 49 | nodes = {node} 50 | while nodes: 51 | node = nodes.pop() 52 | seen.add(node) 53 | nodes |= connections[node] - seen 54 | yield node 55 | for node in list(connections.keys()): 56 | if node not in seen: 57 | yield set(component(node)) 58 | 59 | 60 | def overlap(a, b): 61 | """Check if boundingboxes overlap.""" 62 | return (a[1] >= b[0] and a[0] <= b[1] and 63 | a[3] >= b[2] and a[2] <= b[3]) 64 | 65 | 66 | def overlap_pa(a, b): 67 | """Return percentage of bbox a being in b.""" 68 | intersection = max(0, min(a[1], b[1]) - max(a[0], b[0])) \ 69 | * max(0, min(a[3], b[3]) - max(a[2], b[2])) 70 | aa = (a[1] - a[0]) * (a[3] - a[2]) 71 | return intersection / aa 72 | 73 | 74 | def eigsorted(cov): 75 | """Return eigenvalues, sorted.""" 76 | vals, vecs = np.linalg.eigh(cov) 77 | order = vals.argsort()[::-1] 78 | return vals[order], vecs[:, order] 79 | 80 | 81 | def cov_ellipse(cov, nstd): 82 | """Get the covariance ellipse.""" 83 | vals, vecs = eigsorted(cov) 84 | r1, r2 = nstd * np.sqrt(vals) 85 | theta = np.degrees(np.arctan2(*vecs[:, 0][::-1])) 86 | 87 | return r1, r2, theta 88 | 89 | 90 | def gaussian_bbox(x, P, nstd=2): 91 | """Return boudningbox for gaussian.""" 92 | r1, r2, theta = cov_ellipse(P, nstd) 93 | ux = r1 * cos(theta) 94 | uy = r1 * sin(theta) 95 | vx = r2 * cos(theta + pi/2) 96 | vy = r2 * sin(theta + pi/2) 97 | 98 | dx = sqrt(ux*ux + vx*vx) 99 | dy = sqrt(uy*uy + vy*vy) 100 | 101 | return (float(x[0] - dx), 102 | float(x[0] + dx), 103 | float(x[1] - dy), 104 | float(x[1] + dy)) 105 | 106 | 107 | def within(p, bbox): 108 | """Check if point is within bbox.""" 109 | return ((bbox[0] <= p[0] <= bbox[1]) and (bbox[2] <= p[1] <= bbox[3])) 110 | -------------------------------------------------------------------------------- /mht/plot.py: -------------------------------------------------------------------------------- 1 | """Helper functions for MHT plots.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import matplotlib.colors 19 | from numpy.random import RandomState 20 | import matplotlib.pyplot as plt 21 | from matplotlib.patches import Ellipse, Rectangle 22 | 23 | from .utils import cov_ellipse 24 | 25 | CMAP = matplotlib.colors.ListedColormap(RandomState(0).rand(256, 3)) 26 | 27 | 28 | def plot_trace(trace, c=0, covellipse=True, **kwargs): 29 | """Plot single trace.""" 30 | xs = [] 31 | ys = [] 32 | for x, P in trace: 33 | pos = (float(x[0]), float(x[1])) 34 | xs.append(pos[0]) 35 | ys.append(pos[1]) 36 | if covellipse: 37 | ca = plot_cov_ellipse(P[0:2, 0:2], pos) 38 | ca.set_alpha(0.3) 39 | ca.set_facecolor(CMAP(c)) 40 | plt.plot(xs, ys, marker='*', color=CMAP(c)) 41 | 42 | 43 | def plot_hyptrace(gh, cseed=0, covellipse=True, **kwargs): 44 | """Plot hypothesis trace.""" 45 | for tr in gh.tracks: 46 | plot_trace(tr.filter.trace, tr.target._id + cseed, covellipse, 47 | **kwargs) 48 | 49 | 50 | def plot_cov_ellipse(cov, pos, nstd=2, **kwargs): 51 | """Plot confidence ellipse.""" 52 | r1, r2, theta = cov_ellipse(cov, nstd) 53 | ellip = Ellipse(xy=pos, width=2*r1, height=2*r2, angle=theta, **kwargs) 54 | 55 | plt.gca().add_artist(ellip) 56 | return ellip 57 | 58 | 59 | def plot_hypothesis(gh, cseed=0, covellipse=True, **kwargs): 60 | """Plot targets.""" 61 | options = {'edgecolors': 'k'} 62 | options.update(kwargs) 63 | 64 | for tr in gh.tracks: 65 | pos = (tr.filter.x[0], tr.filter.x[1]) 66 | plt.scatter(*pos, c=tr.target._id+cseed, cmap=CMAP, **options) 67 | if covellipse: 68 | ca = plot_cov_ellipse(tr.filter.P[0:2, 0:2], pos) 69 | ca.set_alpha(0.5) 70 | ca.set_facecolor(CMAP(tr._id + cseed)) 71 | 72 | 73 | def plot_scan(scan, covellipse=True, **kwargs): 74 | """Plot reports from scan.""" 75 | options = { 76 | 'marker': '+', 77 | 'color': 'r', 78 | 'linestyle': 'None' 79 | } 80 | options.update(kwargs) 81 | plt.plot([float(r.z[0]) for r in scan.reports], 82 | [float(r.z[1]) for r in scan.reports], **options) 83 | if covellipse: 84 | for r in scan.reports: 85 | ca = plot_cov_ellipse(r.R[0:2, 0:2], r.z[0:2]) 86 | ca.set_alpha(0.1) 87 | ca.set_facecolor(options['color']) 88 | 89 | 90 | def plot_bbox(obj, cseed=0, **kwargs): 91 | """Plot bounding box.""" 92 | id_ = getattr(obj, '_id', 0) 93 | options = { 94 | 'alpha': 0.3, 95 | 'color': CMAP(id_ + cseed) 96 | } 97 | options.update(kwargs) 98 | bbox = obj.bbox() 99 | plt.gca().add_patch(Rectangle( 100 | (bbox[0], bbox[2]), bbox[1] - bbox[0], bbox[3] - bbox[2], 101 | **options)) 102 | -------------------------------------------------------------------------------- /mht/clusterhyp.py: -------------------------------------------------------------------------------- 1 | """Implementation of the cluster-global hypothesis class.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | 19 | class ClusterHypothesis: 20 | """Class to represent a cluster hypothesis.""" 21 | 22 | def __init__(self): 23 | """Init.""" 24 | self.total_score = 0 25 | self.tracks = [] 26 | 27 | @staticmethod 28 | def initial(tracks): 29 | """Create initial hypothesis.""" 30 | self = ClusterHypothesis() 31 | self.tracks = tracks 32 | self.targets = {tr.target for tr in self.tracks} 33 | self.calculate_score() 34 | return self 35 | 36 | @staticmethod 37 | def new(phyp, assignments, sensor): 38 | """Create new hypothesis.""" 39 | self = ClusterHypothesis() 40 | self.tracks = [track.assign(report, sensor) 41 | for report, track in assignments] 42 | 43 | missed = set(phyp.tracks) - {tr for _, tr in assignments} 44 | self.tracks += [tr.missed(sensor) 45 | for tr in missed 46 | if tr.exist_score > 1] 47 | 48 | self.targets = {tr.target for tr in self.tracks} 49 | 50 | self.calculate_score() 51 | 52 | return self 53 | 54 | @staticmethod 55 | def merge(hyps): 56 | """Merge n hyps.""" 57 | self = ClusterHypothesis() 58 | self.tracks = [tr for c in hyps for tr in c.tracks] 59 | self.targets = {tr.target for tr in self.tracks} 60 | self.calculate_score() 61 | return self 62 | 63 | def split(self, split_targets): 64 | """Return a subhypothesis to cover the provided targets.""" 65 | tracks = [tr for tr in self.tracks if tr.target in split_targets] 66 | if len(tracks) == 0: 67 | return None 68 | h = ClusterHypothesis() 69 | h.tracks = tracks 70 | h.targets = {tr.target for tr in h.tracks} 71 | h.calculate_score() 72 | return h 73 | 74 | def calculate_score(self): 75 | """Calculate score.""" 76 | self.total_score = sum(tr.score() for tr in self.tracks) 77 | 78 | def score(self): 79 | """Return the total score of the hypothesis.""" 80 | return self.total_score 81 | 82 | def __eq__(self, b): 83 | """Check if self == b.""" 84 | return self.tracks == b.tracks 85 | 86 | def __hash__(self): 87 | """Return hash.""" 88 | return hash(tuple(self.tracks)) 89 | 90 | def __gt__(self, b): 91 | """Check which hypothesis is better.""" 92 | return self.score() > b.score() 93 | 94 | def __repr__(self): 95 | """Generate string representing the hypothesis.""" 96 | return """::::: Hypothesis, score {} ::::: 97 | Tracks: 98 | \t{} 99 | """.format(self.score(), 100 | "\n\t".join( 101 | str(track) 102 | for track in sorted(self.tracks, key=lambda x: x._id))) 103 | -------------------------------------------------------------------------------- /mht/kf.py: -------------------------------------------------------------------------------- 1 | """Kalman Filter implementation for MHT target.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from copy import deepcopy 19 | from math import log as ln 20 | from math import sqrt, pi 21 | from numpy.linalg import det 22 | import numpy as np 23 | from numpy.linalg import inv 24 | from scipy.linalg import block_diag 25 | import numbers 26 | 27 | from . import models 28 | from .utils import gaussian_bbox 29 | 30 | 31 | class DefaultTargetInit: 32 | """Default target initiator.""" 33 | 34 | def __init__(self, q, pv, dT=1): 35 | """Init.""" 36 | self.q = q 37 | self.pv = np.eye(2) * pv if isinstance(pv, numbers.Number) else pv 38 | self.dT = dT 39 | 40 | def __call__(self, report, parent=None): 41 | """Init new target from report.""" 42 | if parent is None: 43 | model = models.ConstantVelocityModel(self.q) 44 | x0 = np.array([report.z[0], report.z[1], 0.0, 0.0]) 45 | P0 = block_diag(report.R, self.pv) 46 | return KFilter(model, x0, P0) 47 | # elif parent.is_new(): 48 | # model = models.ConstantVelocityModel(self.q) 49 | # x0 = np.array([report.z[0], 50 | # report.z[1], 51 | # (report.z[0] - parent.filter.x[0])/self.dT, 52 | # (report.z[1] - parent.filter.x[1])/self.dT]) 53 | # P0 = block_diag(report.R, self.pv) 54 | # return KFilter(model, x0, P0) 55 | else: 56 | return deepcopy(parent.filter) 57 | 58 | 59 | class KFilter: 60 | """Kalman-filter target.""" 61 | 62 | def __init__(self, model, x0, P0): 63 | """Init.""" 64 | self.model = model 65 | self.x = x0 66 | self.P = P0 67 | self.trace = [(x0, P0)] 68 | self._calc_bbox() 69 | 70 | def __repr__(self): 71 | """Return string representation of measurement.""" 72 | return "T({}, P)".format(self.x) 73 | 74 | def predict(self, dT): 75 | """Perform motion prediction.""" 76 | new_x, new_P = self.model(self.x, self.P, dT) 77 | self.trace.append((new_x, new_P)) 78 | self.x, self.P = new_x, new_P 79 | 80 | self._calc_bbox() 81 | 82 | def correct(self, r): 83 | """Perform correction (measurement) update.""" 84 | zhat, H = r.mfn(self.x) 85 | dz = r.z - zhat 86 | S = H @ self.P @ H.T + r.R 87 | SI = inv(S) 88 | K = self.P @ H.T @ SI 89 | self.x += K @ dz 90 | self.P -= K @ H @ self.P 91 | 92 | score = dz.T @ SI @ dz / 2.0 + ln(2 * pi * sqrt(det(S))) 93 | 94 | self._calc_bbox() 95 | 96 | return float(score) 97 | 98 | def nll(self, r): 99 | """Get the nll score of assigning a measurement to the filter.""" 100 | zhat, H = r.mfn(self.x) 101 | dz = r.z - zhat 102 | S = H @ self.P @ H.T + r.R 103 | score = dz.T @ inv(S) @ dz / 2.0 + ln(2 * pi * sqrt(det(S))) 104 | return float(score) 105 | 106 | def _calc_bbox(self, nstd=2): 107 | """Calculate minimal bounding box approximation.""" 108 | self._bbox = gaussian_bbox(self.x[0:2], self.P[0:2, 0:2]) 109 | 110 | def bbox(self): 111 | """Get minimal bounding box approximation.""" 112 | return self._bbox 113 | -------------------------------------------------------------------------------- /tests/test_mht.py: -------------------------------------------------------------------------------- 1 | """Test MHT methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import numpy as np 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | import mht 25 | 26 | 27 | class TestMHT(unittest.TestCase): 28 | """Test the generation of global hypotheses.""" 29 | 30 | def setUp(self): 31 | """Set up testcase.""" 32 | self.tracker = mht.MHT() 33 | self.tracker.initiate_clusters([ 34 | mht.kf.KFilter( 35 | mht.models.ConstantVelocityModel(0.1), 36 | np.array([0.0, 0.0, 1.0, 1.0]), 37 | np.eye(4) 38 | ), 39 | mht.kf.KFilter( 40 | mht.models.ConstantVelocityModel(0.1), 41 | np.array([0.0, 10.0, 1.0, -1.0]), 42 | np.eye(4) 43 | ) 44 | ]) 45 | 46 | def test_register_scan(self): 47 | """Test the generation of global hypotheses.""" 48 | self.tracker.register_scan(mht.Scan( 49 | mht.sensors.EyeOfMordor(3, 12), 50 | [ 51 | mht.Report( 52 | np.array([8.0, 8.0]), 53 | np.eye(2), 54 | mht.models.position_measurement), 55 | mht.Report( 56 | np.array([2.0, 2.0]), 57 | np.eye(2), 58 | mht.models.position_measurement) 59 | ])) 60 | 61 | def test_predict(self): 62 | """Test prediction.""" 63 | # self.assertEqual(len(list(self.tracker.targets())), 2) 64 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[0].filter.x[0], 0) # noqa 65 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[0].filter.x[1], 0) # noqa 66 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[1].filter.x[0], 0) # noqa 67 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[1].filter.x[1], 10) # noqa 68 | 69 | self.tracker.predict(1) 70 | 71 | # self.assertEqual(len(list(self.tracker.targets())), 2) 72 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[0].filter.x[0], 1) # noqa 73 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[0].filter.x[1], 1) # noqa 74 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[1].filter.x[0], 1) # noqa 75 | # self.assertAlmostEqual(list(self.tracker.global_hypotheses())[0].tracks[1].filter.x[1], 9) # noqa 76 | 77 | def test_track(self): 78 | """Test repeated updates from moving targets.""" 79 | targets = [ 80 | np.array([0.0, 0.0, 1.0, 1.0]), 81 | np.array([0.0, 10.0, 1.0, -1.0]) 82 | ] 83 | for t in range(3): 84 | if t > 0: 85 | self.tracker.predict(1) 86 | for t in targets: 87 | t[0:2] += t[2:] 88 | 89 | reports = {mht.Report( 90 | np.random.multivariate_normal(t[0:2], np.diag([0.1, 0.1])), # noqa 91 | # t[0:2], 92 | np.eye(2) * 0.001, 93 | mht.models.position_measurement, 94 | i) 95 | for i, t in enumerate(targets)} 96 | self.tracker.register_scan(mht.Scan( 97 | mht.sensors.EyeOfMordor(3, 12), reports)) 98 | 99 | 100 | if __name__ == '__main__': 101 | unittest.main() 102 | -------------------------------------------------------------------------------- /tests/test_hypgen.py: -------------------------------------------------------------------------------- 1 | """Hypgen tests.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | import numpy as np 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | from mht.hypgen import murty, permgen 25 | 26 | MURTY_COST = np.matrix([[7, 51, 52, 87, 38, 60, 74, 66, 0, 20], 27 | [50, 12, 0, 64, 8, 53, 0, 46, 76, 42], 28 | [27, 77, 0, 18, 22, 48, 44, 13, 0, 57], 29 | [62, 0, 3, 8, 5, 6, 14, 0, 26, 39], 30 | [0, 97, 0, 5, 13, 0, 41, 31, 62, 48], 31 | [79, 68, 0, 0, 15, 12, 17, 47, 35, 43], 32 | [76, 99, 48, 27, 34, 0, 0, 0, 28, 0], 33 | [0, 20, 9, 27, 46, 15, 84, 19, 3, 24], 34 | [56, 10, 45, 39, 0, 93, 67, 79, 19, 38], 35 | [27, 0, 39, 53, 46, 24, 69, 46, 23, 1]]) 36 | 37 | 38 | # class TestLap(unittest.TestCase): 39 | # """Test LAP solver.""" 40 | 41 | # def test_lap(self): 42 | # """Test LAP solver.""" 43 | # res = lap(MURTY_COST) 44 | # self.assertAlmostEqual( 45 | # MURTY_COST[range(len(res[1])), res[1]].sum(), 46 | # res[0]) 47 | 48 | 49 | class TestMurty(unittest.TestCase): 50 | """Test Murty algorithm.""" 51 | 52 | def test_murty(self): 53 | """Test murty algo.""" 54 | pre_res = None 55 | n = 0 56 | for res in murty(MURTY_COST): 57 | print('res:', res, MURTY_COST[range(len(res[1])), res[1]].sum()) 58 | self.assertAlmostEqual( 59 | MURTY_COST[range(len(res[1])), res[1]].sum(), 60 | res[0]) 61 | if pre_res is not None: 62 | self.assertGreaterEqual(res[0], pre_res[0]) 63 | pre_res = res 64 | n += 1 65 | self.assertEqual(n, 3628800) 66 | 67 | def test_murty_asym(self): 68 | """Test asymmetric inputs for murty.""" 69 | pre_res = None 70 | n = 0 71 | # print(MURTY_COST[:5, :]) 72 | for res in murty(MURTY_COST[:5, :]): 73 | # print(res) 74 | self.assertAlmostEqual( 75 | MURTY_COST[range(len(res[1])), res[1]].sum(), res[0]) 76 | if pre_res is not None: 77 | self.assertGreaterEqual(res[0], pre_res[0]) 78 | pre_res = res 79 | n += 1 80 | self.assertEqual(n, 30240) 81 | 82 | def test_murty_asym_small(self): 83 | """Test asymmetric inputs for murty.""" 84 | pre_res = None 85 | n = 0 86 | # print(MURTY_COST[:2, :]) 87 | for res in murty(MURTY_COST[:2, :]): 88 | # print(res) 89 | self.assertAlmostEqual( 90 | MURTY_COST[range(len(res[1])), res[1]].sum(), res[0]) 91 | if pre_res is not None: 92 | self.assertGreaterEqual(res[0], pre_res[0]) 93 | pre_res = res 94 | n += 1 95 | self.assertEqual(n, 90) 96 | 97 | 98 | class TestPermgen(unittest.TestCase): 99 | """Test permutation generation.""" 100 | 101 | def test_permgen(self): 102 | """Test permgen function.""" 103 | D = [[(1, 'a'), (1, 'b'), (2, 'c')], 104 | [(1, 'd'), (2, 'e'), (3, 'f')], 105 | [(3, 'g')]] 106 | k = 0 107 | for res in permgen(D): 108 | k += 1 109 | self.assertEqual(k, 9) 110 | 111 | 112 | if __name__ == '__main__': 113 | unittest.main() 114 | -------------------------------------------------------------------------------- /tests/test_clusterhyp.py: -------------------------------------------------------------------------------- 1 | """Test cluster hypothesis methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | from unittest.mock import MagicMock 20 | import os 21 | import sys 22 | 23 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 24 | from mht.clusterhyp import ClusterHypothesis 25 | 26 | 27 | class TestClusterHypothesis(unittest.TestCase): 28 | """Test cluster hypothesis methods.""" 29 | 30 | def setUp(self): 31 | """Set up.""" 32 | self.tracker = MagicMock() 33 | self.tracker.k_max = 10 34 | self.reports = [MagicMock(), MagicMock(), MagicMock()] 35 | self.tracks = [MagicMock(), MagicMock(), MagicMock()] 36 | self.new_tracks = [MagicMock(), MagicMock(), MagicMock()] 37 | self.filters = [MagicMock(), MagicMock(), MagicMock()] 38 | self.targets = [MagicMock(), MagicMock(), MagicMock()] 39 | self.clusters = [MagicMock(), MagicMock(), MagicMock()] 40 | self.hyps = [MagicMock(), MagicMock(), MagicMock()] 41 | for i in range(len(self.tracks)): 42 | self.tracks[i].children = {self.reports[i]: self.new_tracks[i]} 43 | self.tracks[i].filter = [self.filters[i]] 44 | self.tracks[i].score.return_value = i + 2 45 | self.tracks[i].assign.return_value = self.new_tracks[i] 46 | self.new_tracks[i].parent = self.tracks[i] 47 | self.new_tracks[i].score.return_value = 2 * i + 2 48 | self.targets[i].tracks = {None: self.tracks[i]} 49 | self.tracks[i].target = self.targets[i] 50 | self.clusters[i].targets = [self.targets[i]] 51 | self.hyps[i].tracks = [self.tracks[i]] 52 | self.hyps[i].targets = [self.targets[i]] 53 | self.sensor = MagicMock() 54 | self.sensor.score_extraneous = 3 55 | self.sensor.score_miss = 3 56 | self.sensor.score_found = 0.05 57 | 58 | def test_initial(self): 59 | """Test initial.""" 60 | chyp = ClusterHypothesis.initial(self.tracks) 61 | 62 | self.assertSetEqual(set(chyp.targets), set(self.targets)) 63 | self.assertEqual(len(chyp.targets), len(self.targets)) 64 | self.assertEqual(chyp.tracks, self.tracks) 65 | self.assertEqual(chyp.score(), 9) 66 | 67 | def test_new(self): 68 | """Test the creation of new hypotheses.""" 69 | assignments = list(zip(self.reports, self.tracks)) 70 | ph = MagicMock() 71 | chyp = ClusterHypothesis.new(ph, assignments, self.sensor) 72 | 73 | self.assertEqual(len(chyp.tracks), len(assignments)) 74 | self.assertEqual(len(chyp.targets), len(assignments)) 75 | self.assertEqual(chyp.score(), 12) 76 | 77 | def test_split(self): 78 | """Test hypothesis splitting.""" 79 | chyp = ClusterHypothesis.initial(self.tracks) 80 | 81 | split_hyp = chyp.split({self.targets[0]}) 82 | 83 | self.assertEqual(len(split_hyp.tracks), 1) 84 | self.assertEqual(split_hyp.tracks, self.tracks[0:1]) 85 | self.assertIs(split_hyp.tracks[0].target, self.targets[0]) 86 | self.assertEqual(split_hyp.score(), 2) 87 | 88 | def test_merge(self): 89 | """Test hypothesis merging.""" 90 | chyps = [ClusterHypothesis.initial([tr]) for tr in self.tracks] 91 | 92 | merged_hyp = ClusterHypothesis.merge(chyps) 93 | 94 | self.assertSetEqual(set(merged_hyp.tracks), set(self.tracks)) 95 | self.assertEqual(len(merged_hyp.tracks), len(self.tracks)) 96 | self.assertSetEqual({tr.target 97 | for tr in merged_hyp.tracks}, set(self.targets)) 98 | self.assertSetEqual(set(merged_hyp.targets), set(self.targets)) 99 | self.assertEqual(merged_hyp.score(), 9) 100 | -------------------------------------------------------------------------------- /plots/crosstrack.png.py: -------------------------------------------------------------------------------- 1 | """Create crosstrack.png plot.""" 2 | 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 3 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, see . 16 | """ 17 | import os 18 | import sys 19 | import argparse 20 | import numpy as np 21 | import matplotlib.pyplot as plt 22 | # import cProfile 23 | 24 | sys.path.append( 25 | os.path.dirname(os.path.dirname( 26 | os.path.abspath(__file__)))) 27 | import mht 28 | 29 | 30 | np.random.seed(1) 31 | 32 | 33 | def draw(): 34 | """Create plot.""" 35 | tracker = mht.MHT( 36 | cparams=mht.ClusterParameters(k_max=100, nll_limit=4, hp_limit=5), 37 | matching_algorithm="naive") 38 | targets = [ 39 | np.array([0.0, 0.0, 1.0, 1.0]), 40 | np.array([0.0, 10.0, 1.0, -1.0]), 41 | ] 42 | hyps = None 43 | nclusters = [] 44 | ntargets_true = [] 45 | ntargets = [] 46 | nhyps = [] 47 | for k in range(25): 48 | if k > 0: 49 | tracker.predict(1) 50 | for t in targets: 51 | t[0:2] += t[2:] 52 | if k == 5: 53 | targets.append(np.array([5.0, 5.0, 1.0, 0.0])) 54 | if k % 7 == 0: 55 | targets.append(np.random.multivariate_normal( 56 | np.array([k, 7.0, 0.0, 0.0]), 57 | np.diag([0.5] * 4))) 58 | if k % 7 == 1: 59 | del targets[-1] 60 | if k == 10: 61 | targets.append(np.array([10.0, -30.0, 1.0, -0.5])) 62 | if k == 20: 63 | targets.append(np.array([k, 0.0, 1.0, 4.0])) 64 | 65 | reports = {mht.Report( 66 | np.random.multivariate_normal(t[0:2], np.diag([0.1, 0.1])), # noqa 67 | # t[0:2], 68 | np.eye(2) * 0.001, 69 | mht.models.position_measurement, 70 | i) 71 | for i, t in enumerate(targets)} 72 | this_scan = mht.Scan(mht.sensors.EyeOfMordor(10, 3), reports) 73 | tracker.register_scan(this_scan) 74 | hyps = list(tracker.global_hypotheses()) 75 | nclusters.append(len(tracker.active_clusters)) 76 | ntargets.append(len(hyps[0].targets)) 77 | ntargets_true.append(len(targets)) 78 | nhyps.append(len(hyps)) 79 | mht.plot.plot_scan(this_scan) 80 | plt.plot([t[0] for t in targets], 81 | [t[1] for t in targets], 82 | marker='D', color='y', alpha=.5, linestyle='None') 83 | mht.plot.plot_hyptrace(hyps[0], covellipse=True) 84 | mht.plot.plt.axis([-1, k + 1, -k - 1, k + 1 + 10]) 85 | mht.plot.plt.ylabel('Tracks') 86 | mht.plot.plt.figure() 87 | mht.plot.plt.subplot(3, 1, 1) 88 | mht.plot.plt.plot(nclusters) 89 | mht.plot.plt.axis([-1, k + 1, min(nclusters) - 0.1, max(nclusters) + 0.1]) 90 | mht.plot.plt.ylabel('# Clusters') 91 | mht.plot.plt.subplot(3, 1, 2) 92 | mht.plot.plt.plot(ntargets, label='Estimate') 93 | mht.plot.plt.plot(ntargets_true, label='True') 94 | mht.plot.plt.ylabel('# Targets') 95 | mht.plot.plt.legend() 96 | mht.plot.plt.axis([-1, k + 1, min(ntargets + ntargets_true) - 0.1, 97 | max(ntargets + ntargets_true) + 0.1]) 98 | mht.plot.plt.subplot(3, 1, 3) 99 | mht.plot.plt.plot(nhyps) 100 | mht.plot.plt.axis([-1, k + 1, min(nhyps) - 0.1, max(nhyps) + 0.1]) 101 | mht.plot.plt.ylabel('# Hyps') 102 | 103 | 104 | def parse_args(*argv): 105 | """Parse args.""" 106 | parser = argparse.ArgumentParser() 107 | parser.add_argument('--show', action="store_true") 108 | return parser.parse_args(argv) 109 | 110 | 111 | def main(*argv): 112 | """Main.""" 113 | args = parse_args(*argv) 114 | # cProfile.run('draw()', sort='tottime') 115 | draw() 116 | if args.show: 117 | plt.show() 118 | else: 119 | plt.gcf().savefig(os.path.splitext(os.path.basename(__file__))[0], 120 | bbox_inches='tight') 121 | 122 | 123 | if __name__ == '__main__': 124 | main(*sys.argv[1:]) 125 | -------------------------------------------------------------------------------- /tests/test_cluster.py: -------------------------------------------------------------------------------- 1 | """Test cluster methods.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import unittest 19 | from unittest.mock import MagicMock, call 20 | from unittest.mock import patch 21 | import numpy as np 22 | import os 23 | import sys 24 | 25 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 26 | import mht 27 | from mht.cluster import Cluster 28 | 29 | 30 | class TestClusterInit(unittest.TestCase): 31 | """Test cluster class methods.""" 32 | 33 | @patch('mht.cluster.Target') 34 | @patch('mht.cluster.ClusterHypothesis') 35 | def test_initial(self, chmock, tmock): 36 | """Test init.""" 37 | initer = MagicMock() 38 | targets = [MagicMock(), MagicMock()] 39 | tracks = [MagicMock(), MagicMock()] 40 | filters = [MagicMock(), MagicMock()] 41 | for i, t in enumerate(targets): 42 | t.tracks = {None: tracks[i]} 43 | hyp = MagicMock() 44 | chmock.initial = MagicMock(return_value=hyp) 45 | tmock.initial = MagicMock(side_effect=targets) 46 | 47 | cluster = Cluster.initial(initer, filters) 48 | 49 | self.assertEqual(len(cluster.hypotheses), 1) 50 | tmock.initial.assert_has_calls([call(cluster, f) for f in filters]) 51 | chmock.initial.assert_called_once_with(tracks) 52 | self.assertEqual(initer.call_count, 1) 53 | 54 | 55 | class TestClustering(unittest.TestCase): 56 | """Test clustering algorithms.""" 57 | 58 | def setUp(self): 59 | """Set up.""" 60 | self.initer = MagicMock() 61 | self.tracks = [MagicMock(), MagicMock(), MagicMock()] 62 | self.filters = [MagicMock(), MagicMock(), MagicMock()] 63 | for i, tr in enumerate(self.tracks): 64 | tr.filter = [self.filters[i]] 65 | tr.score.return_value = i + 2 66 | self.targets = [MagicMock(), MagicMock(), MagicMock()] 67 | for i, t in enumerate(self.targets): 68 | t.tracks = {None: self.tracks[i]} 69 | self.tracks[i].target = t 70 | self.clusters = [MagicMock(), MagicMock(), MagicMock()] 71 | for i, c in enumerate(self.clusters): 72 | c.targets = [self.targets[i]] 73 | self.hyps = [MagicMock(), MagicMock(), MagicMock()] 74 | for i, h in enumerate(self.hyps): 75 | h.tracks = [self.tracks[i]] 76 | h.targets = [self.targets[i]] 77 | 78 | @patch('mht.cluster.permgen') 79 | @patch('mht.cluster.ClusterHypothesis') 80 | def test_cluster_merging(self, chmock, permgen): 81 | """Test cluster merging.""" 82 | merged_hyp = MagicMock() 83 | merged_hyp.targets = self.targets 84 | chmock.merge = MagicMock(return_value=merged_hyp) 85 | permgen.return_value = [(self.hyps, None)] 86 | 87 | merged_cluster = Cluster.merge(self.initer, self.clusters) 88 | 89 | self.assertEqual(set(self.targets), set(merged_cluster.targets)) 90 | chmock.merge.assert_called_once_with(self.hyps) 91 | self.assertEqual(len(merged_cluster.hypotheses), 1) 92 | self.assertEqual(self.initer.call_count, 1) 93 | 94 | def test_cluster_merged_targets(self): 95 | """Test cluster merging.""" 96 | tracker = mht.MHT() 97 | tracker.initiate_clusters([ 98 | mht.kf.KFilter( 99 | mht.models.ConstantVelocityModel(0.1), 100 | np.array([0.0, 0.0, 1.0, 1.0]), 101 | np.eye(4) 102 | ), 103 | mht.kf.KFilter( 104 | mht.models.ConstantVelocityModel(0.1), 105 | np.array([0.0, 10.0, 1.0, -1.0]), 106 | np.eye(4) 107 | ), 108 | ]) 109 | tracker._load_clusters() 110 | self.assertEqual(len(tracker.active_clusters), 2) 111 | 112 | merged_cluster = Cluster.merge(self.initer, tracker.active_clusters) 113 | 114 | self.assertEqual(len(merged_cluster.targets), 2) 115 | self.assertEqual(self.initer.call_count, 1) 116 | 117 | @patch('mht.cluster.Target') 118 | @patch('mht.cluster.ClusterHypothesis') 119 | def test_cluster_splitting(self, chmock, tmock): 120 | """Test cluster splitting.""" 121 | tmock.initial = MagicMock(side_effect=self.targets) 122 | chmock.initial = MagicMock(side_effect=self.hyps) 123 | merged_cluster = Cluster.initial(self.initer, self.filters) 124 | merged_cluster.ambiguous_tracks = [set(self.tracks[0:2])] 125 | 126 | split_clusters = merged_cluster.split(self.initer) 127 | 128 | self.assertEqual(len(split_clusters), 2) 129 | for c in split_clusters: 130 | self.assertEqual(len(c.hypotheses), 1) 131 | self.assertEqual(self.initer.call_count, 3) 132 | -------------------------------------------------------------------------------- /murty/lap.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jonatan Olofsson 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace lap { 7 | 8 | using Assignment = Eigen::Matrix; 9 | using Slack = Eigen::Matrix; 10 | static const int inf = 300000; 11 | 12 | template // NOLINT 13 | void lap(const CostMatrix& C, Assignment& x, RowSlack& u, ColSlack& v) { 14 | int N = C.rows(); 15 | int M = C.cols(); 16 | int cnt, H, f, f0, i, i1, j, j1, j2 = 0, k, last = 0, low, min = 0, up; 17 | double h, u1, u2; 18 | Eigen::RowVectorXi free(N), col(M), y(M), pred(M); 19 | Eigen::RowVectorXd d(M); 20 | 21 | x.setConstant(-1); 22 | y.setConstant(-1); 23 | v.setZero(); 24 | u.setZero(); 25 | 26 | for (i = 0; i < N; ++i) { free[i] = i; } 27 | for (j = 0; j < M; ++j) { col[j] = j; } 28 | 29 | f = N; // Each row is still unassigned 30 | 31 | for (cnt = 0; cnt < 2; ++cnt) { 32 | k = 0; 33 | f0 = f; 34 | f = 0; 35 | while (k < f0) { 36 | i = free[k++]; 37 | u1 = C(i, 0) - v[0]; 38 | j1 = 0; 39 | u2 = inf; 40 | for (j = 1; j < M; ++j) { 41 | h = C(i, j) - v[j]; 42 | if (h < u2) { 43 | if (h >= u1) { 44 | u2 = h; 45 | j2 = j; 46 | } else { 47 | u2 = u1; 48 | u1 = h; 49 | j2 = j1; 50 | j1 = j; 51 | } 52 | } 53 | } 54 | i1 = y[j1]; 55 | if (u1 < u2) { 56 | v[j1] = v[j1] - u2 + u1; 57 | } else if (i1 >= 0) { 58 | j1 = j2; 59 | i1 = y[j1]; 60 | } 61 | if (i1 >= 0) { 62 | if (u1 < u2) { 63 | free[--k] = i1; 64 | x[i1] = -1; 65 | } else { 66 | free[f++] = i1; 67 | x[i1] = -1; 68 | } 69 | } 70 | x[i] = j1; 71 | y[j1] = i; 72 | } 73 | } 74 | 75 | // -------------------------------------------------------- 76 | // Augmentation: 77 | // 78 | 79 | f0 = f; 80 | for (f = 0; f < f0; ++f) { // Find augmenting path for each unassigned row 81 | i1 = free[f]; 82 | low = 0; 83 | up = 0; 84 | for (j = 0; j < M; ++j) { 85 | d[j] = C(i1, j) - v[j]; 86 | pred[j] = i1; 87 | } 88 | while (true) { 89 | if (up == low) { // Find columns with new value for minimum d 90 | last = low - 1; 91 | min = d[col[up]]; 92 | up = up + 1; 93 | for (k = up; k < M; ++k) { 94 | j = col[k]; 95 | h = d[j]; 96 | if (h <= min) { 97 | if (h < min) { 98 | up = low; 99 | min = h; 100 | } 101 | col[k] = col[up]; 102 | col[up] = j; 103 | up = up + 1; 104 | } 105 | } 106 | for (H = low; H < up; ++H) { 107 | j = col[H]; 108 | if (y[j] == -1) { 109 | goto augment; 110 | } 111 | } 112 | } //{ up=low } 113 | j1 = col[low]; 114 | low = low + 1; 115 | i = y[j1]; 116 | u1 = C(i, j1) - v[j1] - min; 117 | for (k = up; k < M; ++k) { 118 | j = col[k]; 119 | h = C(i, j) - v[j] - u1; 120 | if (h < d[j]) { 121 | d[j] = h; 122 | pred[j] = i; 123 | if (h == min) { 124 | if (y[j] == -1) { 125 | goto augment; 126 | } else { 127 | col[k] = col[up]; 128 | col[up] = j; 129 | up = up + 1; 130 | } 131 | } 132 | } 133 | } // for k 134 | } 135 | 136 | augment: 137 | for (k = 0; k < last; ++k) { // Updating of column prices 138 | j1 = col[k]; 139 | v[j1] = v[j1] + d[j1] - min; 140 | } 141 | do { // Augmentation 142 | i = pred[j]; 143 | y[j] = i; 144 | k = j; 145 | j = x[i]; 146 | x[i] = k; 147 | } while (i != i1); 148 | } // { for f } 149 | 150 | for (i = 0; i < N; ++i) { 151 | j = x[i]; 152 | u[i] = C(i, j) - v[j]; 153 | } 154 | } 155 | 156 | 157 | template 158 | Assignment lap(const CostMatrix& C) { 159 | Slack u(C.rows()); 160 | Slack v(C.cols()); 161 | Assignment x(C.rows()); 162 | lap(C, x, u, v); 163 | return x; 164 | } 165 | 166 | template 167 | inline double cost(CostMatrix& C, Assignment res) { 168 | double c = 0; 169 | for (unsigned i = 0; i < res.rows(); ++i) { 170 | c += C(i, res[i]); 171 | } 172 | return c; 173 | } 174 | 175 | } // namespace lap 176 | -------------------------------------------------------------------------------- /mht/track.py: -------------------------------------------------------------------------------- 1 | """Track class.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from copy import deepcopy 19 | from math import exp, log 20 | 21 | from .utils import LARGE, overlap, overlap_pa 22 | 23 | NEW_EXIST_SCORE = 1 24 | MAX_EXIST_SCORE = 4 25 | 26 | 27 | class Track: 28 | """Class to represent the tracks in a target tree.""" 29 | 30 | def __init__(self, target, parent, filter, report): 31 | """Init.""" 32 | self.target = target 33 | self.target.new_tracks[report] = self 34 | self.parent_id = parent._id if parent else None 35 | self.filter = filter 36 | self.report = report 37 | self.my_score = 0 38 | self.children = {} 39 | self.parent_score = parent.score() if parent else 0 40 | self.exist_score = parent.exist_score if parent else 0 41 | self._trid = self.__class__._counter 42 | self._id = target._id 43 | self.__class__._counter += 1 44 | 45 | self.sources = deepcopy(parent.sources) if parent else set() 46 | if report: 47 | self.sources.add(report.source) 48 | self.trlen = (parent.trlen + 1) if parent else 1 49 | 50 | @staticmethod 51 | def initial(target, filter): 52 | """Create new track for target.""" 53 | self = Track(target, None, filter, None) 54 | self.my_score = 0 55 | self.exist_score = MAX_EXIST_SCORE 56 | return self 57 | 58 | @staticmethod 59 | def new(target, filter, sensor, report): 60 | """Create new track for target.""" 61 | self = Track(target, None, filter, report) 62 | report.assigned_tracks.add(self) 63 | self.my_score = sensor.score_extraneous 64 | self.exist_score = NEW_EXIST_SCORE 65 | return self 66 | 67 | @staticmethod 68 | def extend(parent, report, sensor): 69 | """Create child track.""" 70 | filt = parent.target.cluster.params.init_target_tracker(report, parent) 71 | score = filt.correct(report) 72 | self = Track(parent.target, parent, filt, report) 73 | self.my_score = score - sensor.score_found 74 | self.exist_score = min(parent.exist_score + 1, MAX_EXIST_SCORE) 75 | return self 76 | 77 | def missed(self, sensor): 78 | """Missed detection track.""" 79 | if None not in self.children: 80 | new = Track(self.target, self, deepcopy(self.filter), None) 81 | if sensor.in_fov(self.filter.x): 82 | new.my_score = self.miss_score(sensor) 83 | new.exist_score = max(self.exist_score - 1, 0) 84 | else: 85 | new.my_score = 0 86 | new.exist_score = self.exist_score 87 | self.children[None] = new 88 | return self.children[None] 89 | 90 | def assign(self, report, sensor): 91 | """Assign report to track.""" 92 | if report not in self.target.new_tracks: 93 | new = Track.extend(self, report, sensor) 94 | report.assigned_tracks.add(new) 95 | self.target.new_tracks[report] = new 96 | if report not in self.children: 97 | self.children[report] = self.target.new_tracks[report] 98 | return self.children[report] 99 | 100 | def is_new(self): 101 | """Return true if target is new.""" 102 | return (self.parent_id is None) 103 | 104 | def predict(self, dT): 105 | """Move to next time step.""" 106 | self.filter.predict(dT) 107 | 108 | def score(self): 109 | """Return track score.""" 110 | return self.parent_score + self.my_score 111 | 112 | def match_score(self, r, sensor): 113 | """Find the score of assigning a report to the track.""" 114 | if overlap(self.bbox(), sensor.bbox()): 115 | return self.filter.nll(r) + self.found_score(sensor) \ 116 | - self.miss_score(sensor) 117 | return LARGE 118 | return LARGE 119 | 120 | def found_score(self, sensor): 121 | """Find the score of assigning any report to the track.""" 122 | score_miss = self.miss_score(sensor) 123 | return -log(1 - exp(-score_miss)) \ 124 | if score_miss > 1e-8 else LARGE 125 | 126 | def miss_score(self, sensor): 127 | """Find the score of not assigning any report to the track.""" 128 | return sensor.score_miss \ 129 | * overlap_pa(self.filter.bbox(), sensor.bbox()) 130 | 131 | def bbox(self): 132 | """Return bbox.""" 133 | return self.filter.bbox() 134 | 135 | def __repr__(self): 136 | """Return string representation of object.""" 137 | return "Tr({}/{}/{}: {} {} {})".format( 138 | self._id, 139 | self._trid, 140 | self.parent_id if self.parent_id else "x", 141 | "[{}]".format(", ".join("{:.12f}".format(float(x)) 142 | for x in self.filter.x)), 143 | "x" if self.report is None else self.report.source, 144 | self.exist_score 145 | ) 146 | 147 | def __lt__(self, b): 148 | """Check if self < b.""" 149 | return id(self) < id(b) 150 | Track._counter = 0 151 | -------------------------------------------------------------------------------- /plots/multicluster.png.py: -------------------------------------------------------------------------------- 1 | """Create crosstrack.png plot.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import os 19 | import sys 20 | import argparse 21 | import numpy as np 22 | from functools import reduce 23 | import operator 24 | import matplotlib.pyplot as plt 25 | # import cProfile 26 | 27 | sys.path.append( 28 | os.path.dirname(os.path.dirname( 29 | os.path.abspath(__file__)))) 30 | import mht 31 | 32 | 33 | np.random.seed(2) 34 | 35 | 36 | def draw(): 37 | """Create plot.""" 38 | tracker = mht.MHT( 39 | cparams=mht.ClusterParameters(k_max=100, hp_limit=5), 40 | matching_algorithm="naive") 41 | target_centroids = [ 42 | ([0.0, 0.0, 1.0, 1.0], np.diag([1, 1, 0.3, 0.3]), 15), 43 | ([100.0, 100.0, -1.0, -1.0], np.diag([1, 1, 0.3, 0.3]), 5), # noqa 44 | ([0.0, 70.0, 1.0, 0.0], np.diag([1, 1, 0.3, 0.3]), 0), 45 | ([50.0, 50.0, 0.0, 0.0], np.diag([4, 4, 0.3, 0.3]), 0), # noqa 46 | ] 47 | clutter_centroids = [ 48 | ([0.0, 0.0, 50.0, 50.0], np.diag([70, 70, 1, 1]), 1), 49 | ] 50 | sensors = [ 51 | mht.sensors.Satellite((-10, 40, -10, 40), 3, 3), 52 | mht.sensors.Satellite((45, 120, 0, 40), 3, 3), 53 | mht.sensors.Satellite((35, 120, 45, 120), 3, 3), 54 | mht.sensors.Satellite((-10, 30, 55, 120), 3, 3), 55 | ] 56 | 57 | targets = [np.random.multivariate_normal(c[0], c[1]) 58 | for c in target_centroids for _ in range(c[2])] 59 | mlhyp = None 60 | nclusters = [] 61 | ntargets_true = [] 62 | ntargets = [] 63 | nhyps = [] 64 | for k in range(35): 65 | # print() 66 | # print() 67 | print("k:", k) 68 | if k > 0: 69 | tracker.predict(1) 70 | for t in targets: 71 | t[0:2] += t[2:] 72 | 73 | clutter = [np.random.multivariate_normal(c[0], c[1]) 74 | for c in clutter_centroids for _ in range(c[2])] 75 | reports = {mht.Report( 76 | np.random.multivariate_normal(t[0:2], np.diag([0.1, 0.1])), # noqa 77 | # t[0:2], 78 | np.eye(2) * 0.001, 79 | mht.models.position_measurement, 80 | i) 81 | for i, t in enumerate(targets)} 82 | false_reports = {mht.Report( 83 | np.random.multivariate_normal(t[0:2], np.diag([0.1, 0.1])), # noqa 84 | np.eye(2) * 0.3, 85 | mht.models.position_measurement) 86 | for t in clutter} 87 | 88 | ntt = 0 89 | for s in sensors: 90 | sr = {r for r in reports if s.in_fov(r.z[0:2])} 91 | fsr = {r for r in false_reports if s.in_fov(r.z[0:2])} 92 | ntt += len(sr) 93 | reports -= sr 94 | this_scan = mht.Scan(s, list(sr | fsr)) 95 | # mht.plot.plot_scan(this_scan) 96 | tracker.register_scan(this_scan) 97 | tracker._load_clusters() 98 | mlhyp = next(tracker.global_hypotheses()) 99 | nclusters.append(len(tracker.active_clusters)) 100 | ntargets.append(len(mlhyp.targets)) 101 | ntargets_true.append(len(targets)) 102 | nhyps.append(reduce(operator.mul, (len(c.hypotheses) 103 | for c in tracker.active_clusters))) 104 | plt.plot([t[0] for t in targets], [t[1] for t in targets], 105 | marker='D', color='y', alpha=.5, linestyle='None') 106 | 107 | plt.plot([t[0] for t in targets], [t[1] for t in targets], 108 | marker='D', color='y', alpha=.5, linestyle='None') 109 | mht.plot.plot_hyptrace(mlhyp, covellipse=False) 110 | mht.plot.plot_hypothesis(mlhyp, cseed=2) 111 | mht.plot.plt.axis([-30, 150, -30, 150]) 112 | mht.plot.plt.ylabel('Tracks') 113 | for s in sensors: 114 | mht.plot.plot_bbox(s) 115 | tracker._load_clusters() 116 | print("Clusters:", len(tracker.active_clusters)) 117 | for c in tracker.active_clusters: 118 | mht.plot.plot_bbox(c) 119 | # mht.plot.plt.figure() 120 | # mht.plot.plt.subplot(3, 1, 1) 121 | # mht.plot.plt.plot(nclusters) 122 | # mht.plot.plt.axis([-1, k + 1, min(nclusters) - 0.1, max(nclusters) + 0.1]) 123 | # mht.plot.plt.ylabel('# Clusters') 124 | # mht.plot.plt.subplot(3, 1, 2) 125 | # mht.plot.plt.plot(ntargets, label='Estimate') 126 | # mht.plot.plt.plot(ntargets_true, label='True') 127 | # mht.plot.plt.ylabel('# Targets') 128 | # mht.plot.plt.legend() 129 | # mht.plot.plt.axis([-1, k + 1, min(ntargets + ntargets_true) - 0.1, 130 | # max(ntargets + ntargets_true) + 0.1]) 131 | # mht.plot.plt.subplot(3, 1, 3) 132 | # mht.plot.plt.plot(nhyps) 133 | # mht.plot.plt.axis([-1, k + 1, min(nhyps) - 0.1, max(nhyps) + 0.1]) 134 | # mht.plot.plt.ylabel('# Hyps') 135 | 136 | # for tr in mlhyp.tracks: 137 | # print(tr.trlen, len(set(tr.sources)), tr.sources) 138 | 139 | 140 | def parse_args(*argv): 141 | """Parse args.""" 142 | parser = argparse.ArgumentParser() 143 | parser.add_argument('--show', action="store_true") 144 | return parser.parse_args(argv) 145 | 146 | 147 | def main(*argv): 148 | """Main.""" 149 | args = parse_args(*argv) 150 | # cProfile.run('draw()', sort='tottime') 151 | draw() 152 | if args.show: 153 | plt.show() 154 | else: 155 | plt.gcf().savefig(os.path.splitext(os.path.basename(__file__))[0], 156 | bbox_inches='tight') 157 | 158 | 159 | if __name__ == '__main__': 160 | main(*sys.argv[1:]) 161 | -------------------------------------------------------------------------------- /murty/murty.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jonatan Olofsson 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "lap.hpp" 14 | 15 | namespace lap { 16 | using CostMatrix = Eigen::Matrix; 17 | 18 | class EmptyQueue: public std::exception { 19 | public: 20 | virtual const char* what() const throw() { 21 | return "Queue empty."; 22 | } 23 | }; 24 | 25 | void allbut(const Eigen::MatrixXd& from, Eigen::MatrixXd& to, const unsigned row, const unsigned col) { 26 | unsigned rows = from.rows(); 27 | unsigned cols = from.cols(); 28 | 29 | to.resize(rows - 1, cols - 1); 30 | 31 | if (row > 0 && col > 0) { 32 | to.block(0, 0, row, col) = from.block(0, 0, row, col); 33 | } 34 | if (row > 0 && col < cols - 1) { 35 | to.block(0, col, row, cols - col - 1) = 36 | from.block(0, col + 1, row, cols - col - 1); 37 | } 38 | if (row < rows - 1 && col > 0) { 39 | to.block(row, 0, rows - row - 1, col) = 40 | from.block(row + 1, 0, rows - row - 1, col); 41 | } 42 | if (row < rows - 1 && col < cols - 1) { 43 | to.block(row, col, rows - row - 1, cols - col - 1) = 44 | from.block(row + 1, col + 1, rows - row - 1, cols - col - 1); 45 | } 46 | } 47 | 48 | void allbut(const Eigen::Matrix& from, Eigen::Matrix& to, const unsigned row) { 49 | unsigned rows = from.rows(); 50 | 51 | to.resize(rows - 1, 1); 52 | 53 | if (row > 0) { 54 | to.block(0, 0, row, 1) = from.block(0, 0, row, 1); 55 | } 56 | if (row < rows - 1) { 57 | to.block(row, 0, rows - row - 1, 1) = 58 | from.block(row + 1, 0, rows - row - 1, 1); 59 | } 60 | } 61 | 62 | void allbut(const Eigen::Matrix& from, Eigen::Matrix& to, const unsigned col) { 63 | unsigned cols = from.cols(); 64 | 65 | to.resize(1, cols - 1); 66 | 67 | if (col > 0) { 68 | to.block(0, 0, 1, col) = from.block(0, 0, 1, col); 69 | } 70 | if (col < cols - 1) { 71 | to.block(0, col, 1, cols - col - 1) = 72 | from.block(0, col + 1, 1, cols - col - 1); 73 | } 74 | } 75 | 76 | 77 | struct MurtyState { 78 | using Slacklist = std::vector>; 79 | Eigen::MatrixXd C; 80 | Slack u, v; 81 | double cost; 82 | double boundcost; 83 | bool solved; 84 | Assignment solution; 85 | Assignment res; 86 | std::vector rmap, cmap; 87 | 88 | explicit MurtyState(const MurtyState* s) 89 | : 90 | cost(s->cost), 91 | boundcost(s->boundcost), 92 | solved(false), 93 | solution(s->solution), 94 | rmap(s->rmap), 95 | cmap(s->cmap) {} 96 | 97 | explicit MurtyState(const Eigen::MatrixXd& C_) 98 | : C(C_), 99 | u(C_.rows()), 100 | v(C_.cols()), 101 | cost(0), 102 | boundcost(0), 103 | solved(false), 104 | solution(C_.rows()) { 105 | v.setZero(); 106 | rmap.resize(C.rows()); 107 | cmap.resize(C.cols()); 108 | for (unsigned i = 0; i < C.rows(); ++i) { rmap[i] = i; } 109 | for (unsigned j = 0; j < C.cols(); ++j) { cmap[j] = j; } 110 | } 111 | 112 | auto partition_with(const unsigned i, const unsigned j) { 113 | auto s = std::make_shared(this); 114 | 115 | allbut(C, s->C, i, j); 116 | allbut(u, s->u, i); 117 | allbut(v, s->v, j); 118 | //std::cout << " Binding (" << rmap[i] << "," << cmap[j] << ") (" << C(i, j) << ") [" << s->C.rows() << "x" << s->C.cols() << "]" << std::endl; 119 | s->rmap.erase(s->rmap.begin() + i); 120 | s->cmap.erase(s->cmap.begin() + j); 121 | s->boundcost += C(i, j); 122 | 123 | return s; 124 | } 125 | 126 | auto partition_without(const unsigned i, const unsigned j, const double slack) { 127 | auto s = std::make_shared(this); 128 | s->C = C; 129 | s->u = u; 130 | s->v = v; 131 | s->remove(i, j, slack); 132 | 133 | return s; 134 | } 135 | 136 | void remove(const unsigned i, const unsigned j, const double slack) { 137 | //std::cout << " Removing (" << rmap[i] << "," << cmap[j] << ") (" << C(i, j) << ") [" << C.rows() << "x" << C.cols() << "] raising cost " << cost << " -> "; 138 | C(i, j) = lap::inf; 139 | solved = false; 140 | cost += slack; 141 | //std::cout << cost << std::endl; 142 | //std::cout << "New C: " << std::endl << C << std::endl; 143 | } 144 | 145 | bool solve() { 146 | //std::cout << "Solving (" << cost << "): " << std::endl << C << std::endl; 147 | res.resize(C.rows()); 148 | lap::lap(C, res, u, v); 149 | //std::cout << "rmap: "; 150 | //for (auto& r : rmap) { 151 | //std::cout << r << ", "; 152 | //} 153 | //std::cout << std::endl; 154 | //std::cout << "cmap: "; 155 | //for (auto& c : cmap) { 156 | //std::cout << c << ", "; 157 | //} 158 | //std::cout << std::endl; 159 | cost = boundcost; 160 | for (unsigned i = 0; i < res.rows(); ++i) { 161 | solution[rmap[i]] = cmap[res[i]]; 162 | cost += C(i, res[i]); 163 | } 164 | //std::cout << "Solution: [" << res.transpose() << "] " << cost << std::endl; 165 | solved = true; 166 | return (cost < lap::inf); 167 | } 168 | 169 | MurtyState::Slacklist minslack() const { 170 | std::vector> mslack(C.rows()); 171 | double h; 172 | for (unsigned i = 0; i < C.rows(); ++i) { 173 | mslack[i] = {C(i, 0) - u[i] - v[0], i, res[i]}; 174 | for (unsigned j = 1; j < C.cols(); ++j) { 175 | if (static_cast(j) == res[i]) { 176 | continue; 177 | } 178 | 179 | h = C(i, j) - u[i] - v[j]; 180 | if (h < std::get<0>(mslack[i])) { 181 | mslack[i] = {h, i, res[i]}; 182 | } 183 | } 184 | } 185 | std::sort(mslack.rbegin(), mslack.rend()); 186 | return mslack; 187 | } 188 | }; 189 | 190 | using MurtyStatePtr = std::shared_ptr; 191 | struct MurtyStatePtrCompare { 192 | bool operator()(const MurtyStatePtr& a, const MurtyStatePtr& b) { 193 | if (a->cost > b->cost) { 194 | return true; 195 | } else if (a->cost == b->cost) { 196 | return a->C.rows() > b->C.rows(); 197 | } else { 198 | return false; 199 | } 200 | } 201 | }; 202 | 203 | class Murty { 204 | private: 205 | double offset; 206 | 207 | public: 208 | explicit Murty(CostMatrix C) { 209 | double min = C.minCoeff(); 210 | offset = 0; 211 | if (min < 0) { 212 | C.array() -= min; 213 | offset = min * C.rows(); 214 | } 215 | queue.emplace(std::make_shared(C)); 216 | } 217 | 218 | void get_partition_index(const MurtyState::Slacklist& partition_order, MurtyState::Slacklist::iterator p, unsigned& i, unsigned& j) { 219 | i = std::get<1>(*p); 220 | j = std::get<2>(*p); 221 | for (auto pp = partition_order.begin(); pp != p; ++pp) { 222 | if (std::get<1>(*pp) < std::get<1>(*p)) { --i; } 223 | if (std::get<2>(*pp) < std::get<2>(*p)) { --j; } 224 | } 225 | } 226 | 227 | using ReturnTuple = std::tuple; 228 | ReturnTuple draw_tuple() { 229 | Assignment sol; 230 | double cost; 231 | bool ok = draw(sol, cost); 232 | return {ok, cost, sol}; 233 | } 234 | 235 | bool draw(Assignment& sol, double& cost) { 236 | std::shared_ptr s; 237 | unsigned i, j; 238 | //std::cout << "Draw! Queue size: " << queue.size() << std::endl; 239 | 240 | if (queue.empty()) { 241 | return false; 242 | } 243 | 244 | for (s = queue.top(); !s->solved; s = queue.top()) { 245 | queue.pop(); 246 | if (s->solve()) { 247 | queue.push(s); 248 | } 249 | if (queue.empty()) { 250 | //std::cout << "Queue empty 1!" << std::endl; 251 | return false; 252 | } 253 | } 254 | queue.pop(); 255 | sol = s->solution; 256 | cost = s->cost + offset; 257 | if (cost > lap::inf) { 258 | return false; 259 | } 260 | //std::cout << "Solution: " << sol.transpose() << std::endl; 261 | //std::cout << "Cost: " << cost << std::endl; 262 | //std::cout << "res: " << s->res.transpose() << std::endl; 263 | //std::cout << "rmap: "; 264 | //for (auto& r : s->rmap) { 265 | //std::cout << r << ", "; 266 | //} 267 | //std::cout << std::endl; 268 | //std::cout << "cmap: "; 269 | //for (auto& c : s->cmap) { 270 | //std::cout << c << ", "; 271 | //} 272 | //std::cout << std::endl; 273 | //std::cout << s->C << std::endl; 274 | 275 | auto partition_order = s->minslack(); 276 | auto p = partition_order.begin(); 277 | auto node = s; 278 | 279 | for (; p != partition_order.end(); ++p) { 280 | get_partition_index(partition_order, p, i, j); 281 | queue.push(node->partition_without(i, j, std::get<0>(*p))); 282 | node = node->partition_with(i, j); 283 | } 284 | return true; 285 | } 286 | 287 | private: 288 | std::priority_queue, MurtyStatePtrCompare> queue; 289 | }; 290 | } 291 | -------------------------------------------------------------------------------- /mht/cluster.py: -------------------------------------------------------------------------------- 1 | """MHT Cluster.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | import queue 19 | from math import log, exp 20 | import numpy as np 21 | from itertools import islice 22 | from collections import defaultdict 23 | # import matplotlib.pyplot as plt 24 | # from . import plot 25 | 26 | from .target import Target 27 | from .clusterhyp import ClusterHypothesis 28 | from .hypgen import murty, permgen 29 | from .utils import PrioItem, connected_components, LARGE 30 | from .kf import DefaultTargetInit 31 | 32 | 33 | class ClusterParameters: 34 | """Cluster parmeters.""" 35 | 36 | def __init__(self, **kwargs): 37 | """Init.""" 38 | for name, value in kwargs.items(): 39 | self.__dict__[name] = value 40 | 41 | ClusterParameters.k_max = 100 42 | ClusterParameters.hp_limit = LARGE 43 | ClusterParameters.init_target_tracker = DefaultTargetInit(0.1, 0.1) 44 | 45 | 46 | class Cluster: 47 | """MHT class.""" 48 | 49 | def __init__(self, initer): 50 | """Init.""" 51 | self.targets = [] 52 | self.hypotheses = [] 53 | self.ambiguous_tracks = [] 54 | self.assigned_reports = set() 55 | self.params = None 56 | initer(self) 57 | if self.params is None: 58 | self.params = ClusterParameters() 59 | 60 | @staticmethod 61 | def initial(initer, initial_target_filters): 62 | """Create initial cluster.""" 63 | self = Cluster(initer) 64 | for f in initial_target_filters: 65 | self.targets.append(Target.initial(self, f)) 66 | self.hypotheses = [ClusterHypothesis.initial( 67 | [t.tracks[None] for t in self.targets])] 68 | return self 69 | 70 | @staticmethod 71 | def empty(initer): 72 | """Create empty cluster.""" 73 | return Cluster.initial(initer, []) 74 | 75 | @staticmethod 76 | def merge(initer, clusters): 77 | """Merge multiple clusters.""" 78 | self = Cluster(initer) 79 | 80 | # Hypotheses 81 | self.hypotheses = [ClusterHypothesis.merge(hyps[0]) 82 | for hyps in islice(permgen([[(h.score(), h) 83 | for h in c.hypotheses] 84 | for c in clusters], 85 | True), 86 | 0, self.params.k_max)] 87 | self.normalise() 88 | 89 | # Targets 90 | self.targets = list({t for h in self.hypotheses for t in h.targets}) 91 | for t in self.targets: 92 | t.cluster = self 93 | 94 | # Ambiguous tracks 95 | self.ambiguous_tracks = [ 96 | atrs for c in clusters for atrs in c.ambiguous_tracks] 97 | 98 | return self 99 | 100 | def _splitter(self, initer, split_targets): 101 | """Perform actual split.""" 102 | cl = Cluster(initer) 103 | 104 | # Targets 105 | cl.targets = split_targets 106 | 107 | # Hypotheses 108 | # Sneaky sneaky! This is where duplicate hypotheses are deleted! 109 | cl.hypotheses = sorted(list( 110 | {hyp for hyp in (h.split(split_targets) 111 | for h in self.hypotheses) if hyp})) 112 | cl.normalise() 113 | 114 | # Targets 115 | cl.targets = list({t for h in cl.hypotheses for t in h.targets}) 116 | for t in self.targets: 117 | t.cluster = cl 118 | 119 | # Ambiguous tracks 120 | cl.ambiguous_tracks = [{tr for tr in atrs 121 | if tr.target in split_targets} 122 | for atrs in self.ambiguous_tracks] 123 | cl.ambiguous_tracks = [atrs for atrs in cl.ambiguous_tracks 124 | if len({tr.target for tr in atrs}) > 1] 125 | return cl 126 | 127 | def split(self, initer): 128 | """Split cluster into multiple independent clusters.""" 129 | if len(self.hypotheses) == 0: 130 | return set() 131 | connections = defaultdict(set) 132 | for atrs in self.ambiguous_tracks: 133 | targets = {tr.target for tr in atrs} 134 | for target in targets: 135 | connections[target].update(targets) 136 | new_clusters = list(connected_components(connections)) 137 | 138 | unassigned_targets = set(self.targets).difference(*new_clusters) 139 | new_clusters += [{t} for t in unassigned_targets] 140 | if len(new_clusters) > 1: 141 | return {self._splitter(initer, c) for c in new_clusters} 142 | else: 143 | return {self} 144 | 145 | def predict(self, dT): 146 | """Move to next timestep.""" 147 | for target in self.targets: 148 | target.predict(dT) 149 | 150 | def normalise(self): 151 | """Normalise hypothesis scores.""" 152 | if len(self.hypotheses): 153 | scores = [h.score() for h in self.hypotheses] 154 | min_score = min(scores) 155 | c = log(sum(exp(min_score - s) for s in scores)) - min_score 156 | for h in self.hypotheses: 157 | h.total_score += c 158 | 159 | def register_scan(self, scan): 160 | """Register scan.""" 161 | def hlimit(g): 162 | """Limit hypothesis draws.""" 163 | min_score = LARGE 164 | scores = [] 165 | for ph, c, h in islice(g, None, self.params.k_max): 166 | scores.append(c) 167 | min_score = min(min_score, c) 168 | s = log(sum(exp(min_score - s) for s in scores)) - min_score 169 | if c + s > self.params.hp_limit: 170 | return 171 | yield ph, h 172 | 173 | new_ts = {} 174 | 175 | # Generate new hyptheses 176 | self.hypotheses = list(sorted(list({ 177 | ch for ch in 178 | (ClusterHypothesis.new(ph, hyp, scan.sensor) 179 | for ph, hyp in hlimit(self._assignment_hypotheses(scan, new_ts))) 180 | if len(ch.tracks) > 0}))) 181 | self.normalise() 182 | 183 | # Handle created targets and assignments 184 | self.targets = list({t for h in self.hypotheses for t in h.targets}) 185 | tracks = {tr for h in self.hypotheses for tr in h.tracks} 186 | for target in self.targets: 187 | target.finalize_assignment({tr for tr in tracks 188 | if tr.target is target}) 189 | 190 | # Find tracks from reports that were assigned to multiple targets 191 | self.ambiguous_tracks = [ 192 | set().union(*(tr.children.values() for tr in atrs)) & tracks 193 | for atrs in self.ambiguous_tracks] 194 | self.ambiguous_tracks = [atrs for atrs in self.ambiguous_tracks 195 | if len({tr.target for tr in atrs}) > 1] 196 | for r in scan.reports: 197 | if len({tr.target for tr in r.assigned_tracks}) > 1: 198 | self.ambiguous_tracks.append(r.assigned_tracks) 199 | 200 | def _assignment_hypotheses(self, scan, new_targets): 201 | """Generate cluster hypotheses.""" 202 | def new_target_track(report): 203 | """Create new target.""" 204 | ''' 205 | The cache is global, as the new target is 206 | independent of the parent hypothesis. 207 | ''' 208 | nonlocal new_targets 209 | if report not in new_targets: 210 | new_targets[report] = Target.new( 211 | self, 212 | self.params.init_target_tracker(report), 213 | report, scan.sensor) 214 | return new_targets[report].tracks[report] 215 | 216 | def get_murties(ph): 217 | """Get hypothesis generator for parent hypothesis.""" 218 | M = len(scan.reports) 219 | N = len(ph.tracks) # Nof targets in hypothesis 220 | 221 | miss_all_score = sum(tr.miss_score(scan.sensor) 222 | for tr in ph.tracks) 223 | 224 | if M == 0: 225 | return iter([(ph.score() + miss_all_score, iter([]))]) 226 | 227 | ''' 228 | Form C-matrix: |1 2| 229 | 1: MxN Cost of assigning measurent r to target c. 230 | 2: Diagonal MxM cost of extraneous report (new or false). 231 | All other values LARGE to be avoided by Murty algorithm. 232 | ''' 233 | C = np.empty((M, N + M)) 234 | C.fill(LARGE) 235 | for i, tr in enumerate(ph.tracks): 236 | C[range(M), i] = [tr.match_score(r, scan.sensor) 237 | for r in scan.reports] 238 | C[range(M), range(N, N + M)] = scan.sensor.score_extraneous 239 | 240 | # Murty solution S: (cost, assignments) 241 | return ((ph.score() + S[0] + miss_all_score, 242 | ((r, ph.tracks[a] if a < N else new_target_track(r)) 243 | for r, a in zip(scan.reports, S[1]))) 244 | for S in murty(C)) 245 | 246 | murties = ((ph, get_murties(ph)) for ph in self.hypotheses) 247 | 248 | ''' 249 | Algorithm description: 250 | If all parent hyps has entered the comparison, draw next 251 | parent hyp as this may be a candidate to switch to. 252 | While current parent hypothesis is cheaper, draw from it 253 | children assignments. 254 | ''' 255 | Q = queue.PriorityQueue() 256 | ph, m = next(murties) 257 | a = next(m) 258 | last_item = PrioItem(a[0], (a, ph, m)) 259 | Q.put(last_item) 260 | while not Q.empty(): 261 | item = Q.get_nowait() 262 | if item == last_item: 263 | ph, m = next(murties, (None, None)) 264 | if m: 265 | a = next(m) 266 | last_item = PrioItem(a[0], (a, ph, m)) 267 | Q.put(last_item) 268 | a, ph, m = item.data 269 | next_break = Q.queue[0].prio if not Q.empty() else LARGE 270 | while a and a[0] <= next_break: 271 | r = list(a[1]) 272 | yield ph, a[0], r 273 | a = next(m, None) 274 | if a: 275 | Q.put(PrioItem(a[0], (a, ph, m))) 276 | 277 | def bbox(self): 278 | """Get minimal boundingbox.""" 279 | # FIXME: Cache!!! 280 | bboxes = (tr.bbox() 281 | for t in self.targets for tr in t.tracks.values()) 282 | minbox = next(bboxes) 283 | for bbox in bboxes: 284 | minbox = (min(minbox[0], bbox[0]), 285 | max(minbox[1], bbox[1]), 286 | min(minbox[2], bbox[2]), 287 | max(minbox[3], bbox[3])) 288 | return minbox 289 | -------------------------------------------------------------------------------- /mht/mht.py: -------------------------------------------------------------------------------- 1 | """Library implementing Multiple Hypothesis Tracking.""" 2 | 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 3 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, see . 16 | """ 17 | 18 | from itertools import chain 19 | import sqlite3 20 | import pickle 21 | import multiprocessing as mp 22 | 23 | from .cluster import Cluster, ClusterParameters 24 | from .hypgen import permgen 25 | from .utils import overlap, gaussian_bbox 26 | 27 | 28 | def cluster_initer_factory(tracker, cparams): 29 | """Cluster initer factory.""" 30 | def inner(self): 31 | self._id = tracker._new_cluster_id() 32 | self.params = cparams 33 | return inner 34 | 35 | 36 | def predict_cluster(args): 37 | """Perform parallel time update on cluster.""" 38 | (cluster, dT) = args 39 | cluster.predict(dT) 40 | return cluster 41 | 42 | 43 | def correct_cluster(args): 44 | """Update cluster from multithread process.""" 45 | (scan, cluster) = args 46 | cluster.register_scan(scan) 47 | return cluster 48 | 49 | 50 | class MHT: 51 | """MHT class.""" 52 | 53 | def __init__(self, cparams=None, matching_algorithm=None, 54 | dbfile=':memory:'): 55 | """Init.""" 56 | self.matching_algorithm = matching_algorithm 57 | self.cparams = cparams if cparams else ClusterParameters() 58 | self.cluster_initer = cluster_initer_factory(self, self.cparams) 59 | 60 | self.active_clusters = set() 61 | 62 | self.dbfile = dbfile 63 | self.dbc = sqlite3.connect(dbfile) 64 | self.db = self.dbc.cursor() 65 | self._init_db() 66 | 67 | self.mppool = mp.Pool() 68 | self.npresplit = 0 69 | 70 | def initiate_clusters(self, initial_targets): 71 | """Init clusters.""" 72 | self._save_clusters({Cluster.initial(self.cluster_initer, [f]) 73 | for f in initial_targets}) 74 | 75 | def _reboot(self): 76 | """Reboot filter.""" 77 | self.dbc = sqlite3.connect(self.dbfile) 78 | self.db = self.dbc.cursor() 79 | self._init_db() 80 | self.active_clusters = set() 81 | 82 | def _init_db(self): 83 | """Init database.""" 84 | self.db.execute( 85 | "CREATE TABLE IF NOT EXISTS clusters (" 86 | "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," 87 | "min_x REAL," 88 | "max_x REAL," 89 | "min_y REAL," 90 | "max_y REAL," 91 | "data BLOB" 92 | ");") 93 | if self.matching_algorithm == "rtree": 94 | self.db.execute("CREATE VIRTUAL TABLE IF NOT EXISTS cluster_index" 95 | " USING rtree(id, min_x, max_x, min_y, max_y);") 96 | 97 | def _new_cluster_id(self): 98 | """Insert new row in db and retrieve id.""" 99 | self.db.execute("INSERT INTO clusters DEFAULT VALUES") 100 | return self.db.lastrowid 101 | 102 | def _add_clusters(self, clusters): 103 | """Add clusters.""" 104 | self.active_clusters |= clusters 105 | 106 | def _delete_clusters(self, clusters): 107 | """Remove clusters.""" 108 | self.active_clusters -= clusters 109 | ids = ', '.join(str(c._id) for c in clusters) 110 | self.db.execute("DELETE FROM clusters WHERE id IN ({});".format(ids)) 111 | if self.matching_algorithm == "rtree": 112 | self.db.execute("DELETE FROM cluster_index WHERE id IN ({});" 113 | .format(ids)) 114 | self.dbc.commit() 115 | 116 | def _load_clusters(self, bbox=None): 117 | """Load clusters.""" 118 | if self.matching_algorithm is None or bbox is None: 119 | self.active_clusters = self.query_clusters() 120 | elif self.matching_algorithm == "naive": 121 | all_clusters = self.query_clusters() 122 | self.active_clusters = { 123 | c for c in all_clusters 124 | if overlap(c.bbox(), bbox)} 125 | else: 126 | self.active_clusters = self.query_clusters(bbox) 127 | 128 | def query_clusters(self, bbox=None): 129 | """Get clusters intersecting boundingbox.""" 130 | if bbox is None: 131 | pickles = self.db.execute("SELECT data FROM clusters") 132 | else: 133 | def get_clusters(bbox): 134 | if self.matching_algorithm == "db": 135 | return self.db.execute(( 136 | "SELECT data FROM clusters WHERE " 137 | "max_x >= {} AND " 138 | "min_x <= {} AND " 139 | "max_y >= {} AND " 140 | "min_y <= {}" 141 | ";").format(*bbox)) 142 | elif self.matching_algorithm == "rtree": 143 | # PySQLite standard formatting doesn't work for some 144 | # reason.. bug? Using .format instead, since known data. 145 | return self.db.execute(( 146 | "SELECT clusters.data FROM clusters " 147 | "INNER JOIN cluster_index " 148 | "ON clusters.id = cluster_index.id WHERE " 149 | "cluster_index.max_x >= {} AND " 150 | "cluster_index.min_x <= {} AND " 151 | "cluster_index.max_y >= {} AND " 152 | "cluster_index.min_y <= {}" 153 | ";").format(*bbox)) 154 | 155 | # FIXME: Use multiple queries if around wrapping-points! 156 | pickles = get_clusters(bbox) 157 | return {pickle.loads(p[0]) for p in pickles} 158 | 159 | def _save_clusters(self, clusters=None): 160 | """Store cluster data in database.""" 161 | if clusters is None: 162 | clusters = self.active_clusters 163 | if self.matching_algorithm == "rtree": 164 | for c in clusters: 165 | self.db.execute(("REPLACE INTO cluster_index " 166 | "(id, min_x, max_x, min_y, max_y) " 167 | "VALUES ({}, {}, {}, {}, {});" 168 | ).format(c._id, *c.bbox())) 169 | for c in clusters: 170 | self.db.execute("UPDATE clusters SET " 171 | "min_x=?, max_x=?, min_y=?, max_y=?, data=? " 172 | "WHERE id=?", c.bbox() + (pickle.dumps(c), c._id)) 173 | self.dbc.commit() 174 | 175 | def _overlapping_clusters(self, r): 176 | """Select clusters within reasonable range.""" 177 | return {c for c in self.active_clusters 178 | if any(overlap(tr.bbox(), r.bbox()) 179 | for t in c.targets for tr in t.tracks.values())} 180 | 181 | def _split_clusters(self): 182 | """Split clusters.""" 183 | self.npresplit = len(self.active_clusters) 184 | new_clusters = set() 185 | old_clusters = set() 186 | for c in self.active_clusters: 187 | nc = c.split(self.cluster_initer) 188 | if len(nc) != 1: 189 | old_clusters.add(c) 190 | new_clusters |= nc 191 | self._delete_clusters(old_clusters) 192 | self.active_clusters = new_clusters 193 | 194 | def _merge_clusters(self, clusters): 195 | """Merge multiple clusters.""" 196 | c = Cluster.merge(self.cluster_initer, clusters) 197 | c.assigned_reports = {r for c in clusters 198 | for r in c.assigned_reports} 199 | self._delete_clusters(clusters) 200 | self._add_clusters({c}) 201 | return c 202 | 203 | def _cluster(self, scan): 204 | """Update clusters.""" 205 | new_clusters = set() 206 | self._load_clusters(scan.sensor.bbox()) 207 | 208 | for r in scan.reports: 209 | cmatches = self._overlapping_clusters(r) 210 | 211 | if len(cmatches) > 1: 212 | cluster = self._merge_clusters(cmatches) 213 | elif len(cmatches) == 0: 214 | cluster = Cluster.empty(self.cluster_initer) 215 | new_clusters.add(cluster) 216 | else: 217 | (cluster,) = cmatches 218 | 219 | cluster.assigned_reports.add(r) 220 | 221 | self.active_clusters |= new_clusters 222 | 223 | for c in self.active_clusters: 224 | a = c.assigned_reports 225 | c.assigned_reports = set() 226 | yield (c, a) 227 | 228 | def predict(self, dT, bbox=None): 229 | """Move to next timestep.""" 230 | self._load_clusters(bbox) 231 | self.active_clusters = set( 232 | self.mppool.map(predict_cluster, 233 | ((c, dT) for c in self.active_clusters))) 234 | self._save_clusters() 235 | 236 | def register_scan(self, scan): 237 | """Register new scan.""" 238 | self.active_clusters = set( 239 | self.mppool.map( 240 | correct_cluster, 241 | ((Scan(scan.sensor, cr), c) 242 | for c, cr in self._cluster(scan)))) 243 | self._split_clusters() 244 | self._save_clusters() 245 | 246 | def global_hypotheses(self, bbox=None): 247 | """Return global hypotheses.""" 248 | self._load_clusters(bbox) 249 | yield from (GlobalHypothesis(hyps) for hyps in 250 | permgen(((h.score(), h) for h in c.hypotheses) 251 | for c in self.active_clusters)) 252 | 253 | def targets(self): 254 | """Retrieve all targets in tracker.""" 255 | yield from chain.from_iterable(c.targets for c in self.active_clusters) 256 | 257 | 258 | class GlobalHypothesis: 259 | """Class to represent a global hypothesis.""" 260 | 261 | def __init__(self, hypotheses): 262 | """Init.""" 263 | self.cluster_hypotheses = hypotheses[0] 264 | self.tracks = [tr for h in self.cluster_hypotheses for tr in h.tracks] 265 | self.targets = {tr.target for tr in self.tracks} 266 | self.total_score = sum(tr.score() for tr in self.tracks) 267 | 268 | def score(self): 269 | """Return the total score of the hypothesis.""" 270 | return self.total_score 271 | 272 | def __gt__(self, b): 273 | """Check which hypothesis is better.""" 274 | return self.score() > b.score() 275 | 276 | def __repr__(self): 277 | """Generate string representing the hypothesis.""" 278 | return """::::: Global Hypothesis, score {} ::::: 279 | Tracks: 280 | \t{} 281 | """.format(self.score(), 282 | "\n\t".join(str(track) for track in self.tracks)) 283 | 284 | 285 | class Report: 286 | """Class for containing reports.""" 287 | 288 | def __init__(self, z, R, mfn, source=None, tpos=None): 289 | """Init.""" 290 | self.z = z 291 | self.R = R 292 | self.mfn = mfn 293 | self.assigned_tracks = set() 294 | self.source = source 295 | self.tpos = tpos 296 | self._bbox = gaussian_bbox(self.z[0:2], self.R[0:2, 0:2], 2) 297 | 298 | def bbox(self): 299 | """Return report bbox.""" 300 | return self._bbox 301 | 302 | def __repr__(self): 303 | """Return string representation of reports.""" 304 | return "R({}, R)".format(self.z.T) 305 | 306 | 307 | class Scan: 308 | """Report container class.""" 309 | 310 | def __init__(self, sensor, reports): 311 | """Init.""" 312 | self.sensor = sensor 313 | self.reports = reports 314 | 315 | def __repr__(self): 316 | """Return a string representation of the scan.""" 317 | return "Scan: {}".format(str(self.reports)) 318 | -------------------------------------------------------------------------------- /gpl.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------