"""
///////////////////////////////////////////////////////////////////////////////
//        Copyright (c) 2012-2020 Oscar Riveros. all rights reserved.        //
//                        oscar.riveros@peqnp.science                        //
//                                                                           //
//   without any restriction, Oscar Riveros reserved rights, patents and     //
//  commercialization of this knowledge or derived directly from this work.  //
///////////////////////////////////////////////////////////////////////////////
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import matplotlib.pyplot as plt
import numpy as np

import hess

glb = np.inf


def oracle(seq):
    global glb
    loc = sum(np.linalg.norm(data[seq[i - 1]] - data[seq[i]]) for i in range(n))
    if loc < glb:
        glb = loc
        print(glb)
    return loc


if __name__ == '__main__':
    n = 100

    data = np.random.logistic(size=(n, 2))

    opt = hess.sequence(n, oracle, fast=False)

    x, y = zip(*data[opt + [opt[0]]])

    plt.plot(x, y, 'ro')
    plt.plot(x, y, 'k-')
    plt.savefig('tsp.png')
