Updating the JEInterface Java code
This commit is contained in:
		@@ -22,19 +22,42 @@ import eva2.gui.BeanInspector;
 | 
			
		||||
 * @author mkron
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class MatlabEvalMediator implements Runnable {
 | 
			
		||||
	volatile boolean requesting = false;
 | 
			
		||||
public class MatlabEvalMediator {
 | 
			
		||||
	volatile Boolean requesting = false;
 | 
			
		||||
//	final static boolean TRACE = false;
 | 
			
		||||
	volatile boolean fin = false;
 | 
			
		||||
	volatile private Boolean fin = false;
 | 
			
		||||
	volatile Object question = null;
 | 
			
		||||
	volatile double[] answer = null;
 | 
			
		||||
	boolean quit = false;
 | 
			
		||||
	volatile boolean quit = false;
 | 
			
		||||
	volatile Object optSolution = null;
 | 
			
		||||
	volatile Object[] optSolSet = null;
 | 
			
		||||
	MatlabProblem mp = null;
 | 
			
		||||
	int runID=-1;
 | 
			
		||||
	volatile MatlabProblem mp = null;
 | 
			
		||||
	// no good: even when waiting for only 1 ms the Matlab execution time increases by a factor of 5-10
 | 
			
		||||
	final static int sleepTime = 5;
 | 
			
		||||
	private int sleepTime = 5;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructor with integer argument for the sleep time in between requests. Values higher than 0 reduce
 | 
			
		||||
	 * cpu load of the waiting thread but may also reduce allover runtime for fast evaluation functions.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param threadSleepTime
 | 
			
		||||
	 */
 | 
			
		||||
	public MatlabEvalMediator(int threadSleepTime) {
 | 
			
		||||
		sleepTime=threadSleepTime;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructor with sleep time set to zero.
 | 
			
		||||
	 */
 | 
			
		||||
	public MatlabEvalMediator() {
 | 
			
		||||
		sleepTime=0;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void setMatlabProblem(MatlabProblem theMP) {
 | 
			
		||||
		mp=theMP;
 | 
			
		||||
		mp.log("setting MP " + theMP + " for MEM " + this + "\n");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Request evaluation from Matlab for the given params.
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -53,40 +76,75 @@ public class MatlabEvalMediator implements Runnable {
 | 
			
		||||
//			
 | 
			
		||||
			if (question == null) System.err.println("Error: requesting evaluation for null array!");
 | 
			
		||||
		} else System.err.println("Error, requesting evaluation for non array!"); 
 | 
			
		||||
		
 | 
			
		||||
		requesting = true;
 | 
			
		||||
//		int k=0;
 | 
			
		||||
		mp.log("-- Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n"); 
 | 
			
		||||
		synchronized(requesting) {
 | 
			
		||||
			if (requesting) {
 | 
			
		||||
				String msg="Warning: already in requesting state when request arrived!";
 | 
			
		||||
				System.err.println(msg);
 | 
			
		||||
				mp.log(msg);
 | 
			
		||||
			}
 | 
			
		||||
			requesting = true;
 | 
			
		||||
			mp.log("-- Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n"); 
 | 
			
		||||
		}
 | 
			
		||||
		int k=0; int mod=25;
 | 
			
		||||
		while (requesting && !quit) {
 | 
			
		||||
			// 	wait for matlab to answer the question
 | 
			
		||||
			if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {};
 | 
			
		||||
//			if ((k%100)==0) {
 | 
			
		||||
			if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {
 | 
			
		||||
				System.err.println("Exception in sleep (MatlabEvalMediator)");
 | 
			
		||||
			};
 | 
			
		||||
			k++;
 | 
			
		||||
			if ((k%mod)==0) {
 | 
			
		||||
//				System.out.println("waiting for matlab to answer...");
 | 
			
		||||
//			}
 | 
			
		||||
//			k++;
 | 
			
		||||
				mp.log("waiting for matlab to answer... (" + mod + ") " + getState() + "\n");
 | 
			
		||||
				mod*=2;
 | 
			
		||||
				if (mod <=0) mod=Integer.MAX_VALUE;
 | 
			
		||||
				
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		mp.log("-- Requesting done\n");
 | 
			
		||||
		// matlab is finished, answer is here
 | 
			
		||||
		//return null;
 | 
			
		||||
		return getAnswer(); // return to JE with answer
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public String getState() {
 | 
			
		||||
		return "ID: " + runID + ", qu: " + BeanInspector.toString(question) + " quit,fin,req " + quit + "," + fin +"," + requesting;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Wait loop, wait until the MatlabProblem requests an evaluation (or finishes), then return.
 | 
			
		||||
	 * For debugging, an integer ID for this run may be provided.
 | 
			
		||||
	 */
 | 
			
		||||
	public void run() {
 | 
			
		||||
//		int k=0;
 | 
			
		||||
	public void run(int id) {
 | 
			
		||||
		logMPSys("## MEM start run " + id);
 | 
			
		||||
		runID=id;		
 | 
			
		||||
		int k=0;int mod=25;
 | 
			
		||||
		while (!requesting && !isFinished() && !quit) {
 | 
			
		||||
			// wait for JE to pose a question or finish all
 | 
			
		||||
			if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {};
 | 
			
		||||
//			if ((k%100)==0) {
 | 
			
		||||
//				System.out.println("waiting for JE to ask...");
 | 
			
		||||
//			}
 | 
			
		||||
//			k++;
 | 
			
		||||
			k++;
 | 
			
		||||
			if ((k%mod)==0) {
 | 
			
		||||
				logMPSys("MEM waiting for JE to ask... (" + mod +") "  + getState());
 | 
			
		||||
				mod*=2;
 | 
			
		||||
				if (mod<=0) mod = Integer.MAX_VALUE;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
//		System.out.println("-- Request arrived in MP thread\n");
 | 
			
		||||
		if (requesting) logMPSys("-- MEM Request arrived in MP thread " + runID);
 | 
			
		||||
		else logMPSys("-- MEM finished or quit " + runID); 
 | 
			
		||||
		// requesting is true, now finish and let Matlab work
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Wait loop, wait until the MatlabProblem requests an evaluation (or finishes), then return.
 | 
			
		||||
	 * Calls the {@link #run(int)} method with ID zero. 
 | 
			
		||||
	 */
 | 
			
		||||
	public void run() {
 | 
			
		||||
		run(0);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void logMPSys(String msg) {
 | 
			
		||||
		if (mp!=null) mp.log(msg + "\n");
 | 
			
		||||
		else System.out.println("MEM has no MP! " + msg);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Cancel waiting in any case.
 | 
			
		||||
@@ -116,18 +174,33 @@ public class MatlabEvalMediator implements Runnable {
 | 
			
		||||
	 * @param y
 | 
			
		||||
	 */
 | 
			
		||||
	public void setAnswer(double[] y) {
 | 
			
		||||
//		System.err.println("answer is " + BeanInspector.toString(y)); 
 | 
			
		||||
		if (y==null) {
 | 
			
		||||
			System.err.println("Error: Matlab function returned null array - this is bad.");
 | 
			
		||||
			System.err.println("X-value was " + BeanInspector.toString(getQuestion()));
 | 
			
		||||
		synchronized(requesting) {
 | 
			
		||||
			if (!requesting) {
 | 
			
		||||
				String msg="Error: not in requesting state when answer arrived!!";
 | 
			
		||||
				System.err.println(msg);
 | 
			
		||||
				mp.log(msg);
 | 
			
		||||
			}
 | 
			
		||||
//			System.err.println("answer is " + BeanInspector.toString(y)); 
 | 
			
		||||
			if (y==null) {
 | 
			
		||||
				System.err.println("Error: Matlab function returned null array - this is bad.");
 | 
			
		||||
				System.err.println("X-value was " + BeanInspector.toString(getQuestion()));
 | 
			
		||||
			}
 | 
			
		||||
			answer = y;
 | 
			
		||||
			requesting = false; // answer is finished, break request loop
 | 
			
		||||
			mp.log("-- setAnswer: " + BeanInspector.toString(y) + ", req state is " + requesting + "\n");
 | 
			
		||||
		}
 | 
			
		||||
		answer = y;
 | 
			
		||||
		requesting = false; // answer is finished, break request loop
 | 
			
		||||
		mp.log("-- setAnswer: " + BeanInspector.toString(y) + ", req state is " + requesting + "\n"); 
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void setFinished(boolean val) {
 | 
			
		||||
		fin = val;
 | 
			
		||||
		synchronized (fin) {
 | 
			
		||||
			if (fin && val) {
 | 
			
		||||
				String msg="Error: already finished when setFinished(true) was called!";
 | 
			
		||||
				System.err.println(msg);
 | 
			
		||||
				if (mp!=null) mp.log(msg+"\n");
 | 
			
		||||
			}
 | 
			
		||||
			fin = val;
 | 
			
		||||
			logMPSys("MEM setFinished ok");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
@@ -108,6 +108,7 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
 | 
			
		||||
 | 
			
		||||
	public void setMediator(MatlabEvalMediator h) {
 | 
			
		||||
		handler = h;
 | 
			
		||||
		handler.setMatlabProblem(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void initProblem() {
 | 
			
		||||
@@ -222,13 +223,17 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
 | 
			
		||||
		if (allowSingleRunnable && (runnable != null) && (!runnable.isFinished())) {
 | 
			
		||||
			System.err.println("Please wait for the current optimization to finish");
 | 
			
		||||
		} else {
 | 
			
		||||
//			log("in MP optimize A\n");
 | 
			
		||||
			handler.setMatlabProblem(this);
 | 
			
		||||
			handler.setFinished(false);
 | 
			
		||||
			runnable = OptimizerFactory.getOptRunnable(optType, (AbstractOptimizationProblem)this, outputFilePrefix);
 | 
			
		||||
//			runnable.getGOParams().setPostProcessParams(new PostProcessParams(0, 0.01, 5));
 | 
			
		||||
//			log("in MP optimize B\n");
 | 
			
		||||
			runnable.setTextListener(this);
 | 
			
		||||
			runnable.setVerbosityLevel(verbosityLevel);
 | 
			
		||||
			runnable.setOutputAdditionalInfo(true);
 | 
			
		||||
 | 
			
		||||
//			log("in MP optimize C\n");
 | 
			
		||||
			if ((specParams != null) && (specParams.length > 0)) {
 | 
			
		||||
				if ((specValues == null) || (specValues.length != specParams.length)) {
 | 
			
		||||
					System.err.println("mismatching value list for parameter arguments: " + specValues);
 | 
			
		||||
@@ -258,7 +263,9 @@ public class MatlabProblem extends AbstractOptimizationProblem implements Interf
 | 
			
		||||
					log(BeanInspector.toString(BeanInspector.getMemberDescriptions(opt, true)));
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
//			log("in MP optimize D\n");
 | 
			
		||||
			new Thread(new WaitForEvARunnable(runnable, this)).start();
 | 
			
		||||
//			log("in MP optimize E\n");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@ class WaitForEvARunnable implements Runnable {
 | 
			
		||||
	public WaitForEvARunnable(OptimizerRunnable runnable, MatlabProblem mp) {
 | 
			
		||||
		this.runnable = runnable;
 | 
			
		||||
		this.mp = mp;
 | 
			
		||||
		mp.log("Created WaitForEvARunnable " + this + "\n");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void run() {
 | 
			
		||||
@@ -40,6 +41,9 @@ class WaitForEvARunnable implements Runnable {
 | 
			
		||||
				// write results back to matlab
 | 
			
		||||
				mp.exportResultToMatlab(runnable);
 | 
			
		||||
				mp.exportResultPopulationToMatlab(runnable.getSolutionSet());
 | 
			
		||||
				mp.log("reported results.\n");
 | 
			
		||||
				mp.notifyFinished();
 | 
			
		||||
				mp.log("notified finish.\n");
 | 
			
		||||
				System.out.println("Optimization finished: " + mp.getInfoString());
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
				StringWriter sw = new StringWriter();
 | 
			
		||||
@@ -51,9 +55,11 @@ class WaitForEvARunnable implements Runnable {
 | 
			
		||||
			mp.log("invalid call, no optimization started.\n");
 | 
			
		||||
			mp.exportResultToMatlab(null);
 | 
			
		||||
			mp.exportResultPopulationToMatlab(null);
 | 
			
		||||
			mp.log("notifying finish...\n");
 | 
			
		||||
			mp.notifyFinished();
 | 
			
		||||
			mp.log("notified finish.\n");
 | 
			
		||||
		}
 | 
			
		||||
		mp.notifyFinished();
 | 
			
		||||
		mp.log("notified finish...");
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user