<jupyter_start><jupyter_text>Xopt CNSGA algorithm<jupyter_code>from xopt.cnsga import cnsga
import json
import numpy as np
import matplotlib.pyplot as plt
NAME = 'TNK'
BOUND_LOW, BOUND_UP = [0.0, 0.0], [3.14159, 3.14159]  


X_RANGE = [0, 1.4]
Y_RANGE = [0, 1.4]

# Pure number version
def TNK(individual):    
    x1=individual[0]
    x2=individual[1]
    objectives =  (x1, x2)
    constraints = (x1**2+x2**2-1.0 - 0.1*np.cos(16*np.arctan2(x1, x2)), (x1-0.5)**2+(x2-0.5)**2 )
    return objectives, constraints

# labeled version
def evaluate_TNK(inputs):
    
    info = {'some':'info', 'about':['the', 'run']}
    ind = [inputs['x1'], inputs['x2']]
    objectives, constraints = TNK(ind)    
    outputs = {'y1':objectives[0], 'y2':objectives[1], 'c1':constraints[0], 'c2':constraints[1]}
    
    return outputs

VOCS = {
    'name':'TNK_test',
    
    'variables': {
        'x1':[0, 3.14159],
        'x2':[0, 3.14159]
    },
    'objectives':{
        'y1':'MINIMIZE',
        'y2':'MINIMIZE'
        
    },
    'constraints':{
        'c1': ['GREATER_THAN', 0],
        'c2': ['LESS_THAN', 0.5]
        
    },
    'constants':{'a':'dummy_constant'},
    'linked_variables':{'x9':'x1'}
    
    
}
TNK([0,0])
# Pick a parallel method


#from concurrent.futures import ProcessPoolExecutor as PoolExecutor
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
executor = PoolExecutor()
pop_data = cnsga(executor=executor, vocs=VOCS, evaluate_f=evaluate_TNK,
            max_generations = 20, population_size=64, output_path=None) 
pop_data2 = cnsga(executor=executor, vocs=VOCS, population=pop_data, evaluate_f=evaluate_TNK,
            max_generations = 40, population_size=64, output_path=None) 
pop_data2.keys()<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code># Extract objectives from output
key1, key2 = list(VOCS['objectives'])

x = [o[key1] for o in pop_data2['outputs']]
y = [o[key2] for o in pop_data2['outputs']]

import matplotlib.pyplot as plt
%matplotlib inline

fig, ax = plt.subplots(figsize=(5,5))

ax.scatter(x, y, color='blue')
#ax.set_xlim(X_RANGE)
#ax.set_ylim(Y_RANGE)
ax.set_xlabel(key1)
ax.set_ylabel(key2)
ax.set_aspect('auto')
#ax.set_title(NAME)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16420581655480984, 'fraction_numerical': 0.04742729306487696, 'mean_word_length': 4.070294784580499, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '182221', 'license': 'Apache-2.0', 'n_tokens_mistral': 957, 'n_tokens_neox': 907, 'n_words': 162, 'path': 'examples/cnsga/cnsga_tnk.ipynb', 'repo_name': 'ChristopherMayes/Xopt', 'size': '2235'}