Package org.cspoker.ai.opponentmodels.weka.instances

Source Code of org.cspoker.ai.opponentmodels.weka.instances.InstancesBuilder

/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.cspoker.ai.opponentmodels.weka.instances;

import java.io.StringReader;

import org.apache.log4j.Logger;
import org.cspoker.ai.opponentmodels.weka.Propositionalizer;

import weka.core.Instance;
import weka.core.Instances;

public abstract class InstancesBuilder {

  private static final Logger logger = Logger.getLogger(InstancesBuilder.class);

  public final static String nl = "\n";

  public final int length;

  protected final Instances dataset;

  public InstancesBuilder(String name, String attributes, String targets) {
    String header =
      "@relation "+name+nl+
      attributes+
      targets+
      "@data"+nl;
    this.length = attributes.split(nl).length+targets.split(nl).length;

    try {
      this.dataset = new Instances(new StringReader(header));
    } catch (Exception e) {
      logger.error(e);
      e.printStackTrace();
      throw new IllegalStateException(e);
    }
    dataset.setClassIndex((length>0?length-1:0));
  }

  public abstract Instance getUnclassifiedInstance(Propositionalizer prop, Object actorId);

  public void addClassifiedInstance(Propositionalizer prop, Object actorId, Object[] targets){
    Instance instance = getClassifiedInstance(prop, actorId, targets);
    dataset.add(instance);
  }

  public Instance getClassifiedInstance(Propositionalizer prop,
      Object actorId, Object[] targets) {
    Instance instance = getUnclassifiedInstance(prop, actorId);
    for(int i=0; i<targets.length;i++){
      //dirty, blame weka
      if(targets[i] instanceof Number){
        instance.setValue(length-targets.length+i, ((Number)targets[i]).doubleValue());
      }else{
        instance.setValue(length-targets.length+i, (String)targets[i]);
      }
    }
    return instance;
  }

  @Override
  public String toString() {
    return dataset.toString();
  }
 
}
TOP

Related Classes of org.cspoker.ai.opponentmodels.weka.instances.InstancesBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.