summaryrefslogtreecommitdiffstats
path: root/src/experiments/RunPredictionServer.java
blob: 6612a8675861b721631e9f6f88431d37e0439e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package experiments;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import weka.filters.Filter;
import model.Model;
import org.apache.log4j.Logger;
import weka.core.Instances;
import weka.core.OptionHandler;

/**
 * Created by hailiu on 2016/2/22.
 */
public class RunPredictionServer {
    protected Logger logger = Logger.getLogger(RunPredictionServer.class);

    final String configFile = "data/config.txt";//input file path
    String[] option = new String[50];
    String filter;
    ArrayList filterList;
    String DataFile;
    protected static DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
    protected static Date date = new Date();
    protected static String resultFilename = dateFormat.format(date)+"_results";
    Map map;

    public RunPredictionServer() {
        this.filterList = new ArrayList();
        setClassName();
    }

    public static void main(String[] args)
    {
        RunPredictionServer runServer = new RunPredictionServer();
        //read config file
        runServer.loadConfigFile();
        Model tempModel = new Model();
        tempModel.loadTrainingData(runServer.DataFile);
        tempModel.addPredictor("ZEROR");
        tempModel.addPredictor("PART");
        tempModel.addPredictor("ONER");
        tempModel.addPredictor("JRIP");
        tempModel.addPredictor("IBK");
        tempModel.addPredictor("NBM");
        tempModel.addPredictor("RF");
        tempModel.addPredictor("LWL");
        tempModel.addPredictor("NBC");
        tempModel.addPredictor("BN");
        tempModel.addPredictor("REPTREE");
        tempModel.addPredictor("DT");
        tempModel.addPredictor("J48");
        tempModel.addPredictor("SMO");
        tempModel.addPredictor("MP");
        tempModel.addPredictor("SL");
        tempModel.addPredictor("LOG");
        tempModel.addPredictor("SGD");
        tempModel.addPredictor("VP");
        tempModel.addPredictor("SVM");
        tempModel.addPredictor("KSTAR");

        Instances tempInstances=tempModel.getTrainingInstances();

        Iterator ite=runServer.filterList.iterator();

        while (ite.hasNext())
        {
            ArrayList filterOption=(ArrayList) ite.next();
            Iterator tempIte = filterOption.iterator();
            String tFilter=null;
            if (tempIte.hasNext())
            {
                tFilter=(String) tempIte.next();
            }
            String[] option = new String[filterOption.size()-1];
            int i=0;
            while (tempIte.hasNext())
            {
                option[i]=(String) tempIte.next();
                i++;
            }
            tempInstances = runServer.addFilter(tempInstances,tFilter,option);
        }

        //tempInstances.setClassIndex(tempInstances.numAttributes()-2);
        tempModel.setPreprocessedInstances(tempInstances);
        tempModel.savePreprocessedInstances("preprocessed.arff");

        try {
            tempModel.benchmark(2,resultFilename);

        }catch (Exception e)
        {
            runServer.logger.warn(e.toString());
        }

        tempModel.crossValidatePredictors(10);
        try {
            tempModel.saveResults(resultFilename);
        }catch (Exception e)
        {
            runServer.logger.warn(e.toString());
        }
    }

    //read config file
    private void loadConfigFile() {
        logger.debug("Reading configure from " + configFile);
        try {
            BufferedReader in = new BufferedReader(new FileReader(configFile));
            DataFile = in.readLine();
            System.out.println(DataFile);

            String tempStr=null;
            while((tempStr=in.readLine())!=null)
            {
                String[] tempString = tempStr.split(" ");
                ArrayList<String> filterOption = new ArrayList<>();
                for(String ts:tempString)
                {
                    filterOption.add(ts);
                }
                this.filterList.add(filterOption);
            }
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    private Instances addFilter(Instances tempInstances,String filter, String[] option)
    {
        Class<?> tempClass=null;
        try {
            tempClass = Class.forName(this.map.get(filter).toString());
        }catch (ClassNotFoundException e)
        {
            logger.warn(e.toString());
        }

        if(tempClass==null) return null;
        Object tempFilter=null;
        try {
            tempFilter = tempClass.newInstance();
        }catch (InstantiationException e)
        {
            logger.warn(e.toString());
        }
        catch (IllegalAccessException e)
        {
            logger.warn(e.toString());
        }

        try {
            ((OptionHandler)tempFilter).setOptions(option);
            ((Filter)tempFilter).setInputFormat(tempInstances);
        }catch (Exception e)
        {
            logger.warn(e.toString());
        }

        Instances newInstances=null;
        try {
            newInstances = Filter.useFilter(tempInstances, (Filter) (tempFilter));
        }catch (Exception e)
        {
            logger.warn(e.toString());
        }
        return newInstances;
    }

    private void setClassName()
    {
        map=new HashMap();
        map.put("addexpression","weka.filters.unsupervised.attribute.AddExpression");
        map.put("remove","weka.filters.unsupervised.attribute.Remove");
        map.put("classassigner","weka.filters.unsupervised.attribute.ClassAssigner");
        map.put("numerictonominal","weka.filters.unsupervised.attribute.NumericToNominal");
        map.put("mathexpression","weka.filters.unsupervised.attribute.MathExpression");
    }
}