The new feature onlymutga with new mutation and experiments

This commit is contained in:
Potalas 2022-01-05 10:37:21 +01:00
commit e5b5e8807d
7 changed files with 1339 additions and 0 deletions

View file

@ -0,0 +1,119 @@
#!/usr/bin/python
###############################################################################
# This script is the command that is executed every run.
# Check the examples in examples/
#
# This script is run in the execution directory (execDir, --exec-dir).
#
# PARAMETERS:
# argv[1] is the candidate configuration ID
# argv[2] is the instance ID
# argv[3] is the seed
# argv[4] is the instance name
# The rest (argv[5:]) are parameters to the run
#
# RETURN VALUE:
# This script should print one numerical value: the cost that must be minimized.
# Exit with 0 if no error, with 1 in case of error
###############################################################################
import datetime
import os.path
import re
import subprocess
import sys
exe = "../../../../release/onlymutga"
problem = 19
pop_size = 1
offspring_size = 100
fixed_parameters = ["--problem", str(problem), "--crossover-rate", "0", "--mutation-rate", "1", "--pop-size", str(pop_size), " --offspring-size", str(offspring_size)]
if __name__=='__main__':
if len(sys.argv) < 5:
print("\nUsage: ./target-runner.py <configuration_id> <instance_id> <seed> <instance_path_name> <list of parameters>\n")
sys.exit(1)
# Get the parameters as command line arguments.
configuration_id = sys.argv[1]
instance_id = sys.argv[2]
seed = sys.argv[3]
instance = sys.argv[4]
slices_prop = sys.argv[5:]
#print(sys.argv)
exe = os.path.expanduser(exe)
cmd = [exe] + fixed_parameters + ["--instance", instance, "--seed", seed]
residual_prob = 1
cl_probs = []
residual_size = 1
cl_sizes = []
values = ""
sizes = ""
for i in range(len(slices_prop)):
cl_probs.append(residual_prob * float(slices_prop[i]))
cl_sizes.append(residual_size * (1-float(slices_prop[i])))
residual_prob -= cl_probs[-1]
residual_size -= cl_sizes[-1]
values += "%.2f,"%cl_probs[-1]
sizes += "%.2f,"%cl_sizes[-1]
cl_probs.append(residual_prob)
values += "%.2f"%cl_probs[-1]
sizes += "%.2f"%cl_sizes[-1]
cmd += ["--cl-probs", values, "--cl-sizes", sizes]
# Define the stdout and stderr files.
out_file = "c" + str(configuration_id) + "-" + str(instance_id) + str(seed) + ".stdout"
err_file = "c" + str(configuration_id) + "-" + str(instance_id) + str(seed) + ".stderr"
def target_runner_error(msg):
now = datetime.datetime.now()
print(str(now) + " error: " + msg)
sys.exit(1)
def check_executable(fpath):
fpath = os.path.expanduser(fpath)
if not os.path.isfile(fpath):
target_runner_error(str(fpath) + " not found")
if not os.access(fpath, os.X_OK):
target_runner_error(str(fpath) + " is not executable")
# This is an example of reading a number from the output.
def parse_output(out):
match = re.search(r'Best ([-+0-9.eE]+)', out.strip())
if match:
return match.group(1);
else:
return "No match"
check_executable (exe)
outf = open(out_file, "w")
errf = open(err_file, "w")
return_code = subprocess.call(cmd, stdout = outf, stderr = errf)
outf.close()
errf.close()
if return_code != 0:
target_runner_error("command returned code " + str(return_code))
if not os.path.isfile(out_file):
target_runner_error("output file " + out_file + " not found.")
cost = parse_output (open(out_file).read())
#print(cost)
print(open(out_file).read().strip())
os.remove(out_file)
os.remove(err_file)
sys.exit(0)

View file

@ -0,0 +1,267 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "04867792",
"metadata": {},
"source": [
"# Imports"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "435212a6",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import seaborn as sb\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.ticker import MaxNLocator\n",
"import matplotlib.animation\n",
"from math import sqrt, log, cos, sin, pi\n",
"import numpy as np\n",
"import os\n",
"import shutil\n",
"import re\n",
"from subprocess import call\n",
"sb.set_style(\"whitegrid\")\n",
"#sb.set_palette(\"cubehelix\")\n",
"sb.set_palette(\"husl\")\n",
"sb.set(font_scale=1) # crazy big\n",
"sb.set_style('whitegrid', {'legend.frameon':True})\n",
"myfontsize = 12\n",
"titlesize = 15\n",
"%matplotlib notebook"
]
},
{
"cell_type": "markdown",
"id": "d76bdf6b",
"metadata": {},
"source": [
"# Function to generate the scenario file for irace"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "d86a9ca8",
"metadata": {},
"outputs": [],
"source": [
"def scenario(filename=\"scenario.txt\", \n",
" parameterFile=\"parameters.txt\", \n",
" execDir=\".\", \n",
" logFile=\"./irace.Rdata\", \n",
" targetRunner = \"target-runner.py\", \n",
" maxExperiments = 100000,\n",
" digits = 2):\n",
" f = open(filename, \"w\")\n",
" f.write(\"parameterFile=\" + parameterFile +\"\\n\")\n",
" f.write(\"execDir=\" + execDir + \"\\n\")\n",
" f.write(\"logFile=\" + logFile + \"\\n\")\n",
" f.write(\"targetRunner=\" + targetRunner + \"\\n\")\n",
" f.write(\"maxExperiments=\" + maxExperiments + \"\\n\")\n",
" f.write(\"digits=\" + digits + \"\\n\")\n",
" f.close()"
]
},
{
"cell_type": "markdown",
"id": "1213321a",
"metadata": {},
"source": [
"# Function to generate the parameter file for irace"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "70d221c9",
"metadata": {},
"outputs": [],
"source": [
"# Generate the param file for irace with all configuarable parameters\n",
"def parameters(filename=\"parameters.txt\"):\n",
" f = open(\"parameters.txt\", \"w\")\n",
" f.write(\"# name\\tswitch\\ttype\\tvalues\\n\") # head of the param file\n",
" cl_nb_part = 10 # number of category for the custom categorial probabilistic law\n",
" for i in range(cl_nb_part-1): # minus 1 slice than the number of categories\n",
" f.write(\"slice_prob_%s\\t\\\"\\\"\\tr\\t(0,1)\\n\"%i) # percentage of the residual probability for the slice\n",
"\n",
" ######################################### NOT USED YET ##########################################\n",
" #for i in range(cl_nb_part-1): # minus 1 slice than the number of categories\n",
" # f.write(\"slice_size_%s\\t\\\"\\\"\\tr\\t(0,1)\\n\"%i) # percentage of the residual size for the slice\n",
" #################################################################################################\n",
" f.close()"
]
},
{
"cell_type": "markdown",
"id": "65fcb69d",
"metadata": {},
"source": [
"# Fonction to generate problem dedicated target-runner.py"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "a18ba251",
"metadata": {},
"outputs": [],
"source": [
"def target_runner(origin=\"irace-config/target-runner.py\", path=\"target-runner.py\", problem=1):\n",
" \n",
" generalTR = open(origin, \"r\")\n",
" dedicatedTR = open(path, \"w\")\n",
" for line in generalTR:\n",
" if re.search(\"problem = \", line, flags=0):\n",
" dedicatedTR.write(\"problem = \" + str(problem) + \"\\n\")\n",
" else:\n",
" dedicatedTR.write(line)\n",
" generalTR.close()\n",
" dedicatedTR.close()"
]
},
{
"cell_type": "markdown",
"id": "59421dee",
"metadata": {},
"source": [
"# Run script"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "38dfec96",
"metadata": {},
"outputs": [],
"source": [
"results_directory = \"results\"\n",
"irace_path = \"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/irace/bin/irace\"\n",
"instances_file = \"instances.txt\"\n",
"scenario_file = \"scenario.txt\"\n",
"parameters_file = \"parameters.txt\"\n",
"target_runner_file = \"target-runner.py\"\n",
"\n",
"# create or clear the results directory\n",
"if not os.path.isdir(results_directory):\n",
" os.mkdir(results_directory)\n",
" \n",
"for pb in range(1,3): # for each problem\n",
" # create or clear a subdirectory for the problem\n",
" problem_directory = results_directory + \"/problem_%s\"%pb\n",
" if os.path.isdir(problem_directory):\n",
" shutil.rmtree(problem_directory)\n",
" os.mkdir(problem_directory)\n",
" \n",
" # generate a custom target runner file for the problem\n",
" target_runner(path = problem_directory + \"/\" + target_runner_file, problem = pb)\n",
"\n",
" # copy the config files for iraces\n",
" for filename in [instances_file, scenario_file, parameters_file, target_runner_file]:\n",
" src = r'irace-config/' + filename\n",
" dst = problem_directory + \"/\" + filename\n",
" shutil.copyfile(src, dst)\n",
" \n",
" # run irace for\n",
" cmd = [irace_path, \"--scenario\", problem_directory + \"/\" + scenario_file] #, \"&> irace.log\"\n",
" call(cmd)\n",
"#call(cmd)\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "b707ff3b",
"metadata": {},
"outputs": [],
"source": [
"shutil.rmtree(\"results\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "460c588e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-10"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"call([\"../../release/onlymutga\"])"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "eb234425",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'problem_1/default.instances'"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import shutil\n",
"import os\n",
"src = r'irace-config/default.instances'\n",
"dst = r'problem_1/default.instances'\n",
"shutil.copyfile(src, dst)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a62c411",
"metadata": {},
"outputs": [],
"source": [
"chmod u+x script.py\n",
"cp -a a b"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 14 12:16:17 2021
@author: labeiros
"""
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))

View file

@ -0,0 +1,119 @@
#!/usr/bin/python
###############################################################################
# This script is the command that is executed every run.
# Check the examples in examples/
#
# This script is run in the execution directory (execDir, --exec-dir).
#
# PARAMETERS:
# argv[1] is the candidate configuration ID
# argv[2] is the instance ID
# argv[3] is the seed
# argv[4] is the instance name
# The rest (argv[5:]) are parameters to the run
#
# RETURN VALUE:
# This script should print one numerical value: the cost that must be minimized.
# Exit with 0 if no error, with 1 in case of error
###############################################################################
import datetime
import os.path
import re
import subprocess
import sys
exe = "../../../../release/onlymutga"
problem = blabla
pop_size = 1
offspring_size = 100
fixed_parameters = ["--problem", str(problem), "--crossover-rate", "0", "--mutation-rate", "1", "--pop-size", str(pop_size), " --offspring-size", str(offspring_size)]
if __name__=='__main__':
if len(sys.argv) < 5:
print("\nUsage: ./target-runner.py <configuration_id> <instance_id> <seed> <instance_path_name> <list of parameters>\n")
sys.exit(1)
# Get the parameters as command line arguments.
configuration_id = sys.argv[1]
instance_id = sys.argv[2]
seed = sys.argv[3]
instance = sys.argv[4]
slices_prop = sys.argv[5:]
#print(sys.argv)
exe = os.path.expanduser(exe)
cmd = [exe] + fixed_parameters + ["--instance", instance, "--seed", seed]
residual_prob = 1
cl_probs = []
residual_size = 1
cl_sizes = []
values = ""
sizes = ""
for i in range(len(slices_prop)):
cl_probs.append(residual_prob * float(slices_prop[i]))
cl_sizes.append(residual_size * (1-float(slices_prop[i])))
residual_prob -= cl_probs[-1]
residual_size -= cl_sizes[-1]
values += "%.2f,"%cl_probs[-1]
sizes += "%.2f,"%cl_sizes[-1]
cl_probs.append(residual_prob)
values += "%.2f"%cl_probs[-1]
sizes += "%.2f"%cl_sizes[-1]
cmd += ["--cl-probs", values, "--cl-sizes", sizes]
# Define the stdout and stderr files.
out_file = "c" + str(configuration_id) + "-" + str(instance_id) + str(seed) + ".stdout"
err_file = "c" + str(configuration_id) + "-" + str(instance_id) + str(seed) + ".stderr"
def target_runner_error(msg):
now = datetime.datetime.now()
print(str(now) + " error: " + msg)
sys.exit(1)
def check_executable(fpath):
fpath = os.path.expanduser(fpath)
if not os.path.isfile(fpath):
target_runner_error(str(fpath) + " not found")
if not os.access(fpath, os.X_OK):
target_runner_error(str(fpath) + " is not executable")
# This is an example of reading a number from the output.
def parse_output(out):
match = re.search(r'Best ([-+0-9.eE]+)', out.strip())
if match:
return match.group(1);
else:
return "No match"
check_executable (exe)
outf = open(out_file, "w")
errf = open(err_file, "w")
return_code = subprocess.call(cmd, stdout = outf, stderr = errf)
outf.close()
errf.close()
if return_code != 0:
target_runner_error("command returned code " + str(return_code))
if not os.path.isfile(out_file):
target_runner_error("output file " + out_file + " not found.")
cost = parse_output (open(out_file).read())
#print(cost)
print(open(out_file).read().strip())
os.remove(out_file)
os.remove(err_file)
sys.exit(0)