# -*- coding: utf-8 -*-



import pandas as pd
import numpy as np
import math

df = pd.read_excel("koordinaten.xlsx")

def getSample(df,n):
    return df.sample(n)


def getDistance(lat1,lon1,lat2,lon2):
    lat = (lat1+lat2)/2*0.01745
    dx = 111.3*math.cos(lat)*(lon1-lon2)
    dy = 111.3*(lat1-lat2)
    return math.sqrt(dx*dx+dy*dy)

def cost_function(sample,permutation):
    cost = 0
    for i in range(0, len(permutation)-1):
        lat1 = sample['Breitengrad_DEC'][sample.index[permutation[i]]]
        lon1 = sample['Laengengrad_DEC'][sample.index[permutation[i]]]
        lat2 = sample['Breitengrad_DEC'][sample.index[permutation[i+1]]]
        lon2 = sample['Laengengrad_DEC'][sample.index[permutation[i+1]]]
        cost += math.ceil(getDistance(lat1,lon1,lat2,lon2))
    return cost

def swap(liste, x, y): 
    for i in range(len(liste)):
        if(liste[i]==x):
            liste[i] = y
            continue
        if(liste[i]==y):
            liste[i] = x
            continue
    return liste
  
def add_pos_vel(p1,v):
    p2=p1.copy()
    for i in v:
        p2 = swap(p2,i[0],i[1])
    return p2

def add_vel(v1,v2):
    return v1+v2

def sub_pos(p1,p2):
    v =list()
    temp1=0
    temp2=0
    p1_copy = p1.copy()
    p2_copy = p2.copy()
    for i in range(len(p1)):
        if(p1_copy[i]!=p2_copy[i]):
            temp1=p1_copy[i]
            temp2=p2_copy[i]
            swap(p1_copy,p1_copy[i],p2_copy[i])
            v.append((temp1,temp2))
    return v

def multipl_coeff_vel(c,v):
    newLen = int(np.floor(c*len(v)))
    v2 = v[:newLen]
    return v2