00001
00004 package org.sci2s.eamhco;
00005
00006 import java.util.Collections.*;
00007 import java.util.*;
00008
00013 public class Prototype
00014 {
00016 boolean normalized = false;
00018 public static final int UNKNOW_INDEX = -1;
00020 protected int index = UNKNOW_INDEX;
00022 protected static int[] type;
00024 public static final int INTEGER = 0;
00026 public static final int DOUBLE = 1;
00028 public static final int NOMINAL = 2;
00029
00030 protected static double normalize(double min, double max, double value)
00031 {
00032 if(value <= min)
00033 return 0.0;
00034 return (value-min)/(max-min);
00035 }
00036
00037
00038
00043 public static int getTypeOfAttribute(int i)
00044 {
00045 return type[i];
00046 }
00047
00052 public int getIndex()
00053 {
00054 return index;
00055 }
00056
00061 public void setIndex(int index)
00062 {
00063 this.index = index;
00064 }
00065
00067 protected double[] inputs = null;
00068
00070 protected double[] outputs = null;
00071
00072
00073
00074
00075
00076
00077 public Prototype(){
00078 this.inputs = null;
00079 this.outputs = null;
00080 this.index = UNKNOW_INDEX;
00081 }
00082
00083
00088 public Prototype(int numInput, int numOutput){
00089 this.inputs = new double[numInput];
00090 this.outputs = new double[numOutput];
00091 this.index = UNKNOW_INDEX;
00092 }
00098 public Prototype(double[] inputs, double[] outputs)
00099 {
00100 this.inputs = inputs;
00101 this.outputs = outputs;
00102 this.index = UNKNOW_INDEX;
00103 }
00104
00105
00110 public Prototype(Prototype original)
00111 {
00112 this.inputs = Arrays.copyOf(original.inputs, original.inputs.length);
00113 this.outputs = Arrays.copyOf(original.outputs, original.outputs.length);
00114
00115 this.index = original.index;
00116 this.normalized = original.normalized;
00117 }
00118
00123 public void set(Prototype original)
00124 {
00125 this.inputs = Arrays.copyOf(original.inputs, original.inputs.length);
00126 this.outputs = Arrays.copyOf(original.outputs, original.outputs.length);
00127 this.normalized = original.normalized;
00128 }
00129
00134 public double[] getInputs()
00135 {
00136 return inputs;
00137 }
00138
00144 public double getInput(int i)
00145 {
00146 return inputs[i];
00147 }
00148
00149
00156 public void setInput(int i, double valor){
00157 inputs[i] =valor;
00158 }
00159
00164 public double[] getOutputs()
00165 {
00166 return inputs;
00167 }
00168
00174 public double getOutput(int i)
00175 {
00176 return outputs[i];
00177 }
00178
00183 public double firstOutput()
00184 {
00185 return outputs[0];
00186 }
00187
00192 public double label()
00193 {
00194 return firstOutput();
00195 }
00196
00201 public double assignedClass()
00202 {
00203 return firstOutput();
00204 }
00205
00210 public int numberOfInputs()
00211 {
00212 return inputs.length;
00213 }
00214
00219 public void setFirstOutput(double val)
00220 {
00221 outputs[0] = val;
00222 }
00223
00228 public void setClass(double val)
00229 {
00230 setFirstOutput(val);
00231 }
00232
00237 public void setLabel(double val)
00238 {
00239 setFirstOutput(val);
00240 }
00241
00246 public int numberOfOutputs()
00247 {
00248 return outputs.length;
00249 }
00250
00251
00258 public Prototype mul(Prototype other)
00259 {
00260 int numInputs = numberOfInputs();
00261 double[] _inputs = new double[numInputs];
00262 int numOutputs = numberOfOutputs();
00263 double[] _outputs = new double[numOutputs];
00264
00265 if(other.numberOfInputs() == this.numberOfInputs()){
00266 for(int i=0; i<numInputs; ++i)
00267 _inputs[i] = other.inputs[i]*(this.inputs[i]);
00268
00269 }
00270
00271 _outputs = Arrays.copyOf(this.outputs, this.outputs.length);
00272
00273 return new Prototype(_inputs, _outputs);
00274 }
00275
00282 public double mulEscalar(Prototype other)
00283 {
00284 int numInputs = numberOfInputs();
00285 double[] _inputs = new double[numInputs];
00286
00287
00288 double suma = 0;
00289 if(other.numberOfInputs() == this.numberOfInputs()){
00290 for(int i=0; i<numInputs; ++i){
00291 suma+= other.inputs[i]*(this.inputs[i]);
00292
00293 }
00294 }
00295
00296 return suma;
00297 }
00298
00299
00305 public Prototype mul(double weight)
00306 {
00307 int numInputs = numberOfInputs();
00308 double[] _inputs = new double[numInputs];
00309 int numOutputs = numberOfOutputs();
00310 double[] _outputs = new double[numOutputs];
00311
00312 for(int i=0; i<numInputs; ++i)
00313 _inputs[i] = weight*(this.inputs[i]);
00314
00315
00316
00317 _outputs = Arrays.copyOf(this.outputs, this.outputs.length);
00318
00319 return new Prototype(_inputs, _outputs);
00320 }
00321
00322
00323
00329 public Prototype sqrt()
00330 {
00331 int numInputs = numberOfInputs();
00332 double[] _inputs = new double[numInputs];
00333 int numOutputs = numberOfOutputs();
00334 double[] _outputs = new double[numOutputs];
00335
00336 for(int i=0; i<numInputs; ++i)
00337 _inputs[i] = Math.sqrt(this.inputs[i]);
00338
00339
00340
00341 _outputs = Arrays.copyOf(this.outputs, this.outputs.length);
00342
00343 return new Prototype(_inputs, _outputs);
00344 }
00345
00346
00351 public double module(){
00352 double result = 0.0;
00353
00354
00355 for(int i=0; i<this.inputs.length; i++){
00356 result += this.inputs[i] * this.inputs[i];
00357 }
00358
00359 return Math.sqrt(result);
00360 }
00361
00367 public Prototype add(double increment){
00368 Prototype p = new Prototype(this);
00369
00370 for(int i=0; i< p.inputs.length; i++){
00371 p.setInput(i, p.getInput(i) + increment);
00372 }
00373
00374 return p;
00375 }
00376
00382 public Prototype add(Prototype other)
00383 {
00384 int numInputs = numberOfInputs();
00385 double[] _inputs = new double[numInputs];
00386 int numOutputs = numberOfOutputs();
00387 double[] _outputs = new double[numOutputs];
00388
00389 for(int i=0; i<numInputs; ++i)
00390 _inputs[i] = this.inputs[i] + other.inputs[i];
00391
00392
00393
00394
00395 _outputs = Arrays.copyOf(this.outputs, this.outputs.length);
00396
00397 return new Prototype(_inputs, _outputs);
00398 }
00399
00406
00407 public Prototype addMul(Prototype other, double weight)
00408 {
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 return (this.add(other)).mul(weight);
00423 }
00424
00431 public Prototype addDiv(Prototype other, double divisor)
00432 {
00433 double weight = 1.0/divisor;
00434 return addMul(other, weight);
00435 }
00436
00437
00438
00439
00445 public Prototype avg(Prototype other)
00446 {
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 return addMul(other,0.5);
00460 }
00461
00468 public static Prototype avg(Prototype p1, Prototype p2)
00469 {
00470 return p1.avg(p2);
00471 }
00472
00481 public static Prototype avg(Prototype p1, double w1, Prototype p2, double w2)
00482 {
00483 Prototype averaged = new Prototype(p1);
00484 double denominator = 1.0/(w1+w2);
00485 int numInputs = p1.numberOfInputs();
00486 for(int i=0; i<numInputs; ++i)
00487 {
00488 averaged.inputs[i] = w1*p1.inputs[i] + w2*p2.inputs[i];
00489 averaged.inputs[i] *= denominator;
00490 }
00491 return averaged;
00492 }
00493
00499 public Prototype sub(Prototype other)
00500 {
00501 int numInputs = numberOfInputs();
00502 double[] _inputs = new double[numInputs];
00503 int numOutputs = numberOfOutputs();
00504 double[] _outputs = new double[numOutputs];
00505
00506 for(int i=0; i<numInputs; ++i)
00507 _inputs[i] = this.inputs[i] - other.inputs[i];
00508
00509
00510 _outputs = Arrays.copyOf(this.outputs, this.outputs.length);
00511
00512 return new Prototype(_inputs, _outputs);
00513 }
00514
00521
00522 public Prototype subMul(Prototype other, double weight)
00523 {
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 return (this.sub(other)).mul(weight);
00535 }
00536
00541 @Override
00542 public String toString()
00543 {
00544 String result = "";
00545 int nInputs = numberOfInputs();
00546 int nOutputs = numberOfOutputs();
00547
00548 for(int i=0; i<nInputs; ++i)
00549 result += inputs[i] + " ";
00550 for(int i=0; i<nOutputs; ++i)
00551 result += outputs[i] + " ";
00552 return result;
00553 }
00554
00555 protected static double round(double value)
00556 {
00557 String s = Double.toString(value);
00558 String[] comma = s.split("\\.");
00559 if (comma.length > 1)
00560 {
00561 int pos1, pos2;
00562 if (comma[1].indexOf("E") < 0)
00563 {
00564 pos1 = comma[1].indexOf("0000");
00565 pos2 = comma[1].indexOf("9999");
00566 if (pos1 >= 0) {
00567 comma[1] = comma[1].substring(0,pos1);
00568 if (comma[1].length() == 0) comma[1] = "0";
00569 } else if (pos2 >= 0) {
00570 comma[1] = comma[1].substring(0,pos2);
00571
00572 if (comma[1].length() == 0) {
00573 comma[1] = "0";
00574 int redondo = Integer.parseInt(comma[0]);
00575 redondo++;
00576 comma[0] = String.valueOf(redondo);
00577 } else {
00578 long redondo = Long.parseLong(comma[1].substring(comma[1].length()-1));
00579 redondo++;
00580 comma[1] = comma[1].substring(0,comma[1].length()-1) + String.valueOf(redondo);
00581 }
00582 }
00583 }
00584 return Double.valueOf(comma[0] + "." + comma[1]);
00585 }
00586 return Double.valueOf(comma[0]);
00587 }
00588
00589
00590
00591
00592
00593
00599 public boolean equals(Prototype other)
00600 {
00601 return Arrays.equals(inputs, other.inputs) && Arrays.equals(outputs, other.outputs);
00602 }
00603
00604
00610 public boolean equalsInputs(Prototype other)
00611 {
00612 return Arrays.equals(inputs, other.inputs);
00613 }
00614
00615
00621 public void applyThresholds()
00622 {
00623 int nInputs = numberOfInputs();
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 for(int i=0; i<nInputs; ++i)
00636 {
00637 if(inputs[i]>1)
00638 inputs[i] = 1;
00639 else if(inputs[i]< 0)
00640 inputs[i] = 0;
00641
00642 }
00643 }
00644
00648
00649
00650
00651
00652
00653
00654
00655
00656
00660 public void makeNull()
00661 {
00662 int nInputs = numberOfInputs();
00663 int nOutputs = numberOfOutputs();
00664
00665 for(int i=0; i<nInputs; ++i)
00666 inputs[i] = 0.0;
00667 for(int i=0; i<nOutputs; ++i)
00668 outputs[i] = 0.0;
00669 }
00670
00674 public void print(){
00675 System.out.print("\n");
00676
00677 for (int i =0; i<this.inputs.length; i++){
00678
00679 System.out.print(inputs[i] + " ");
00680
00681 }
00682
00683 System.out.print(this.outputs[0]+"\n");
00684
00685
00686 }
00687
00688
00694 public Prototype opposite(){
00695 Prototype opuesto = new Prototype(this);
00696
00697 for (int i=0; i< this.inputs.length; i++){
00698 opuesto.inputs[i] = 1-this.inputs[i];
00699 }
00700
00701 return opuesto;
00702 }
00703
00704
00705
00711 public Prototype opposite(double a, double b){
00712 Prototype opuesto = new Prototype(this);
00713
00714 for (int i=0; i< this.inputs.length; i++){
00715 opuesto.inputs[i] = a+b-this.inputs[i];
00716 }
00717
00718 return opuesto;
00719 }
00720
00721 public Prototype opposite(double a[], double b[]){
00722 Prototype opuesto = new Prototype(this);
00723
00724 for (int i=0; i< this.inputs.length; i++){
00725 opuesto.inputs[i] = a[i]+b[i]-this.inputs[i];
00726 }
00727
00728 return opuesto;
00729 }
00730
00731
00732
00733
00734 }