StochasticLinearRanker.java revision 6b4eebc73439cbc3ddfb547444a341d1f9be7996
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.bordeaux.learning;
19import android.util.Log;
20import java.util.List;
21import java.util.Arrays;
22import java.util.ArrayList;
23
24/**
25 * Stochastic Linear Ranker, learns how to rank a sample. The learned rank score
26 * can be used to compare samples.
27 * This java class wraps the native StochasticLinearRanker class.
28 * To update the ranker, call updateClassifier with two samples, with the first
29 * one having higher rank than the second one.
30 * To get the rank score of the sample call scoreSample.
31 *  TODO: adding more interfaces for changing the learning parameters
32 */
33public class StochasticLinearRanker {
34    String TAG = "StochasticLinearRanker";
35    static int VAR_NUM = 15;
36    public StochasticLinearRanker() {
37        mNativeClassifier = initNativeClassifier();
38    }
39
40    /**
41     * Train the ranker with a pair of samples. A sample,  a pair of arrays of
42     * keys and values. The first sample should have higher rank than the second
43     * one.
44     */
45    public boolean updateClassifier(String[] keys_positive,
46                                    float[] values_positive,
47                                    String[] keys_negative,
48                                    float[] values_negative) {
49        return nativeUpdateClassifier(keys_positive, values_positive,
50                                      keys_negative, values_negative,
51                                      mNativeClassifier);
52    }
53
54    /**
55     * Get the rank score of the sample, a sample is a list of key, value pairs..
56     */
57    public float scoreSample(String[] keys, float[] values) {
58        return nativeScoreSample(keys, values, mNativeClassifier);
59    }
60
61    /**
62     * Get the current model and parameters of ranker
63     */
64    public void getModel(ArrayList keys_list, ArrayList values_list, ArrayList param_list){
65        int len = nativeGetLengthClassifier(mNativeClassifier);
66        String[] keys = new String[len];
67        float[] values = new float[len];
68        float[] param = new float[VAR_NUM];
69        nativeGetClassifier(keys, values, param, mNativeClassifier);
70        boolean add_flag;
71        for (int  i=0; i< keys.length ; i++){
72            add_flag = keys_list.add(keys[i]);
73            add_flag = values_list.add(values[i]);
74        }
75        for (int  i=0; i< param.length ; i++)
76            add_flag = param_list.add(param[i]);
77    }
78
79    /**
80     * use the given model and parameters for ranker
81     */
82    public boolean loadModel(String [] keys, float[] values, float[] param){
83        return nativeLoadClassifier(keys, values, param, mNativeClassifier);
84    }
85
86    @Override
87    protected void finalize() throws Throwable {
88        deleteNativeClassifier(mNativeClassifier);
89    }
90
91    static {
92        System.loadLibrary("bordeaux");
93    }
94
95    private int mNativeClassifier;
96
97    /*
98     * The following methods are the java stubs for the jni implementations.
99     */
100    private native int initNativeClassifier();
101
102    private native void deleteNativeClassifier(int classifierPtr);
103
104    private native boolean nativeUpdateClassifier(
105            String[] keys_positive,
106            float[] values_positive,
107            String[] keys_negative,
108            float[] values_negative,
109            int classifierPtr);
110
111    private native float nativeScoreSample(String[] keys,
112                                           float[] values,
113                                           int classifierPtr);
114    private native void nativeGetClassifier(String [] keys, float[] values, float[] param,
115                                             int classifierPtr);
116    private native boolean nativeLoadClassifier(String [] keys, float[] values,
117                                                 float[] param, int classifierPtr);
118    private native int nativeGetLengthClassifier(int classifierPtr);
119}
120