| 1 | from scikits.openopt import NLP |
|---|
| 2 | from numpy import cos, arange, ones, asarray, abs, zeros |
|---|
| 3 | N = 30 |
|---|
| 4 | M = 5 |
|---|
| 5 | ff = lambda x: ((x-M)**2).sum() |
|---|
| 6 | p = NLP(ff, cos(arange(N))) |
|---|
| 7 | |
|---|
| 8 | def df(x): |
|---|
| 9 | r = 2*(x-M) |
|---|
| 10 | r[0] += 15 #incorrect derivative |
|---|
| 11 | r[8] += 8 #incorrect derivative |
|---|
| 12 | return r |
|---|
| 13 | p.df = df |
|---|
| 14 | |
|---|
| 15 | p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8] |
|---|
| 16 | |
|---|
| 17 | def dc(x): |
|---|
| 18 | r = zeros((2, p.n)) |
|---|
| 19 | r[0,0] = 2 * 4 * x[0]**3 |
|---|
| 20 | r[1,1] = 2 * x[1] |
|---|
| 21 | r[1,2] = 2 * x[2] + 15 #incorrect derivative |
|---|
| 22 | return r |
|---|
| 23 | p.dc = dc |
|---|
| 24 | |
|---|
| 25 | h1 = lambda x: 1e1*(x[-1]-1)**4 |
|---|
| 26 | h2 = lambda x: (x[-2]-1.5)**4 |
|---|
| 27 | p.h = (h1, h2) |
|---|
| 28 | |
|---|
| 29 | def dh(x): |
|---|
| 30 | r = zeros((2, p.n)) |
|---|
| 31 | r[0,-1] = 1e1*4*(x[-1]-1)**3 |
|---|
| 32 | r[1,-2] = 4*(x[-2]-1.5)**3 + 15 #incorrect derivative |
|---|
| 33 | return r |
|---|
| 34 | p.dh = dh |
|---|
| 35 | |
|---|
| 36 | p.checkdf() |
|---|
| 37 | p.checkdc() |
|---|
| 38 | p.checkdh() |
|---|
| 39 | """ |
|---|
| 40 | you can use p.checkdF(x) for other point than x0 (F is f, c or h) |
|---|
| 41 | p.checkdc(myX) |
|---|
| 42 | or |
|---|
| 43 | p.checkdc(x=myX) |
|---|
| 44 | values with difference greater than |
|---|
| 45 | maxViolation (default 1e-5) |
|---|
| 46 | will be shown |
|---|
| 47 | p.checkdh(maxViolation=1e-4) |
|---|
| 48 | p.checkdh(myX, maxViolation=1e-4) |
|---|
| 49 | p.checkdh(x=myX, maxViolation=1e-4) |
|---|
| 50 | |
|---|
| 51 | ################################################################################# |
|---|
| 52 | Typical output (unfortunately, in terminal or other IDEs the blank space used in strings separation can have other lengths): |
|---|
| 53 | Note that RD (relative difference) is defined as |
|---|
| 54 | int(ceil(log10(abs(Diff) / maxViolation + 1e-150))) |
|---|
| 55 | where |
|---|
| 56 | Diff = 1 - (info_user+1e-8)/(info_numerical + 1e-8) |
|---|
| 57 | |
|---|
| 58 | OpenOpt checks user-supplied gradient df (shape: (30,) ) |
|---|
| 59 | according to: |
|---|
| 60 | prob.diffInt = [ 1.00000000e-07] |
|---|
| 61 | |1 - info_user/info_numerical| <= prob.maxViolation = 0.01 |
|---|
| 62 | df num user-supplied numerical RD |
|---|
| 63 | 0 +7.000e+00 -8.000e+00 3 |
|---|
| 64 | 8 -2.291e+00 -1.029e+01 2 |
|---|
| 65 | max(abs(df_user - df_numerical)) = 14.9999995251 |
|---|
| 66 | (is registered in df number 0) |
|---|
| 67 | ======================== |
|---|
| 68 | OpenOpt checks user-supplied gradient dc (shape: (2, 30) ) |
|---|
| 69 | according to: |
|---|
| 70 | prob.diffInt = [ 1.00000000e-07] |
|---|
| 71 | |1 - info_user/info_numerical| <= prob.maxViolation = 0.01 |
|---|
| 72 | dc num i,j:dc[i]/dx[j] user-supplied numerical RD |
|---|
| 73 | 32 1 / 2 +1.417e+01 -8.323e-01 4 |
|---|
| 74 | max(abs(dc_user - dc_numerical)) = 14.9999999032 |
|---|
| 75 | (is registered in dc number 32) |
|---|
| 76 | ======================== |
|---|
| 77 | OpenOpt checks user-supplied gradient dh (shape: (2, 30) ) |
|---|
| 78 | according to: |
|---|
| 79 | prob.diffInt = [ 1.00000000e-07] |
|---|
| 80 | |1 - info_user/info_numerical| <= prob.maxViolation = 0.01 |
|---|
| 81 | dh num i,j:dh[i]/dx[j] user-supplied numerical RD |
|---|
| 82 | 58 1 / 28 -4.474e+01 -5.974e+01 2 |
|---|
| 83 | max(abs(dh_user - dh_numerical)) = 14.9999962441 |
|---|
| 84 | (is registered in dh number 58) |
|---|
| 85 | ======================== |
|---|
| 86 | |
|---|
| 87 | """ |
|---|