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

import numpy as np

import geatpy as ea   # import geatpy

from mlrGAtsne.GA import MyProblem # 导入自定义问题接口

#from draft import Myproblem
import csv

from mlrGAtsne.Configuration import Configuration


def main(con:Configuration):

    """================================实例化问题对象==========================="""

    problem = MyProblem(con) # 生成问题对象

    """==================================种群设置==============================="""

    Encoding = 'RI'       # 编码方式

    #NIND = 30            # 种群规模
    NIND=con.NIND
    Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # 创建区域描述器

    population = ea.Population(Encoding, Field, NIND) # 实例化种群对象（此时种群还没被初始化，仅仅是完成种群对象的实例化）

    """================================算法参数设置============================="""

    myAlgorithm = ea.soea_DE_rand_1_L_templet(problem, population) # 实例化一个算法模板对象

    #myAlgorithm.MAXGEN = 200 # 最大进化代数
    myAlgorithm.MAXGEN=con.MAXGEN

    myAlgorithm.mutOper.F = 0.5 # 差分进化中的参数F

    myAlgorithm.recOper.XOVR = 0.7 # 重组概率

    myAlgorithm.drawing=0 # 设置绘图方式（0：不绘图；1：绘制结果图；2：绘制过程动画）


    """===========================调用算法模板进行种群进化======================="""

    [population, obj_trace, var_trace] = myAlgorithm.run() # 执行算法模板

    population.save() # 把最后一代种群的信息保存到文件中

    # 输出结果

    best_gen = np.argmin(problem.maxormins * obj_trace[:, 1]) # 记录最优种群个体是在哪一代

    best_ObjV = obj_trace[best_gen, 1]

    print('最优的目标函数值为：%s'%(best_ObjV))

    print('最优的决策变量值为：')

    for i in range(var_trace.shape[1]):

        print(var_trace[best_gen, i])

    print('有效进化代数：%s'%(obj_trace.shape[0]))

    print('最优的一代是第 %s 代'%(best_gen + 1))

    print('评价次数：%s'%(myAlgorithm.evalsNum))

    print('实际评价次数：%s'%(problem.fitfun.count))

    print('时间已过 %s 秒'%(myAlgorithm.passTime))

    """
        with open("performance.csv", "a", newline='') as csvfile2:
        writer = csv.writer(csvfile2)
        writer.writerow([con.function + ' based on ' + con.mode, problem.fitfun.count, myAlgorithm.passTime])
    
    """

    """
   
    """
    # 保存每一代信息
    with open(con.function + ' based on ' + con.mode + ".csv", "a", newline='') as csvfile:
        writer = csv.writer(csvfile)
        for index in range(len(problem.fitnessvalue)):  # not tested
            writer.writerow([str(index), int(problem.fitnessvalue[index])])
    print(con.function + ' based on ' + con.mode + ' is done.')



if __name__ == '__main__':
    main()