Skip to content

Tutorial: Prerequisites and simplified example

Paht J edited this page Aug 20, 2020 · 5 revisions

Prerequisites

You will need to install a few dependencies to get going. Python3 - this should be installed if not, you can get it from the internet for your OS.

Python dependencies: numpy

Good resources for python:

You will also need to clone/download this repository how-to-clone-a-repository

Multi-objective - Kursawe Function

What is a Kursawe Function what the optimized shape looks like

Installing GlennOPT

This is not needed to run the test code! Do this only if you really want to.

Navigate to the main folder glennopt and run this command in terminal python setup.py install

Serial Code

Step 1 (Easy mode): Navigate to glennopt\test_functions\KUR\serial

Run the code optimization_setup.py python optimization_setup.py

Step 2: Understanding optimization_setup.py

Import sys and sys.path.insert provides a relative path reference to the init.py and it's vital for testing without installing the full code.

import sys,os
sys.path.insert(0,'../../../../')

These import all the required modules.

from glennopt.helpers import Parameter, mutation_parameters, de_mutation_type
from glennopt.nsga3 import NSGA3,mutation_parameters, de_mutation_type
from glennopt.doe import generate_reference_points

The code below sets up the evaluation. The parameters are configured as a list. The name "x1", "x2", and "x3" are what you will find in the input.txt file. The objectives and performance parameters are read from the output.txt file at the end of each evaluation.

# Generate the DOE
current_dir = os.getcwd()
ns = NSGA3(eval_script = "Evaluation/evaluation.py", eval_folder="Evaluation",num_populations=10,pop_size=40,optimization_folder=current_dir)

eval_parameters = []
eval_parameters.append(Parameter(name="x1",min_value=-5,max_value=5))
eval_parameters.append(Parameter(name="x2",min_value=-5,max_value=5))
eval_parameters.append(Parameter(name="x3",min_value=-5,max_value=5))
ns.add_eval_parameters(eval_params = eval_parameters)

objectives = []
objectives.append(Parameter(name='objective1'))
objectives.append(Parameter(name='objective2'))
ns.add_objectives(objectives=objectives)

# No performance Parameters
performance_parameters = []
performance_parameters.append(Parameter(name='p1'))
performance_parameters.append(Parameter(name='p2'))
performance_parameters.append(Parameter(name='p3'))
ns.add_performance_parameters(performance_params = performance_parameters)

The code below sets up the mutation type. This defines how the next population is generated.

params = mutation_parameters
ns.mutation_params.mutation_type = de_mutation_type.de_1_rand_bin

# ns.start_doe(doe_size=128)
ns.optimize_from_population(pop_start=-1,n_generations=10)
Clone this wiki locally