# Imports
import numpy as np
import pandas as pd
import sys
import tqdm
import warnings
import time
import ternary
from ternary.helpers import simplex_iterator
import multiprocessing as mp

warnings.simplefilter("ignore")

if sys.platform == "darwin":
    sys.path.append("/Users/aymericvie/Documents/GitHub/evology/evology/code")
    # Need to be executed from cd to MCarloLongRuns
if sys.platform == "linux":
    sys.path.append("/home/vie/Documents/GitHub/evology/evology/code")
from main import main as evology


startTime = time.time()
TimeHorizon = 252 * 5
PopulationSize = 3


def job(coords):
    np.random.seed()
    try:
        df, pop = evology(
            space="scholl",
            solver="esl.true",
            wealth_coordinates=coords,
            POPULATION_SIZE=PopulationSize,
            MAX_GENERATIONS=TimeHorizon,
            PROBA_SELECTION=0,
            MUTATION_RATE=0,
            ReinvestmentRate=1.0,
            InvestmentHorizon=21,
            InvestorBehavior="profit",
            tqdm_display=True,
            reset_wealth=True,
        )
        result = [
            coords[0],
            coords[1],
            coords[2],
            df["NT_returns"].mean(),
            df["VI_returns"].mean(),
            df["TF_returns"].mean(),
            df["NT_returns"].std(),
            df["VI_returns"].std(),
            df["TF_returns"].std(),
            df["HighestT"].mean(),
            df["AvgAbsT"].mean(),
        ]
        return result
    except Exception as e:
        print(e)
        print("Failed run" + str(coords) + str(e))
        result = [coords[0], coords[1], coords[2]]
        for _ in range(8):
            result.append(0)
        return result


# Define the domains


def GenerateCoords(reps, scale):
    param = []
    for (i, j, k) in simplex_iterator(scale):
        for _ in range(reps):
            param.append([i / scale, j / scale, k / scale])
    return param


reps = 10
scale = 50  # increment = 1/scale
param = GenerateCoords(reps, scale)
# print(param)
print(len(param))

# Run experiment
def main():
    p = mp.Pool()
    data = p.map(job, tqdm.tqdm(param))
    p.close()
    data = np.array(data)
    return data


if __name__ == "__main__":
    data = main()
    df = pd.DataFrame()

    # Inputs
    df["WS_NT"] = data[:, 0]
    df["WS_VI"] = data[:, 1]
    df["WS_TF"] = data[:, 2]

    # Outputs
    df["NT_returns_mean"] = data[:, 3]
    df["VI_returns_mean"] = data[:, 4]
    df["TF_returns_mean"] = data[:, 5]
    df["NT_returns_std"] = data[:, 6]
    df["VI_returns_std"] = data[:, 7]
    df["TF_returns_std"] = data[:, 8]
    df["HighestT"] = data[:, 9]
    df["AvgAbsT"] = data[:, 10]

    print(df)

    df.to_csv("data/data1.csv")
    print("Completion time: " + str(time.time() - startTime))

[((12, 0, 12, 31), 'warnings.simplefilter', 'warnings.simplefilter', ({(12, 22, 12, 30): '"""ignore"""'}, {}), "('ignore')", False, 'import warnings\n'), ((22, 12, 22, 23), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((15, 4, 15, 78), 'sys.path.append', 'sys.path.append', ({(15, 20, 15, 77): '"""/Users/aymericvie/Documents/GitHub/evology/evology/code"""'}, {}), "('/Users/aymericvie/Documents/GitHub/evology/evology/code')", False, 'import sys\n'), ((18, 4, 18, 70), 'sys.path.append', 'sys.path.append', ({(18, 20, 18, 69): '"""/home/vie/Documents/GitHub/evology/evology/code"""'}, {}), "('/home/vie/Documents/GitHub/evology/evology/code')", False, 'import sys\n'), ((28, 4, 28, 20), 'numpy.random.seed', 'np.random.seed', ({}, {}), '()', True, 'import numpy as np\n'), ((72, 21, 72, 44), 'ternary.helpers.simplex_iterator', 'simplex_iterator', ({(72, 38, 72, 43): 'scale'}, {}), '(scale)', False, 'from ternary.helpers import simplex_iterator\n'), ((86, 8, 86, 17), 'multiprocessing.Pool', 'mp.Pool', ({}, {}), '()', True, 'import multiprocessing as mp\n'), ((89, 11, 89, 25), 'numpy.array', 'np.array', ({(89, 20, 89, 24): 'data'}, {}), '(data)', True, 'import numpy as np\n'), ((95, 9, 95, 23), 'pandas.DataFrame', 'pd.DataFrame', ({}, {}), '()', True, 'import pandas as pd\n'), ((30, 18, 43, 9), 'main.main', 'evology', (), '', True, 'from main import main as evology\n'), ((87, 22, 87, 38), 'tqdm.tqdm', 'tqdm.tqdm', ({(87, 32, 87, 37): 'param'}, {}), '(param)', False, 'import tqdm\n'), ((115, 36, 115, 47), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n')]