Advertisement
Guest User

jcobyla successful termination when constraints violated

a guest
Jan 14th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. import com.cureos.numerics.Calcfc;
  2. import com.cureos.numerics.Cobyla;
  3.  
  4. public class JCobylaTest {
  5.  
  6.   public static void main(String[] args) {
  7.    
  8.     double[] optimize = new double[8];
  9.     for (int i = 0; i < optimize.length; i++) {
  10.       optimize[i] = 1d/optimize.length;
  11.     }
  12.    
  13.     Cobyla.FindMinimum(new Calcfc() {
  14.      
  15.       @Override
  16.       public double Compute(int n, int m, double[] x, double[] con) {
  17.  
  18.         // ----- vars have to be non negative -----
  19.         for (int i = 0; i < x.length; i++) {
  20.           con[i] = x[i];
  21.         }
  22.         // ----- END -----
  23.         // ----- sum of vars have to be approximately 1 -----
  24.         double sum = 0;
  25.         for (int i = 0; i < x.length; i++) {
  26.           sum += x[i];
  27.         }
  28.         con[x.length] = sum - 0.9999;
  29.         con[x.length + 1] = 1.0001 - sum;
  30.         // ----- END -----
  31.         // ----- pnorm_1(matrix * x) <= 0.06651 -----
  32.         double[][] matrix = {{-0.25, -0.25, 0.75, 0.75, -0.25, -0.25, 0.75, 0.75},
  33.                              {0.0, -0.8, 0.0, 0.2, 0.0, -0.8, 0.0, 0.2},
  34.                              {0.0, 0.0, 0.0, 0.0, -0.6, -0.6, 0.4, 0.4},
  35.                              {0.0, 0.0, -0.7, -0.7, 0.0, 0.0, 0.3, 0.3},
  36.                              {0.0, 0.0, -0.5, 0.5, 0.0, 0.0, -0.5, 0.5},
  37.                              {-0.05, -0.05, 0.0, 0.0, 0.95, 0.95, 0.0, 0.0},
  38.                              {-0.01, 0.99, 0.0, 0.0, -0.01, 0.99, 0.0, 0.0}};
  39.         double[] matrix_m_x = new double[matrix.length];
  40.         sum = 0;
  41.         for (int i = 0; i < matrix.length; i++) {
  42.           for (int j = 0; j < matrix[i].length; j++) {
  43.             matrix_m_x[i] = matrix_m_x[i] + matrix[i][j] * x[j];
  44.           }
  45.           sum += Math.abs(matrix_m_x[i]);
  46.         }
  47.         con[x.length + 2] = 0.06651 - sum;
  48.         // ----- END -----
  49.         // ----- maximise entropy -----
  50.         double entropie = 0;
  51.         for (int i = 0; i < x.length; i++) {
  52.           double d = x[i];
  53.           if (Double.compare(d, 0) <= 0) {
  54.             continue;
  55.           }
  56.           entropie -= d *log2(d);
  57.         }
  58.         // ----- END -----
  59.        
  60.         return -entropie;
  61.       }
  62.      
  63.     }, 8, 11, optimize, /*1*/0.001, 1.0e-6, 3, 1000);
  64.   }
  65.  
  66.   private static double log2(double val) {
  67.     return Math.log10(val) / Math.log(2);
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement