Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:22

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', required=True, help='max number of points to be generated')
0008 parser.add_argument('--num_points', action='store', 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) as output_json:
0029         json.dump(new_points, output_json)
0030 
0031 
0032 def get_ng_parameter(param):
0033     if type(param) in [tuple]:
0034         return ng.p.Scalar(param[0], param[0], param[1])
0035     elif type(param) in [list]:
0036         return ng.p.Choice(param)
0037     else:
0038         return None
0039 
0040 
0041 def generate_new_points(input_points, opt_space, max_points, num_points):
0042     if len(input_points) > max_points:
0043         return []
0044     num_points = min(num_points, max_points - len(input_points))
0045 
0046     ng_opt_space = {}
0047     for opt in opt_space:
0048         value = get_ng_parameter(opt_space[opt])
0049         if value:
0050             ng_opt_space[opt] = value
0051 
0052     instrum = ng.p.Instrumentation(**ng_opt_space)
0053 
0054     optimizer = ng.optimizers.DiscreteOnePlusOne(parametrization=instrum, budget=max_points, num_workers=1)
0055 
0056     unfinished_points = []
0057     for input_point in input_points:
0058         point, loss = input_point
0059         if loss:
0060             optimizer.tell(point, loss)
0061         else:
0062             unfinished_points.append(point)
0063 
0064     new_points = []
0065     for _ in range(num_points):
0066         x = optimizer.ask()
0067         # TODO: check duplication
0068         if x in unfinished_points:
0069             continue
0070         new_points.append(x)
0071     # recommendation = optimizer.provide_recommendation()
0072     return new_points
0073 
0074 
0075 input_points, opt_space = get_input_points(args.input)
0076 new_points = generate_new_points(input_points, opt_space, args.max_points, args.num_points)
0077 write_output_points(new_points, args.output)