source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/MORPH.java @ 114

Last change on this file since 114 was 86, checked in by sherbold, 9 years ago
  • switched workspace encoding to UTF-8 and fixed broken characters
  • Property svn:mime-type set to text/plain
File size: 5.5 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.dataprocessing;
16
17import java.security.InvalidParameterException;
18import java.util.Random;
19
20import org.apache.commons.collections4.list.SetUniqueList;
21import org.apache.commons.math3.util.MathArrays;
22
23import weka.core.Instance;
24import weka.core.Instances;
25
26/**
27 * Implements the MORPH data privatization.
28 *
29 *
30 * @author Steffen Herbold
31 */
32public class MORPH implements ISetWiseProcessingStrategy, IProcessesingStrategy {
33
34    /**
35     * random number generator for MORPH
36     */
37    Random rand = new Random();
38   
39    /**
40     * parameter alpha for MORPH, default is 0.15
41     */
42    double alpha = 0.15;
43   
44    /**
45     * parameter beta for MORPH, default is 0.35
46     */
47    double beta = 0.35;
48   
49    /**
50     * Does not have parameters. String is ignored.
51     *
52     * @param parameters
53     *            ignored
54     */
55    @Override
56    public void setParameter(String parameters) {
57        if (parameters != null && !parameters.equals("")) {
58            String[] values = parameters.split(" ");
59            if( values.length!=2 ) {
60                throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values");
61            }
62            try {
63                alpha = Double.parseDouble(values[0]);
64                beta = Double.parseDouble(values[1]);
65            } catch(NumberFormatException e) {
66                throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values");
67            }
68        }
69    }
70
71    /**
72     * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances,
73     *      org.apache.commons.collections4.list.SetUniqueList)
74     */
75    @Override
76    public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
77        for( Instances traindata : traindataSet ) {
78            applyMORPH(traindata);
79        }
80    }
81
82    /**
83     * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances,
84     *      weka.core.Instances)
85     */
86    @Override
87    public void apply(Instances testdata, Instances traindata) {
88        applyMORPH(traindata);
89    }
90   
91    /**
92     *
93     * <p>
94     * Applies MORPH to the data
95     * </p>
96     *
97     * @param data data to which the processor is applied
98     */
99    private void applyMORPH(Instances data) {
100        for (int i=0; i<data.numInstances(); i++ ) {
101            Instance instance = data.instance(i);
102            Instance nearestUnlikeNeighbor = getNearestUnlikeNeighbor(instance, data);
103            if( nearestUnlikeNeighbor==null ) {
104                throw new RuntimeException("could not find nearest unlike neighbor within the data: " + data.relationName());
105            }
106            for( int j=0; j<data.numAttributes() ; j++ ) {
107                if( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) {
108                    double randVal = rand.nextDouble()*(beta-alpha)+alpha;
109                    instance.setValue(j, instance.value(j) + randVal*(instance.value(j)-nearestUnlikeNeighbor.value(j)) );
110                }
111            }
112        }
113    }
114   
115    /**
116     * <p>
117     * Determines the nearest unlike neighbor of an instance.
118     * </p>
119     *
120     * @param instance instance to which the nearest unlike neighbor is determined
121     * @param data data where the nearest unlike neighbor is determined from
122     * @return nearest unlike instance
123     */
124    protected Instance getNearestUnlikeNeighbor(Instance instance, Instances data) {
125        Instance nearestUnlikeNeighbor = null;
126       
127        double[] instanceVector = new double[data.numAttributes()-1];
128        int tmp = 0;
129        for( int j=0; j<data.numAttributes(); j++ ) {
130            if( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) {
131                instanceVector[tmp] = instance.value(j);
132            }
133        }
134       
135        double minDistance = Double.MAX_VALUE;
136        for( int i=0 ; i<data.numInstances() ; i++ ) {
137            if( instance.classValue() != data.instance(i).classValue() ) {
138                double[] otherVector = new double[data.numAttributes() - 1];
139                tmp = 0;
140                for (int j = 0; j < data.numAttributes(); j++) {
141                    if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) {
142                        otherVector[tmp++] = data.instance(i).value(j);
143                    }
144                }
145                if( MathArrays.distance(instanceVector, otherVector)<minDistance) {
146                    minDistance = MathArrays.distance(instanceVector, otherVector);
147                    nearestUnlikeNeighbor = data.instance(i);
148                }
149            }
150        }
151        return nearestUnlikeNeighbor;
152    }
153}
Note: See TracBrowser for help on using the repository browser.