Skip to content

Commit

Permalink
Better module import for command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gnannicini committed May 24, 2020
1 parent 9390b33 commit 06780ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
38 changes: 23 additions & 15 deletions bin/rbfopt_cl_interface.py
Expand Up @@ -16,6 +16,7 @@

import sys
import os
import importlib
# We must set the threading options before numpy is loaded, otherwise
# there might be issues when running several processes in parallel.
os.environ['OMP_NUM_THREADS'] = '1'
Expand Down Expand Up @@ -79,7 +80,9 @@ def register_options(parser):
'function and the description of its ' +
'characteristics. This file should implement a ' +
'BlackBox class derived from ' +
'rbfopt_black_box.BlackBox. ')
'rbfopt_black_box.BlackBox, with name ' +
'RbfoptBlackBox. Note: the directory containing ' +
'it will be added to the Python path.')
intset.add_argument('--load', '-l', action='store', dest='load_state',
help='File to read state to resume optimization')
intset.add_argument('--log', '-o', action='store',
Expand Down Expand Up @@ -173,10 +176,9 @@ def rbfopt_cl_interface(args, black_box):
except Exception as e:
print('Exception raised reading file with initialization points',
file=output_stream)
print(type(e), file=output_stream)
print(e, file=output_stream)
output_stream.close()
exit()
raise
alg = RbfoptAlgorithm(settings=settings, black_box=black_box,
init_node_pos=init_node_pos,
init_node_val=init_node_val)
Expand Down Expand Up @@ -212,17 +214,23 @@ def rbfopt_cl_interface(args, black_box):
raise ValueError('The file {:s} '.format(args.black_box_file) +
'supposed to provide the implementation of ' +
'RbfoptBlackBox does not exist.')
if (sys.version_info[0] == 2):
import imp
bb = imp.load_source('user_bb', args.black_box_file)
elif (sys.version_info[0] == 3 and 3 <= sys.version_info[1] <= 4):
from importlib.machinery import SourceFileLoader
bb = SourceFileLoader('user_bb', args.black_box_file).load_module()
elif (sys.version_info[0] == 3 and sys.version_info[1] >= 5):
import importlib.util
spec = importlib.util.spec_from_file_location('user_bb',
args.black_box_file)
bb = importlib.util.module_from_spec(spec)
spec.loader.exec_module(bb)

try:
rel_dir = os.path.dirname(args.black_box_file)
if (rel_dir == ''):
rel_dir = '.'
abs_dir = os.path.abspath(rel_dir)
# Add directory to path
sys.path.append(abs_dir)
# Import module (after removing trailing .py, if any)
module_name = os.path.basename(args.black_box_file)
if (module_name.endswith('.py')):
module_name = module_name[:-3]
bb = importlib.import_module(module_name)
except Exception as e:
print('Error while opening module with user black box',
file=sys.stderr)
print(e, file=sys.stderr)
raise
# Run the interface
rbfopt_cl_interface(vars(args), bb.RbfoptBlackBox())
6 changes: 3 additions & 3 deletions src/rbfopt/rbfopt_test_functions.py
Expand Up @@ -1051,9 +1051,9 @@ def evaluate(cls, x):
penalty += 10*max(0, (0.0056858*x[1]*x[4] + 0.0006262*x[0]*x[3] -
0.0022053*x[2]*x[4] + 85.334407) - 92)
penalty += 10*max(0, -(0.0071317*x[1]*x[4] + 0.0029955*x[0]*x[1] +
0.0021813*math.sqrt(x[2]) + 80.51249) + 90)
0.0021813*np.sqrt(x[2]) + 80.51249) + 90)
penalty += 10*max(0, (0.0071317*x[1]*x[4] + 0.0029955*x[0]*x[1] +
0.0021813*math.sqrt(x[2]) + 80.51249) - 110)
0.0021813*np.sqrt(x[2]) + 80.51249) - 110)
penalty += 10*max(0, -(0.0047026*x[2]*x[4] + 0.0012547*x[0]*x[2] +
0.0019085*x[2]*x[3] + 9.300961) + 20)
penalty += 10*max(0, (0.0047026*x[2]*x[4] + 0.0012547*x[0]*x[2] +
Expand Down Expand Up @@ -2618,7 +2618,7 @@ def evaluate(cls, x):
for i in range(len(x)-2):
si = 2**i*np.sqrt((x[i]-cls.optimum_point[i])**2 +
(x[i+1]-cls.optimum_point[i+1])**2)
value += (normalizer * math.sqrt(si) *
value += (normalizer * np.sqrt(si) *
(fun(50*si**0.20) + 1))**2
return value - 10

Expand Down

0 comments on commit 06780ef

Please sign in to comment.