Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Iterations to Objective #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AdrianoDee
Copy link

Nothing special, just adding the number of the iteration on which we are to the objective function. A bit of context: when building cms-pixel-autotuning/CA-parameter-tuning#4 I noticed that having the number of the iteration we are running on in the objective function could be useful from time to time.

In that specific case, since the outputs are redirect to logs it's useful to keep track of each log per iteration.

def reco_and_validate(params,config,iter,**kwargs):

     workdir = os.getcwd() 
     num_particles = len(params)

     # setting up temp folder
     if not os.path.exists('temp'):
         os.mkdir('temp')

     # writing current input parameters from mopso
     write_csv('temp/parameters.csv', params)
     validation_result = 'temp/simple_validation.root'


     # redirecting outputs to logs
     logfiles = tuple('%s/logs/%s_%d' % (workdir, name,iter) for name in ['process_last_out', 'process_last_err'])
     stdout = open(logfiles[0], 'w')
     stderr = open(logfiles[1], 'w')

     command = ['cmsRun',config,'parametersFile=temp/parameters.csv', 'outputFile=' + validation_result]    
     subprocess.run(command,stdout = stdout, stderr = stderr)

     with uproot.open(validation_result) as uproot_file:
         population_fitness = [get_metrics(uproot_file, i) for i in range(num_particles)]

     return population_fitness
     

In general, for a generic objective function, once could use **kwargs after the mandatory params input for anything extra. This is basically the only modification I did to the optimizer itself for the automatic optimization. I would have tested it with one of the examples but apparently, at the moment, they are not runnable.

@rsreds
Copy link
Collaborator

rsreds commented Dec 13, 2023

Sorry for the delay.
Tested it with the fixed examples and it works.
As you say using **kwargs is probably better to make it more generic. Should we do it directly in this pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants