//
//  OpticsEvaluator.java
//  xal
//
//  Created by Thomas Pelaia on 2/18/05.
//  Copyright 2005 Oak Ridge National Lab. All rights reserved.
//


package xal.app.energymanager;

import xal.tools.solver.*;
import xal.tools.solver.algorithm.*;

import java.util.*;


/** Class for evaluating an optics. */
public class OpticsEvaluator implements Evaluator {
	/** The simulator used to run the online model with trial points */
	final OnlineModelSimulator _simulator;
	
	
	/**
	 * Constructor
	 */
	public OpticsEvaluator( final OnlineModelSimulator simulator ) {
		_simulator = simulator;
	}
	
	
	/**
	 * Get the simulator.
	 */
	public OnlineModelSimulator getSimulator() {
		return _simulator;
	}
	
	
	/**
	 * Score the trial.
	 * @param trial  The trial to evaluate.
	 */
	public void evaluate( final Trial trial ) {
		final Map<Variable, Number> trialValues = trial.getTrialPoint().getValueMap();
		final Simulation designSimulation = _simulator.getDesignSimulation();
		final Simulation simulation = _simulator.runWithVariables( trialValues );

		for ( final Objective rawObjective : trial.getProblem().getObjectives() ) {
			final OpticsObjective objective = (OpticsObjective)rawObjective;
			final double score = objective.getValue( trialValues, simulation, designSimulation );
			trial.setScore( objective, score );
		}
		
		trial.setCustomInfo( simulation );
	}
}

@@ -1044,16 +1044,47 @@
 ues );%0A%0A
+%09%09if ( simulation != null ) %7B%0A%09
 %09%09for ( 
@@ -1149,24 +1149,25 @@
 tives() ) %7B%0A
+%09
 %09%09%09final Opt
@@ -1225,16 +1225,17 @@
 ive;%0A%09%09%09
+%09
 final do
@@ -1315,16 +1315,17 @@
 n );%0A%09%09%09
+%09
 trial.se
@@ -1358,14 +1358,14 @@
 ;%0A%09%09
-%7D
+%09%7D%0A
 %0A%09%09
-%0A%09
 %09tri
@@ -1396,13 +1396,101 @@
 tion );%0A
+%09%09%7D%0A%09%09else %7B%0A%09%09%09trial.vetoTrial( new TrialVeto( trial, null, %22Model Exception%22 ) );%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Handle exceptions in the Energy Manager evaluator by vetoing associated trials so the solver can continue searching.