// Path: src/main/java/edu/cmu/cs/lti/ark/util/SerializedObjects.java
// public class SerializedObjects {
// 	public static void writeSerializedObject(Object object, String outFile) {
// 		ObjectOutputStream output = null;
// 		try {
// 			output = getObjectOutputStream(outFile);
// 			output.writeObject(object);
// 		} catch(IOException ex){
// 			// TODO: NONONONONO! stop swallowing errors!
// 			ex.printStackTrace();
// 		} finally{
// 			closeQuietly(output);
// 		}
// 	}
// 
// 	public static Object readSerializedObject(String inputFile) {
// 		ObjectInputStream input = null;
// 		Object recoveredObject = null;
// 		try{
// 			input = getObjectInputStream(inputFile);
// 			recoveredObject = input.readObject();
// 		} catch(Exception ex) {
// 			// TODO: NONONONONO! stop swallowing errors!
// 			ex.printStackTrace();
// 		} finally{
// 			closeQuietly(input);
// 		}
// 		return recoveredObject;
// 	}
// 
// 	public static <T> T readObject(final String inputFile) throws IOException, ClassNotFoundException {
// 		return SerializedObjects.<T>readObject(new InputSupplier<ObjectInputStream>() {
// 			@Override public ObjectInputStream getInput() throws IOException {
// 				return getObjectInputStream(inputFile);
// 			} });
// 	}
// 
// 	@SuppressWarnings("unchecked")
// 	public static <T> T readObject(InputSupplier<ObjectInputStream> inputSupplier)
// 			throws IOException, ClassNotFoundException {
// 		final ObjectInputStream input = inputSupplier.getInput();
// 		try{
// 			return (T) input.readObject();
// 		} finally{
// 			closeQuietly(input);
// 		}
// 	}
// 
// 	private static ObjectInputStream getObjectInputStream(String inputFile) throws IOException {
// 		return new ObjectInputStream(new BufferedInputStream(getInputStream(inputFile)));
// 	}
// 
// 	public static InputStream getInputStream(String inputFile) throws IOException {
// 		if(inputFile.endsWith(".gz")) {
// 			return new GZIPInputStream(new FileInputStream(inputFile));
// 		} else {
// 			return new FileInputStream(inputFile);
// 		}
// 	}
// 
// 	private static ObjectOutputStream getObjectOutputStream(String outFile) throws IOException {
// 		OutputStream file = getOutputStream(outFile);
// 		return new ObjectOutputStream(new BufferedOutputStream(file));
// 	}
// 
// 	public static OutputStream getOutputStream(String outFile) throws IOException {
// 		if(outFile.endsWith(".gz")) {
// 			return new GZIPOutputStream(new FileOutputStream(outFile));
// 		} else {
// 			return new FileOutputStream(outFile);
// 		}
// 	}
// }


import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.primitives.Ints;
import edu.cmu.cs.lti.ark.fn.optimization.Lbfgs;
import edu.cmu.cs.lti.ark.fn.utils.FNModelOptions;
import edu.cmu.cs.lti.ark.fn.utils.ThreadPool;
import edu.cmu.cs.lti.ark.util.SerializedObjects;
import edu.cmu.cs.lti.ark.util.ds.Pair;
import gnu.trove.TIntDoubleHashMap;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import static edu.cmu.cs.lti.ark.util.IntRanges.xrange;
import static edu.cmu.cs.lti.ark.util.Math2.sum;
	}

	/** Adds the sparse vector rhs to the dense vector lhs **/
	private static void plusEquals(double[] lhs, TIntDoubleHashMap rhs) {
		for (int i : rhs.keys()) {
			lhs[i] += rhs.get(i);
		}
	}

	/** Adds the dense vector rhs to the dense vector lhs **/
	private static void plusEquals(double[] lhs, double[] rhs) {
		for (int i = 0; i < lhs.length; i++) {
			lhs[i] += rhs[i];
		}
	}

	/** Multiplies the dense vector <code>vector</code> by the scalar <code>scalar</code> **/
	private static void timesEquals(double[] vector, double scalar) {
		for (int i = 0; i < vector.length; i++) {
			vector[i] *= scalar;
		}
	}

	/**
	 * Get features for every frame for given target
	 *
	 * @param targetIdx the target to get features for
	 * @return map from feature index to feature value (for each frame)
	 */
	private FeaturesAndCost[] getFeaturesForTarget(int targetIdx) throws Exception {
// Path: src/main/java/edu/cmu/cs/lti/ark/util/SerializedObjects.java
// public class SerializedObjects {
// 	public static void writeSerializedObject(Object object, String outFile) {
// 		ObjectOutputStream output = null;
// 		try {
// 			output = getObjectOutputStream(outFile);
// 			output.writeObject(object);
// 		} catch(IOException ex){
// 			// TODO: NONONONONO! stop swallowing errors!
// 			ex.printStackTrace();
// 		} finally{
// 			closeQuietly(output);
// 		}
// 	}
// 
// 	public static Object readSerializedObject(String inputFile) {
// 		ObjectInputStream input = null;
// 		Object recoveredObject = null;
// 		try{
// 			input = getObjectInputStream(inputFile);
// 			recoveredObject = input.readObject();
// 		} catch(Exception ex) {
// 			// TODO: NONONONONO! stop swallowing errors!
// 			ex.printStackTrace();
// 		} finally{
// 			closeQuietly(input);
// 		}
// 		return recoveredObject;
// 	}
// 
// 	public static <T> T readObject(final String inputFile) throws IOException, ClassNotFoundException {
// 		return SerializedObjects.<T>readObject(new InputSupplier<ObjectInputStream>() {
// 			@Override public ObjectInputStream getInput() throws IOException {
// 				return getObjectInputStream(inputFile);
// 			} });
// 	}
// 
// 	@SuppressWarnings("unchecked")
// 	public static <T> T readObject(InputSupplier<ObjectInputStream> inputSupplier)
// 			throws IOException, ClassNotFoundException {
// 		final ObjectInputStream input = inputSupplier.getInput();
// 		try{
// 			return (T) input.readObject();
// 		} finally{
// 			closeQuietly(input);
// 		}
// 	}
// 
// 	private static ObjectInputStream getObjectInputStream(String inputFile) throws IOException {
// 		return new ObjectInputStream(new BufferedInputStream(getInputStream(inputFile)));
// 	}
// 
// 	public static InputStream getInputStream(String inputFile) throws IOException {
// 		if(inputFile.endsWith(".gz")) {
// 			return new GZIPInputStream(new FileInputStream(inputFile));
// 		} else {
// 			return new FileInputStream(inputFile);
// 		}
// 	}
// 
// 	private static ObjectOutputStream getObjectOutputStream(String outFile) throws IOException {
// 		OutputStream file = getOutputStream(outFile);
// 		return new ObjectOutputStream(new BufferedOutputStream(file));
// 	}
// 
// 	public static OutputStream getOutputStream(String outFile) throws IOException {
// 		if(outFile.endsWith(".gz")) {
// 			return new GZIPOutputStream(new FileOutputStream(outFile));
// 		} else {
// 			return new FileOutputStream(outFile);
// 		}
// 	}
// }


// Path: src/main/java/edu/cmu/cs/lti/ark/fn/identification/training/TrainBatch.java
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.primitives.Ints;
import edu.cmu.cs.lti.ark.fn.optimization.Lbfgs;
import edu.cmu.cs.lti.ark.fn.utils.FNModelOptions;
import edu.cmu.cs.lti.ark.fn.utils.ThreadPool;
import edu.cmu.cs.lti.ark.util.SerializedObjects;
import edu.cmu.cs.lti.ark.util.ds.Pair;
import gnu.trove.TIntDoubleHashMap;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import static edu.cmu.cs.lti.ark.util.IntRanges.xrange;
import static edu.cmu.cs.lti.ark.util.Math2.sum;

	}

	/** Adds the sparse vector rhs to the dense vector lhs **/
	private static void plusEquals(double[] lhs, TIntDoubleHashMap rhs) {
		for (int i : rhs.keys()) {
			lhs[i] += rhs.get(i);
		}
	}

	/** Adds the dense vector rhs to the dense vector lhs **/
	private static void plusEquals(double[] lhs, double[] rhs) {
		for (int i = 0; i < lhs.length; i++) {
			lhs[i] += rhs[i];
		}
	}

	/** Multiplies the dense vector <code>vector</code> by the scalar <code>scalar</code> **/
	private static void timesEquals(double[] vector, double scalar) {
		for (int i = 0; i < vector.length; i++) {
			vector[i] *= scalar;
		}
	}

	/**
	 * Get features for every frame for given target
	 *
	 * @param targetIdx the target to get features for
	 * @return map from feature index to feature value (for each frame)
	 */
	private FeaturesAndCost[] getFeaturesForTarget(int targetIdx) throws Exception {
		return SerializedObjects.readObject(eventFiles.get(targetIdx));