RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.rltoys.experiments.parametersweep; 00002 00003 import java.io.File; 00004 import java.util.ArrayList; 00005 import java.util.HashSet; 00006 import java.util.List; 00007 import java.util.Set; 00008 00009 import rlpark.plugin.rltoys.experiments.helpers.ExperimentCounter; 00010 import rlpark.plugin.rltoys.experiments.parametersweep.interfaces.Context; 00011 import rlpark.plugin.rltoys.experiments.parametersweep.interfaces.JobWithParameters; 00012 import rlpark.plugin.rltoys.experiments.parametersweep.interfaces.SweepDescriptor; 00013 import rlpark.plugin.rltoys.experiments.parametersweep.internal.ParametersLogFileReader; 00014 import rlpark.plugin.rltoys.experiments.parametersweep.internal.ParametersLogFileWriter; 00015 import rlpark.plugin.rltoys.experiments.parametersweep.parameters.FrozenParameters; 00016 import rlpark.plugin.rltoys.experiments.parametersweep.parameters.Parameters; 00017 import rlpark.plugin.rltoys.experiments.scheduling.interfaces.JobDoneEvent; 00018 import rlpark.plugin.rltoys.experiments.scheduling.interfaces.JobPool; 00019 import rlpark.plugin.rltoys.experiments.scheduling.interfaces.JobPool.JobPoolListener; 00020 import rlpark.plugin.rltoys.experiments.scheduling.interfaces.PoolResult; 00021 import rlpark.plugin.rltoys.experiments.scheduling.interfaces.Scheduler; 00022 import rlpark.plugin.rltoys.experiments.scheduling.pools.FileJobPool; 00023 import rlpark.plugin.rltoys.experiments.scheduling.pools.PoolResults; 00024 import rlpark.plugin.rltoys.experiments.scheduling.schedulers.LocalScheduler; 00025 import zephyr.plugin.core.api.signals.Listener; 00026 00027 public class SweepAll { 00028 static private boolean verbose = true; 00029 private final Scheduler scheduler; 00030 private final PoolResults poolResults = new PoolResults(); 00031 int nbJobs; 00032 00033 00034 public SweepAll() { 00035 this(new LocalScheduler()); 00036 } 00037 00038 public SweepAll(Scheduler scheduler) { 00039 this.scheduler = scheduler; 00040 } 00041 00042 private void createAndSubmitRequiredJobs(SweepDescriptor sweepDescriptor, ExperimentCounter counter, Context context, 00043 ParametersLogFileWriter logFile) { 00044 List<Parameters> allParameters = sweepDescriptor.provideParameters(context); 00045 String[] parameterLabels = allParameters.get(0).labels(); 00046 Set<FrozenParameters> doneParameters = new HashSet<FrozenParameters>(extractParameters(logFile.filepath, 00047 parameterLabels)); 00048 List<Runnable> todoJobList = new ArrayList<Runnable>(); 00049 for (Parameters parameters : allParameters) { 00050 if (!doneParameters.contains(parameters.froze())) 00051 todoJobList.add(context.createJob(parameters, counter)); 00052 } 00053 if (todoJobList.size() == 0) { 00054 logFile.reorganizeLogFile(parameterLabels); 00055 return; 00056 } 00057 println(String.format("Submitting %d/%d jobs for %s...", todoJobList.size(), allParameters.size(), 00058 extractName(logFile))); 00059 submitRequiredJob(logFile, parameterLabels, todoJobList); 00060 } 00061 00062 private List<FrozenParameters> extractParameters(String filepath, String[] parameterLabels) { 00063 ParametersLogFileReader logFile = new ParametersLogFileReader(filepath); 00064 return logFile.extractParameters(parameterLabels); 00065 } 00066 00067 private void submitRequiredJob(ParametersLogFileWriter logFile, String[] parameterLabels, List<Runnable> todoJobList) { 00068 Listener<JobDoneEvent> jobListener = createJobListener(logFile); 00069 JobPoolListener poolListener = createPoolListener(logFile, parameterLabels); 00070 JobPool pool = new FileJobPool(extractName(logFile), poolListener, jobListener); 00071 for (Runnable job : todoJobList) 00072 pool.add(job); 00073 PoolResult poolResult = pool.submitTo(scheduler); 00074 poolResults.add(poolResult); 00075 } 00076 00077 private String extractName(ParametersLogFileWriter logFile) { 00078 File file = new File(logFile.filepath); 00079 File algoNameParentFile = file.getParentFile(); 00080 File problemNameParentFile = algoNameParentFile.getParentFile(); 00081 return String.format("%s/%s/%s", problemNameParentFile.getName(), algoNameParentFile.getName(), file.getName()); 00082 } 00083 00084 private JobPoolListener createPoolListener(final ParametersLogFileWriter logFile, final String[] parameterLabels) { 00085 return new JobPoolListener() { 00086 @Override 00087 public void listen(JobPool eventInfo) { 00088 logFile.reorganizeLogFile(parameterLabels); 00089 } 00090 }; 00091 } 00092 00093 private Listener<JobDoneEvent> createJobListener(final ParametersLogFileWriter logFile) { 00094 return new Listener<JobDoneEvent>() { 00095 @Override 00096 public void listen(JobDoneEvent eventInfo) { 00097 Parameters doneParameters = ((JobWithParameters) eventInfo.done).parameters(); 00098 logFile.appendParameters(doneParameters); 00099 nbJobs++; 00100 } 00101 }; 00102 } 00103 00104 private void println(String message) { 00105 if (!verbose) 00106 return; 00107 System.out.println(message); 00108 } 00109 00110 public void runSweep(SweepDescriptor sweepDescriptor, ExperimentCounter counter) { 00111 submitSweep(sweepDescriptor, counter); 00112 runAll(); 00113 } 00114 00115 public void runAll() { 00116 startScheduler(); 00117 waitAll(); 00118 } 00119 00120 public void dispose() { 00121 scheduler.dispose(); 00122 } 00123 00124 public void waitAll() { 00125 poolResults.waitPools(); 00126 scheduler.waitAll(); 00127 } 00128 00129 public void startScheduler() { 00130 scheduler.start(); 00131 } 00132 00133 public void submitSweep(SweepDescriptor sweepDescriptor, ExperimentCounter counter) { 00134 println(String.format("Submitting sweep in %s...", counter.folder.getPath())); 00135 counter.reset(); 00136 while (counter.hasNext()) { 00137 counter.nextExperiment(); 00138 submitOneSweep(sweepDescriptor, counter); 00139 } 00140 } 00141 00142 private void submitOneSweep(SweepDescriptor sweepDescriptor, ExperimentCounter counter) { 00143 List<? extends Context> contexts = sweepDescriptor.provideContexts(); 00144 for (Context context : contexts) { 00145 String filename = counter.folderFilename(context.folderPath(), context.fileName()); 00146 ParametersLogFileWriter logFile = new ParametersLogFileWriter(filename); 00147 createAndSubmitRequiredJobs(sweepDescriptor, counter, context, logFile); 00148 } 00149 } 00150 00151 public int nbJobs() { 00152 return nbJobs; 00153 } 00154 00155 static public void disableVerbose() { 00156 verbose = false; 00157 } 00158 00159 public Scheduler scheduler() { 00160 return scheduler; 00161 } 00162 }