1/* 2 * Copyright (C) 2012 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 17package android.bordeaux.services; 18 19import android.bordeaux.learning.MulticlassPA; 20import android.os.IBinder; 21 22import java.util.List; 23import java.util.ArrayList; 24 25public class Learning_MulticlassPA extends ILearning_MulticlassPA.Stub 26 implements IBordeauxLearner { 27 private MulticlassPA mMulticlassPA_learner; 28 private ModelChangeCallback modelChangeCallback = null; 29 30 class IntFloatArray { 31 int[] indexArray; 32 float[] floatArray; 33 }; 34 35 private IntFloatArray splitIntFloatArray(List<IntFloat> sample) { 36 IntFloatArray splited = new IntFloatArray(); 37 ArrayList<IntFloat> s = (ArrayList<IntFloat>)sample; 38 splited.indexArray = new int[s.size()]; 39 splited.floatArray = new float[s.size()]; 40 for (int i = 0; i < s.size(); i++) { 41 splited.indexArray[i] = s.get(i).index; 42 splited.floatArray[i] = s.get(i).value; 43 } 44 return splited; 45 } 46 47 public Learning_MulticlassPA() { 48 mMulticlassPA_learner = new MulticlassPA(2, 2, 0.001f); 49 } 50 51 // Beginning of the IBordeauxLearner Interface implementation 52 public byte [] getModel() { 53 return null; 54 } 55 56 public boolean setModel(final byte [] modelData) { 57 return false; 58 } 59 60 public IBinder getBinder() { 61 return this; 62 } 63 64 public void setModelChangeCallback(ModelChangeCallback callback) { 65 modelChangeCallback = callback; 66 } 67 // End of IBordeauxLearner Interface implemenation 68 69 // This implementation, combines training and prediction in one step. 70 // The return value is the prediction value for the supplied sample. It 71 // also update the model with the current sample. 72 public void TrainOneSample(List<IntFloat> sample, int target) { 73 IntFloatArray splited = splitIntFloatArray(sample); 74 mMulticlassPA_learner.sparseTrainOneExample(splited.indexArray, 75 splited.floatArray, 76 target); 77 if (modelChangeCallback != null) { 78 modelChangeCallback.modelChanged(this); 79 } 80 } 81 82 public int Classify(List<IntFloat> sample) { 83 IntFloatArray splited = splitIntFloatArray(sample); 84 int prediction = mMulticlassPA_learner.sparseGetClass(splited.indexArray, 85 splited.floatArray); 86 return prediction; 87 } 88 89} 90