<jupyter_start><jupyter_text>Xopt class, Astra kekgun exampleThis is the class method for running Xopt.<jupyter_code>from xopt import Xopt
!mkdir temp/
import yaml

# Make a proper input file. 

YAML="""
xopt: {output_path: temp, verbose: true,
  algorithm: cnsga}
  
algorithm:
  name: cnsga
  options: {max_generations: 3, population_size: 8, crossover_probability: 0.9, mutation_probability: 1.0,
  selection: auto, verbose: true, population: null}

simulation:
  name: astra_with_generator
  evaluate: astra.evaluate.evaluate_astra_with_generator

# Note that you can call another file in the top level group:
vocs: ../templates/kekgun/kekgun_vocs.yaml

"""
config = yaml.safe_load(YAML)
config
# Create object
X = Xopt(config=config, verbose=True)
X
# Create object
X = Xopt(config=config)

# Change some things to make it run faster
X.vocs['constants']['lspch'] = True
X.vocs['constants']['ipart'] = 1000
X.vocs['constants']['zstop'] = 0.2

# Show config
X
# Check random inputs 
X.random_inputs()
# Evaluate with some particular settings
X.evaluate(
    {'ipart': 1000,
 'lspch': True,
 'zstop': 0.2})
%%time
# Do a random evaluate to check that everything will run
output = X.random_evaluate()
output
# These are the algorithm options
X.algorithm['options']
# These are the options in the evaluate function
X.simulation['options']<jupyter_output><empty_output><jupyter_text>Run CNSGA using processes or threads<jupyter_code>from concurrent.futures import ProcessPoolExecutor as PoolExecutor
#from concurrent.futures import ThreadPoolExecutor as PoolExecutor
executor = PoolExecutor()
# Create object
X = Xopt(config=config)

# Change some things to make it run faster
X.vocs['constants']['lspch'] = True
X.vocs['constants']['ipart'] = 1000
X.vocs['constants']['zstop'] = 0.2
X.results

# Run
X.run(executor)
# Check for errors
X.results['error']<jupyter_output><empty_output><jupyter_text>Write this configuration<jupyter_code>X.config['algorithm']['options']['population'] = '/Users/chrisonian/Code/GitHub/xopt/examples/xopt_class/temp/pop_3.json'
X.save('test.yaml')
!cat test.yaml<jupyter_output>xopt: {output_path: /Users/chrisonian/Code/GitHub/xopt/examples/xopt_class/temp, verbose: true,
  algorithm: cnsga}
algorithm:
  name: cnsga
  function: xopt.cnsga.cnsga
  options: {max_generations: 3, population_size: 8, crossover_probability: 0.9, mutation_probability: 1.0,
    selection: auto, verbose: true, population: /Users/chrisonian/Code/GitHub/xopt/examples/xopt_class/temp/pop_3.json,
    seed: null}
simulation:
  name: astra_with_generator
  evaluate: astra.evaluate.evaluate_astra_with_generator
  options: {astra_input_file: /Users/chrisonian/Code/GitHub/xopt/examples/templates/kekgun/kekgun.in,
    generator_input_file: /Users/chrisonian/Code/GitHub/xopt/examples/templates/kekgun/dist004.in,
    workdir: null, astra_bin: /Users/chrisonian/Code/astra/bin/Astra, generator_bin: /Users/chrisonian/Code/astra/bin/generator,
    timeout: 2500, verbose: false, auto_set_spacecharge_mesh: true, archive_path: null,
    merit_f: null}
vocs:
  name: kekgun
  description: Test case
  simu[...]<jupyter_text>Run with MPI<jupyter_code>!mpirun -n 4 python -m mpi4py.futures -m xopt.mpi.run test.yaml<jupyter_output>_   
                | |  
__  _____  _ __ | |_ 
\ \/ / _ \| '_ \| __|
 >  < (_) | |_) | |_ 
/_/\_\___/| .__/ \__|
          | |        
          |_|        

_________________________________
Parallel execution with 4 workers
Loading as YAML file: test.yaml
Warning: No path set for key simulation : options : archive_path

            Xopt 
________________________________           
Version: 0.4.3
Configured: True
Config as YAML:
xopt: {output_path: /Users/chrisonian/Code/GitHub/xopt/examples/xopt_class/temp, verbose: true,
  algorithm: cnsga}
algorithm:
  name: cnsga
  function: xopt.cnsga.cnsga
  options: {max_generations: 3, population_size: 8, crossover_probability: 0.9, mutation_probability: 1.0,
    selection: auto, verbose: true, population: /Users/chrisonian/Code/GitHub/xopt/examples/xopt_class/temp/pop_3.json,
    seed: null}
simulation:
  name: astra_with_generator
  evaluate: astra.evaluate.evaluate_astra_with_generator
  options: {astra_input_file: /Users/chrisonian/Cod[...]<jupyter_text>Dask<jupyter_code>from dask.distributed import Client
executor = Client()
#executor = Client(processes=True)
executor
# Wait a few seconds for the Dask cluster to start
from time import sleep
sleep(5)
# Run again
X.run(executor)
X.results['error']<jupyter_output><empty_output><jupyter_text>Cleanup<jupyter_code>!rm -r dask-worker-space
!rm test.yaml
!rm -r temp
!rm NORRAN<jupyter_output>rm: NORRAN: No such file or directory
{'fraction_non_alphanumeric': 0.11745827984595636, 'fraction_numerical': 0.011339323919554984, 'mean_word_length': 4.910240202275601, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '214817', 'license': 'Apache-2.0', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1627, 'n_words': 370, 'path': 'examples/xopt_class/xopt_cnsga_astra.ipynb', 'repo_name': 'whitegr/xopt', 'size': '4674'}