######################################################################## # # This is an example call of MIDACO 7 # ----------------------------------- # # MIDACO solves Multi-Objective Mixed-Integer Non-Linear Problems: # # # Minimize F_1(X),... F_O(X) where X(1,...N-NI) is CONTINUOUS # and X(N-NI+1,...N) is DISCRETE # # subject to G_j(X) = 0 (j=1,...ME) equality constraints # G_j(X) >= 0 (j=ME+1,...M) inequality constraints # # and bounds XL <= X <= XU # # # The problem statement of this example is given below. You can use # this example as template to run your own problem. To do so: Replace # the objective functions 'F' (and in case the constraints 'G') given # here with your own problem and follow the below instruction steps. # # TEXT-IO: The problem is NOT written in Python here. It is a blackbox # (any executable, e.g. a simulation) that communicates via text files: # # variables.txt written by Python, read by the blackbox (X) # objectives.txt written by the blackbox, read by Python (F,G) # # Every evaluation runs in its own temporary folder, so this example # also works in parallel (see Step 4). The blackbox of this example # is the program in the folder 'blackbox_source'. # ######################################################################## import os, shutil, subprocess, tempfile import midaco ######################################################################## ###################### OPTIMIZATION PROBLEM ######################## ######################################################################## # The blackbox: any executable that reads 'variables.txt' and writes # 'objectives.txt'. Here the example program of the folder # blackbox_source, which lies next to this file. BLACKBOX = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'blackbox.exe' if os.name == 'nt' else 'blackbox' ) O = 1 # Number of objectives (lines of F in 'objectives.txt') M = 3 # Number of constraints (lines of G in 'objectives.txt') def problem_function(x): # Every evaluation gets its own folder (safe for parallel runs) folder = tempfile.mkdtemp( prefix='EVALUATION_FOLDER_', dir=os.getcwd() ) # Write the variables X to 'variables.txt', one value per line with open(os.path.join(folder,'variables.txt'),'w') as txt: for value in x: txt.write( repr(float(value)) + '\n' ) # Run the blackbox inside the evaluation folder subprocess.call( [BLACKBOX], cwd=folder ) # Read F(X) and G(X) from 'objectives.txt': first O lines F, then M lines G with open(os.path.join(folder,'objectives.txt')) as txt: values = [ float(line) for line in txt if line.strip() ] f = values[0:O] g = values[O:O+M] shutil.rmtree( folder, ignore_errors=True ) # Remove the evaluation folder return f,g ######################################################################## ######################### MAIN PROGRAM ############################# ######################################################################## key = b'MIDACO_LIMITED_VERSION___________________ALL_RIGHTS_RESERVED' problem = {} # Initialize dictionary containing problem specifications option = {} # Initialize dictionary containing MIDACO options problem['@'] = problem_function # Handle for problem function name ######################################################################## ### Step 1: Problem definition ##################################### ######################################################################## # STEP 1.A: Problem dimensions ############################## problem['o'] = O # Number of objectives problem['n'] = 4 # Number of variables (in total) problem['ni'] = 2 # Number of integer variables (0 <= ni <= n) problem['m'] = M # Number of constraints (in total) problem['me'] = 1 # Number of equality constraints (0 <= me <= m) # STEP 1.B: Lower and upper bounds 'xl' & 'xu' ############################################## problem['xl'] = [1.0, 1.0, 1.0, 1.0] problem['xu'] = [4.0, 4.0, 4.0, 4.0] # STEP 1.C: Starting point 'x' ############################## problem['x'] = list(problem['xl']) # Here for example: starting point = lower bounds ######################################################################## ### Step 2: Choose stopping criteria and printing options ########### ######################################################################## # STEP 2.A: Stopping criteria ############################# option['maxeval'] = 1000 # Maximum evaluation budget (e.g. 1000000) option['maxtime'] = 60*60*24 # Maximum time limit (e.g. 60*60*24 = 1 Day) # STEP 2.B: Printing options ############################ option['printeval'] = 100 # Print-Frequency for current best solution (e.g. 1000) option['save2file'] = 1 # Save SCREEN and SOLUTION to TXT-files [0=NO/1=YES/>1=HISTORY] option['resumable'] = 0 # Resumable mode [0=NO/1=YES] ######################################################################## ### Step 3: Choose MIDACO parameters (FOR ADVANCED USERS) ########### ######################################################################## option['param1'] = 0.0 # ACCURACY option['param2'] = 0.0 # SEED option['param3'] = 0.0 # FSTOP option['param4'] = 0.0 # ALGOSTOP option['param5'] = 0.0 # EVALSTOP option['param6'] = 0.0 # FOCUS option['param7'] = 0.0 # ANTS option['param8'] = 0.0 # KERNEL option['param9'] = 0.0 # ORACLE option['param10'] = 0.0 # PARETOMAX option['param11'] = 0.0 # EPSILON option['param12'] = 0.0 # BALANCE option['param13'] = 0.0 # CHARACTER option['param14'] = 0.0 # SURROGATE option['param15'] = 0.0 # PMISQP ######################################################################## ### Step 4: Choose Parallelization Factor ############################ ######################################################################## option['parallel'] = 0 # Serial: 0 or 1, Parallel: 2,3,4,5,6,7,8... option['paradigm'] = 0 # Parallel mode: 0 = multiprocessing, 1 = mpi4py, 2 = vectorized ######################################################################## ############################ Run MIDACO ################################ ######################################################################## if __name__ == '__main__': solution = midaco.run( problem, option, key ) # print(solution['f']) # print(solution['g']) # print(solution['x']) ######################################################################## ############################ END OF FILE ############################### ########################################################################