Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:58:46

0001 import argparse
0002 import json
0003 import nevergrad as ng
0004 
0005 
0006 parser = argparse.ArgumentParser()
0007 parser.add_argument('--max_points', action='store', type=int, required=True, help='max number of points to be generated')
0008 parser.add_argument('--num_points', action='store', type=int, required=True, help='number of points to be generated')
0009 parser.add_argument('--input', action='store', required=True, help='input json file which includes all pre-generated points')
0010 parser.add_argument('--output', action='store', required=True, help='output json file where outputs will be wrote')
0011 
0012 args = parser.parse_args()
0013 
0014 
0015 def get_input_points(input):
0016     points = None
0017     opt_space = None
0018     with open(input) as input_json:
0019         opt_points = json.load(input_json)
0020     if 'points' in opt_points:
0021         points = opt_points['points']
0022     if 'opt_space' in opt_points:
0023         opt_space = opt_points['opt_space']
0024     return points, opt_space
0025 
0026 
0027 def write_output_points(new_points, output):
0028     with open(args.output, 'w') as output_json:
0029         json.dump(new_points, output_json)
0030 
0031 
0032 def get_ng_parameter(param):
0033     # print(param)
0034     params = {}
0035     if 'params' in param:
0036         params = param['params']
0037     bounds = None
0038     if 'bounds' in param:
0039         bounds = param['bounds']
0040 
0041     if param['type'] == 'Choice':
0042         return ng.p.Choice(**params)
0043     elif param['type'] == 'TransitionChoice':
0044         return ng.p.TransitionChoice(**params)
0045     elif param['type'] == 'Array':
0046         s = ng.p.Array(**params)
0047         if bounds:
0048             s.set_bounds(*bounds)
0049         return s
0050     elif param['type'] == 'Scalar':
0051         s = ng.p.Scalar(**params)
0052         if bounds:
0053             s.set_bounds(*bounds)
0054         return s
0055     elif param['type'] == 'Log':
0056         return ng.p.Log(**params)
0057     else:
0058         return None
0059 
0060 
0061 def generate_new_points(input_points, opt_space, max_points, num_points):
0062     if len(input_points) > max_points:
0063         return []
0064     num_points = min(num_points, max_points - len(input_points))
0065 
0066     ng_opt_space = {}
0067     for opt in opt_space:
0068         value = get_ng_parameter(opt_space[opt])
0069         if value:
0070             ng_opt_space[opt] = value
0071 
0072     instrum = ng.p.Instrumentation(**ng_opt_space)
0073 
0074     optimizer = ng.optimizers.DiscreteOnePlusOne(parametrization=instrum, budget=max_points, num_workers=1)
0075 
0076     # print(instrum)
0077     # print(ng_opt_space)
0078     unfinished_points = []
0079     for input_point in input_points:
0080         point, loss = input_point
0081         # print(point)
0082         optimizer.suggest(**point)
0083         candicate = optimizer.ask()
0084         if loss:
0085             optimizer.tell(candicate, loss)
0086         else:
0087             unfinished_points.append(candicate)
0088 
0089     new_points = []
0090     for _ in range(num_points):
0091         x = optimizer.ask()
0092         # TODO: check duplication
0093         if x in unfinished_points:
0094             continue
0095         point = x.value[1]
0096         new_points.append(point)
0097     # recommendation = optimizer.provide_recommendation()
0098     return new_points
0099 
0100 
0101 input_points, opt_space = get_input_points(args.input)
0102 new_points = generate_new_points(input_points, opt_space, args.max_points, args.num_points)
0103 write_output_points(new_points, args.output)