{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\n# print(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from random import Random\nfrom time import time\nimport inspyred\nimport csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display=True\n# display=False \n\nprng = Random()\nprng.seed(time())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Defaults values for NSGA-II:\n    - Selection: Tournament selection, size: 2. It randomly picks 2 values and choose the best.\n    - Archive : best_archive, it saves the best solutions through time"},{"metadata":{"trusted":true,"_uuid":"f2092665df4ce3e09c21389d9058aca995d06914"},"cell_type":"code","source":"problem = inspyred.benchmarks.Kursawe(3)\nea = inspyred.ec.emo.NSGA2(prng)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ea.variator = [inspyred.ec.variators.crossovers.blend_crossover,\n               inspyred.ec.variators.gaussian_mutation]\n\nea.terminator = inspyred.ec.terminators.generation_termination","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_pop = ea.evolve(generator=problem.generator,\n                      evaluator=problem.evaluator,\n                      pop_size=100,\n                      maximize=problem.maximize,\n                      bounder=problem.bounder,\n                      max_generations=100,\n                      crossover_rate = 1,\n                      mutation_rate=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if display:\n    final_arc = ea.archive\n    #print('Best Solutions: \\n')\n    #for f in final_arc:\n        #print(f)\n    import matplotlib.pyplot as plt\n    x = []\n    y = []\n    for f in final_arc:\n        x.append(f.fitness[0])\n        y.append(f.fitness[1])\n    plt.scatter(x, y, color='b')\n    plt.xlabel(\"Function 1\")\n    plt.ylabel(\"Function 2\")\n    #Plot\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fieldnames = [ 'x1', 'x2','f1','f2']\nlistCandidate = []\nfor f in final_arc:\n    listCandidate.append(f.candidate + f.fitness.values)\n    \nmy_list = []\nfor values in listCandidate:\n    temp = zip(fieldnames, values)\n    inner_dict = dict(temp)\n    my_list.append(inner_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Sampes:\npd.DataFrame(my_list).sample(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}