/** {@inheritDoc} */ @Override public final Object read(final InputStream is) { Map<String, String> networkParams = null; final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; int inputCount = 0; int instarCount = 0; int outputCount = 0; int winnerCount = 0; Matrix m1 = null; Matrix m2 = null; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("CPN") && section.getSubSectionName().equals("PARAMS")) { networkParams = section.parseParams(); } if (section.getSectionName().equals("CPN") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); inputCount = EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT); instarCount = EncogFileSection.parseInt(params, PersistConst.INSTAR); outputCount = EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT); winnerCount = EncogFileSection.parseInt(params, PersistCPN.PROPERTY_winnerCount); m1 = EncogFileSection.parseMatrix(params, PersistCPN.PROPERTY_inputToInstar); m2 = EncogFileSection.parseMatrix(params, PersistCPN.PROPERTY_instarToInput); } } final CPN result = new CPN(inputCount, instarCount, outputCount, winnerCount); result.getProperties().putAll(networkParams); result.getWeightsInputToInstar().set(m1); result.getWeightsInstarToOutstar().set(m2); return result; }
/** {@inheritDoc} */ @Override public final Object read(final InputStream is) { final BAM result = new BAM(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("BAM") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("BAM") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); result.setF1Count(EncogFileSection.parseInt(params, PersistConst.PROPERTY_F1_COUNT)); result.setF2Count(EncogFileSection.parseInt(params, PersistConst.PROPERTY_F2_COUNT)); result.setWeightsF1toF2( EncogFileSection.parseMatrix(params, PersistConst.PROPERTY_WEIGHTS_F1_F2)); result.setWeightsF2toF1( EncogFileSection.parseMatrix(params, PersistConst.PROPERTY_WEIGHTS_F2_F1)); } } return result; }
/** {@inheritDoc} */ @Override public Object read(final InputStream is) { final ART1 result = new ART1(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("ART1") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("ART1") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); result.setA1(EncogFileSection.parseDouble(params, ART.PROPERTY_A1)); result.setB1(EncogFileSection.parseDouble(params, ART.PROPERTY_B1)); result.setC1(EncogFileSection.parseDouble(params, ART.PROPERTY_C1)); result.setD1(EncogFileSection.parseDouble(params, ART.PROPERTY_D1)); result.setF1Count(EncogFileSection.parseInt(params, PersistConst.PROPERTY_F1_COUNT)); result.setF2Count(EncogFileSection.parseInt(params, PersistConst.PROPERTY_F2_COUNT)); result.setNoWinner(EncogFileSection.parseInt(params, ART.PROPERTY_NO_WINNER)); result.setL(EncogFileSection.parseDouble(params, ART.PROPERTY_L)); result.setVigilance(EncogFileSection.parseDouble(params, ART.PROPERTY_VIGILANCE)); result.setWeightsF1toF2( EncogFileSection.parseMatrix(params, PersistConst.PROPERTY_WEIGHTS_F1_F2)); result.setWeightsF2toF1( EncogFileSection.parseMatrix(params, PersistConst.PROPERTY_WEIGHTS_F2_F1)); } } return result; }
/** {@inheritDoc} */ @Override public Object read(final InputStream is) { final BoltzmannMachine result = new BoltzmannMachine(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("BOLTZMANN") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("BOLTZMANN") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); result.setWeights( NumberList.fromList(CSVFormat.EG_FORMAT, params.get(PersistConst.WEIGHTS))); result.setCurrentState( NumberList.fromList(CSVFormat.EG_FORMAT, params.get(PersistConst.OUTPUT))); result.setNeuronCount(EncogFileSection.parseInt(params, PersistConst.NEURON_COUNT)); result.setThreshold( NumberList.fromList(CSVFormat.EG_FORMAT, params.get(PersistConst.THRESHOLDS))); result.setAnnealCycles(EncogFileSection.parseInt(params, BoltzmannMachine.ANNEAL_CYCLES)); result.setRunCycles(EncogFileSection.parseInt(params, BoltzmannMachine.RUN_CYCLES)); result.setTemperature(EncogFileSection.parseDouble(params, PersistConst.TEMPERATURE)); } } return result; }
/** {@inheritDoc} */ @Override public final Object read(final InputStream is) { final BayesianNetwork result = new BayesianNetwork(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; String queryType = ""; String queryStr = ""; String contentsStr = ""; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("BAYES-NETWORK") && section.getSubSectionName().equals("BAYES-PARAM")) { final Map<String, String> params = section.parseParams(); queryType = params.get("queryType"); queryStr = params.get("query"); contentsStr = params.get("contents"); } if (section.getSectionName().equals("BAYES-NETWORK") && section.getSubSectionName().equals("BAYES-TABLE")) { result.setContents(contentsStr); // first, define relationships (1st pass) for (String line : section.getLines()) { result.defineRelationship(line); } result.finalizeStructure(); // now define the probabilities (2nd pass) for (String line : section.getLines()) { result.defineProbability(line); } } if (section.getSectionName().equals("BAYES-NETWORK") && section.getSubSectionName().equals("BAYES-PROPERTIES")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } } // define query, if it exists if (queryType.length() > 0) { BayesianQuery query = null; if (queryType.equals("EnumerationQuery")) { query = new EnumerationQuery(result); } else { query = new SamplingQuery(result); } if (query != null && queryStr.length() > 0) { result.setQuery(query); result.defineClassificationStructure(queryStr); } } return result; }
public void loadConfig() { try { String home = System.getProperty("user.home"); File file = new File(home, EncogWorkBench.CONFIG_FILENAME); InputStream is = new FileInputStream(file); EncogReadHelper in = new EncogReadHelper(is); // read the config file EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("ENCOG") && section.getSubSectionName().equals("TRAINING")) { Map<String, String> params = section.parseParams(); try { this.defaultError = EncogFileSection.parseDouble(params, EncogWorkBenchConfig.PROPERTY_DEFAULT_ERROR); this.threadCount = EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_THREAD_COUNT); this.useOpenCL = EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_THREAD_COUNT) > 0; this.errorCalculation = EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_ERROR_CALC); this.iterationStepCount = EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_STEP_COUNT); this.trainingHistory = EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_TRAINING_HISTORY); setLogLevel(EncogFileSection.parseInt(params, EncogWorkBenchConfig.PROPERTY_LOG_LEVEL)); this.showTrainingImprovement = EncogFileSection.parseBoolean( params, EncogWorkBenchConfig.PROPERTY_TRAINING_IMPROVEMENT); } catch (PersistError e) { resetDefaults(); } } else if (section.getSectionName().equals("ENCOG") && section.getSubSectionName().equals("PATHS")) { Map<String, String> params = section.parseParams(); if (params.containsKey(EncogWorkBenchConfig.PROPERTY_PROJECT_ROOT)) { this.projectRoot = params.get(EncogWorkBenchConfig.PROPERTY_CLOUD_ALLOW_CONNECTIONS); } if (this.projectRoot == null || this.projectRoot.trim().length() == 0) { this.projectRoot = EncogWorkBenchConfig.getDefaultProjectRoot().toString(); } } } is.close(); } catch (Exception e) { e.printStackTrace(); } }
/** {@inheritDoc} */ @Override public final Object read(final InputStream is) { final SOM result = new SOM(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("SOM") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("SOM") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); result.setWeights(EncogFileSection.parseMatrix(params, PersistConst.WEIGHTS)); result.setOutputNeuronCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT)); result.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT)); } } return result; }
/** {@inheritDoc} */ @Override public Object read(final InputStream is) { final BasicNetwork result = new BasicNetwork(); final FlatNetwork flat = new FlatNetwork(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("BASIC") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("BASIC") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); flat.setBeginTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_BEGIN_TRAINING)); flat.setConnectionLimit( EncogFileSection.parseDouble(params, BasicNetwork.TAG_CONNECTION_LIMIT)); flat.setContextTargetOffset( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_OFFSET)); flat.setContextTargetSize( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_SIZE)); flat.setEndTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_END_TRAINING)); flat.setHasContext(EncogFileSection.parseBoolean(params, BasicNetwork.TAG_HAS_CONTEXT)); flat.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT)); flat.setLayerCounts(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_COUNTS)); flat.setLayerFeedCounts( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_FEED_COUNTS)); flat.setLayerContextCount( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_CONTEXT_COUNT)); flat.setLayerIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_INDEX)); flat.setLayerOutput(section.parseDoubleArray(params, PersistConst.OUTPUT)); flat.setLayerSums(new double[flat.getLayerOutput().length]); flat.setOutputCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT)); flat.setWeightIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_WEIGHT_INDEX)); flat.setWeights(section.parseDoubleArray(params, PersistConst.WEIGHTS)); flat.setBiasActivation(section.parseDoubleArray(params, BasicNetwork.TAG_BIAS_ACTIVATION)); } else if (section.getSectionName().equals("BASIC") && section.getSubSectionName().equals("ACTIVATION")) { int index = 0; flat.setActivationFunctions(new ActivationFunction[flat.getLayerCounts().length]); for (final String line : section.getLines()) { ActivationFunction af = null; final List<String> cols = EncogFileSection.splitColumns(line); // if this is a class name with a path, then do not default to inside of the Encog // package. String name; if (cols.get(0).indexOf('.') != -1) { name = cols.get(0); } else { name = "org.encog.engine.network.activation." + cols.get(0); } try { final Class<?> clazz = Class.forName(name); af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e); } for (int i = 0; i < af.getParamNames().length; i++) { af.setParam(i, CSVFormat.EG_FORMAT.parse(cols.get(i + 1))); } flat.getActivationFunctions()[index++] = af; } } } result.getStructure().setFlat(flat); result.updateProperties(); return result; }
@Override public Object read(final InputStream is) { long nextInnovationID = 0; long nextGeneID = 0; final NEATPopulation result = new NEATPopulation(); final NEATInnovationList innovationList = new NEATInnovationList(); innovationList.setPopulation(result); result.setInnovations(innovationList); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("NEAT-POPULATION") && section.getSubSectionName().equals("INNOVATIONS")) { for (final String line : section.getLines()) { final List<String> cols = EncogFileSection.splitColumns(line); final NEATInnovation innovation = new NEATInnovation(); final int innovationID = Integer.parseInt(cols.get(1)); innovation.setInnovationID(innovationID); innovation.setNeuronID(Integer.parseInt(cols.get(2))); result.getInnovations().getInnovations().put(cols.get(0), innovation); nextInnovationID = Math.max(nextInnovationID, innovationID + 1); } } else if (section.getSectionName().equals("NEAT-POPULATION") && section.getSubSectionName().equals("SPECIES")) { NEATGenome lastGenome = null; BasicSpecies lastSpecies = null; for (final String line : section.getLines()) { final List<String> cols = EncogFileSection.splitColumns(line); if (cols.get(0).equalsIgnoreCase("s")) { lastSpecies = new BasicSpecies(); lastSpecies.setPopulation(result); lastSpecies.setAge(Integer.parseInt(cols.get(1))); lastSpecies.setBestScore(CSVFormat.EG_FORMAT.parse(cols.get(2))); lastSpecies.setGensNoImprovement(Integer.parseInt(cols.get(3))); result.getSpecies().add(lastSpecies); } else if (cols.get(0).equalsIgnoreCase("g")) { final boolean isLeader = lastGenome == null; lastGenome = new NEATGenome(); lastGenome.setInputCount(result.getInputCount()); lastGenome.setOutputCount(result.getOutputCount()); lastGenome.setSpecies(lastSpecies); lastGenome.setAdjustedScore(CSVFormat.EG_FORMAT.parse(cols.get(1))); lastGenome.setScore(CSVFormat.EG_FORMAT.parse(cols.get(2))); lastGenome.setBirthGeneration(Integer.parseInt(cols.get(3))); lastSpecies.add(lastGenome); if (isLeader) { lastSpecies.setLeader(lastGenome); } } else if (cols.get(0).equalsIgnoreCase("n")) { final NEATNeuronGene neuronGene = new NEATNeuronGene(); final int geneID = Integer.parseInt(cols.get(1)); neuronGene.setId(geneID); final ActivationFunction af = EncogFileSection.parseActivationFunction(cols.get(2)); neuronGene.setActivationFunction(af); neuronGene.setNeuronType(PersistNEATPopulation.stringToNeuronType(cols.get(3))); neuronGene.setInnovationId(Integer.parseInt(cols.get(4))); lastGenome.getNeuronsChromosome().add(neuronGene); nextGeneID = Math.max(geneID + 1, nextGeneID); } else if (cols.get(0).equalsIgnoreCase("l")) { final NEATLinkGene linkGene = new NEATLinkGene(); linkGene.setId(Integer.parseInt(cols.get(1))); linkGene.setEnabled(Integer.parseInt(cols.get(2)) > 0); linkGene.setFromNeuronID(Integer.parseInt(cols.get(3))); linkGene.setToNeuronID(Integer.parseInt(cols.get(4))); linkGene.setWeight(CSVFormat.EG_FORMAT.parse(cols.get(5))); linkGene.setInnovationId(Integer.parseInt(cols.get(6))); lastGenome.getLinksChromosome().add(linkGene); } } } else if (section.getSectionName().equals("NEAT-POPULATION") && section.getSubSectionName().equals("CONFIG")) { final Map<String, String> params = section.parseParams(); final String afStr = params.get(NEATPopulation.PROPERTY_NEAT_ACTIVATION); if (afStr.equalsIgnoreCase(PersistNEATPopulation.TYPE_CPPN)) { HyperNEATGenome.buildCPPNActivationFunctions(result.getActivationFunctions()); } else { result.setNEATActivationFunction( EncogFileSection.parseActivationFunction( params, NEATPopulation.PROPERTY_NEAT_ACTIVATION)); } result.setActivationCycles( EncogFileSection.parseInt(params, PersistConst.ACTIVATION_CYCLES)); result.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT)); result.setOutputCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT)); result.setPopulationSize( EncogFileSection.parseInt(params, NEATPopulation.PROPERTY_POPULATION_SIZE)); result.setSurvivalRate( EncogFileSection.parseDouble(params, NEATPopulation.PROPERTY_SURVIVAL_RATE)); result.setActivationCycles( EncogFileSection.parseInt(params, NEATPopulation.PROPERTY_CYCLES)); } } // set factories if (result.isHyperNEAT()) { result.setGenomeFactory(new FactorHyperNEATGenome()); result.setCODEC(new HyperNEATCODEC()); } else { result.setGenomeFactory(new FactorNEATGenome()); result.setCODEC(new NEATCODEC()); } // set the next ID's result.getInnovationIDGenerate().setCurrentID(nextInnovationID); result.getGeneIDGenerate().setCurrentID(nextGeneID); // find first genome, which should be the best genome if (result.getSpecies().size() > 0) { final Species species = result.getSpecies().get(0); if (species.getMembers().size() > 0) { result.setBestGenome(species.getMembers().get(0)); } } return result; }
/** {@inheritDoc} */ @Override public Object read(final InputStream is) { final RBFNetwork result = new RBFNetwork(); final FlatNetworkRBF flat = (FlatNetworkRBF) result.getFlat(); final EncogReadHelper in = new EncogReadHelper(is); EncogFileSection section; while ((section = in.readNextSection()) != null) { if (section.getSectionName().equals("RBF-NETWORK") && section.getSubSectionName().equals("PARAMS")) { final Map<String, String> params = section.parseParams(); result.getProperties().putAll(params); } if (section.getSectionName().equals("RBF-NETWORK") && section.getSubSectionName().equals("NETWORK")) { final Map<String, String> params = section.parseParams(); flat.setBeginTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_BEGIN_TRAINING)); flat.setConnectionLimit( EncogFileSection.parseDouble(params, BasicNetwork.TAG_CONNECTION_LIMIT)); flat.setContextTargetOffset( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_OFFSET)); flat.setContextTargetSize( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_SIZE)); flat.setEndTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_END_TRAINING)); flat.setHasContext(EncogFileSection.parseBoolean(params, BasicNetwork.TAG_HAS_CONTEXT)); flat.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT)); flat.setLayerCounts(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_COUNTS)); flat.setLayerFeedCounts( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_FEED_COUNTS)); flat.setLayerContextCount( EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_CONTEXT_COUNT)); flat.setLayerIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_INDEX)); flat.setLayerOutput(section.parseDoubleArray(params, PersistConst.OUTPUT)); flat.setLayerSums(new double[flat.getLayerOutput().length]); flat.setOutputCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT)); flat.setWeightIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_WEIGHT_INDEX)); flat.setWeights(section.parseDoubleArray(params, PersistConst.WEIGHTS)); flat.setBiasActivation(section.parseDoubleArray(params, BasicNetwork.TAG_BIAS_ACTIVATION)); } else if (section.getSectionName().equals("RBF-NETWORK") && section.getSubSectionName().equals("ACTIVATION")) { int index = 0; flat.setActivationFunctions(new ActivationFunction[flat.getLayerCounts().length]); for (final String line : section.getLines()) { ActivationFunction af = null; final List<String> cols = EncogFileSection.splitColumns(line); final String name = "org.encog.engine.network.activation." + cols.get(0); try { final Class<?> clazz = Class.forName(name); af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e); } for (int i = 0; i < af.getParamNames().length; i++) { af.setParam(i, CSVFormat.EG_FORMAT.parse(cols.get(i + 1))); } flat.getActivationFunctions()[index++] = af; } } else if (section.getSectionName().equals("RBF-NETWORK") && section.getSubSectionName().equals("RBF")) { int index = 0; final int hiddenCount = flat.getLayerCounts()[1]; final int inputCount = flat.getLayerCounts()[2]; flat.setRBF(new RadialBasisFunction[hiddenCount]); for (final String line : section.getLines()) { RadialBasisFunction rbf = null; final List<String> cols = EncogFileSection.splitColumns(line); final String name = "org.encog.mathutil.rbf." + cols.get(0); try { final Class<?> clazz = Class.forName(name); rbf = (RadialBasisFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e); } rbf.setWidth(CSVFormat.EG_FORMAT.parse(cols.get(1))); rbf.setPeak(CSVFormat.EG_FORMAT.parse(cols.get(2))); rbf.setCenters(new double[inputCount]); for (int i = 0; i < inputCount; i++) { rbf.getCenters()[i] = CSVFormat.EG_FORMAT.parse(cols.get(i + 3)); } flat.getRBF()[index++] = rbf; } } } return result; }