# Copyright (c) 2020 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import numpy as np


def euclidean_distance(x, y):

    n, m = len(x), len(y)
    if n > m:
        a = np.linalg.norm(y - x[:m])
        b = np.linalg.norm(y[-1] - x[m:])
    else:
        a = np.linalg.norm(x - y[:n])
        b = np.linalg.norm(x[-1] - y[n:])
    return np.sqrt(a**2 + b**2)


def compute_novelty_vs_archive(archived_optimizers, optimizers, niche, k, low, high, gather_obstacle_dataset):
    distances = []
    niche.update_pata_ec(archived_optimizers, optimizers, low, high, gather_obstacle_dataset=gather_obstacle_dataset)
    for point in archived_optimizers.values():
        distances.append(euclidean_distance(point.pata_ec, niche.pata_ec))

    for point in optimizers.values():
        distances.append(euclidean_distance(point.pata_ec, niche.pata_ec))


    # Pick k nearest neighbors
    distances = np.array(distances)
    top_k_indicies = (distances).argsort()[:k]
    top_k = distances[top_k_indicies]
    return top_k.mean()
