-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirect.m
More file actions
756 lines (694 loc) · 28.2 KB
/
Copy pathDirect.m
File metadata and controls
756 lines (694 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
function [ret_minval,final_xatmin,history] = Direct...
(Problem,bounds,opts,varargin)
% Function : Direct Version 4.0
% Written by : Dan Finkel (definkel@unity.ncsu.edu)
% Created on : 01/27/2003
% Last Update: 06/21/2004
% Purpose : Direct optimization algorithm.
%
% This code comes with no guarantee or warranty of any kind
%
% If unfamiliar with DIRECT, user is recommended to manual
% that accompanies this code, and can be found at:
% www4.ncsu.edu/~definkel/research/index.html
%
%
% [x,fmin,history] = Direct(Problem,bounds,opts,varargin)
%
% Parameters:
% IN: Problem - Structure containing problem
% Problem.f = Objective function handle
%
% NOTE: If you problem has no constraints (other than
% those on the bounds, this is the only field you
% need to add to Problem
%
% Problem.numconstraints = number of constraints
% Problem.constraint(i).func = i-th constraint handle
% Problem.constraint(i).penalty = penalty parameter for
% i-th constraint
% Note: If f returns objective function AND constraints
% then set Problem.constraint(1).func = Problem.f
% bounds - an n x 2 vector of the lower and upper bounds.
% The first column is the lower bounds, and the second
% column contains the upper bounds
% opts - (optional) MATLAB structure.
% opts.ep = Jones factor (default is 1e-4)
% opts.maxevals = max. number of function evals (default is 20)
% opts.maxits = max. number of iterations (default is 10)
% opts.maxdeep = max. number of rect. divisions (default is 100)
% opts.testflag = 1 if globalmin known, 0 otherwise (default is 0)
% opts.showits = 1 if disp. stats shown, 0 oth.
% (default is 1)
% opts.globalmin = globalmin (if known)
% (default is 0)
% opts.tol = tolerance for term. if tflag=1
% (default is 0.01)
% opts.impcons = turns on implicit constraint capability
% (default is 0)
% If set to one, objective function
% is expected to return a flag which represents
% the feasibility of the point sampled
% varargin - (optional) additional arguements to be passed to
% objective function
%
% NOTE: If opts.tflag == 0, maxevals, maxevals and maxdeep are ignored.
% DIRECT will stop when the absolute error is less
% than tol. Also, preallocation will not occur, and the algorithm
% can run slower than if opts.tflag == 1
% NOTE: opts.maxevals is an approximate stopping condition. DIRECT will
% exceed this budget by a slight amount
%
% OUT: minval - minimum value found
% xatmin - (optional) location of minimal value
% history - (optional) array of iteration historyory, useful for tables and plots
% The three columns are iteration, fcn evals, and min value found.
%
% Direct may be called by
%
% minval = Direct(Problem,bounds);
% or
% with any variation of the optional arguments
%------------------------------------------------------------------%
%
% Implementation taken from:
% D.R. Jones, C.D. Perttunen, and B.E. Stuckman. "Lipschitzian
% Optimization Without the Lipschitz Constant". Journal of
% Optimization Theory and Application, 79(1):157-181, October 1993
%
%------------------------------------------------------------------%
%-- Initialize the variables --------------------------------------%
lengths = [];c = [];fc = [];
con = [];szes = [];feas_flags=[];
om_lower = bounds(:,1);
om_upper = bounds(:,2);
fcncounter = 0;
perror = 0;
itctr = 1;
done = 0;
g_nargout = nargout;
n = size(bounds,1);
% Determine option values
if nargin<3, opts=[]; end
if (nargin>=3) & (length(opts)==0), opts=[]; end
getopts(opts, ...
'maxits', 20,... % maximum of iterations
'maxevals', 10,... % maximum # of function evaluations
'maxdeep', 100,... % maximum number of side divisions
'testflag', 0,... % terminate if within a relative tolerence of f_opt
'globalmin', 0,... % minimum value of function
'ep', 1e-4,... % global/local weight parameter.
'tol', 0.01,... % allowable relative error if f_reach is set
'showits', 1,... % print iteration stats
'impcons', 0,... % flag for using implicit constraint handling
'pert', 1e-6); % pertubation for implicit constraint handling
% 'maxflag', 0, ... % set to 1 for max problems, 0 for min problems
% 'sizeconst', 0.5,... % constant on rectangle size function
% 'distance', 1,... % 1/0 for distance/volume measure of size
% 'minlength', 1e-4,... % stop if best rectangle has all sides 1ess than this
% 'minevals', 0,... % but must evaluate at least this many points
theglobalmin = globalmin;
tflag = testflag;
%-- New 06/08/2004 Pre-allocate memory for storage vectors
if tflag == 0
lengths = zeros(n,maxevals + floor(.10*maxevals));
c = lengths;
fc = zeros(1,maxevals + floor(.10*maxevals));
szes = fc;
con = fc;
feas_flags = fc;
end
%-- Call DIRini ---------------------------------------------------%
[thirds , lengths, c , fc, con, feas_flags minval,xatmin,perror,...
history,szes,fcncounter,calltype] =...
DIRini(Problem,n,bounds(:,1),bounds(:,2),...
lengths,c,fc,con, feas_flags, szes,...
theglobalmin,maxdeep,tflag,g_nargout, impcons, varargin{:});
ret_minval = minval;
ret_xatmin = xatmin;
%-- MAIN LOOP -----------------------------------------------------%
minval = fc(1) + con(1);
while perror > tol
%-- Create list S of potentially optimal hyper-rectangles
S = find_po(fc(1:fcncounter)+con(1:fcncounter),...
lengths(:,1:fcncounter),minval,ep,szes(1:fcncounter));
%-- Loop through the potentially optimal hrectangles -----------%
%-- and divide -------------------------------------------------%
for i = 1:size(S,2)
[lengths,fc,c,con,feas_flags,szes,fcncounter,success] = ...
DIRdivide(bounds(:,1),bounds(:,2),Problem,S(1,i),thirds,lengths,...
fc,c,con,feas_flags,fcncounter,szes,impcons,calltype,varargin{:});
end
%-- update minval, xatmin --------------------------------------%
[minval,fminindex] = min(fc(1:fcncounter)+con(1:fcncounter));
penminval = minval + con(fminindex);
xatmin = (om_upper - om_lower).*c(:,fminindex) + om_lower;
if (con(fminindex) > 0)|(feas_flags(fminindex) ~= 0)
%--- new minval is infeasible, don't do anything
else
%--- update return values
ret_minval = minval;
ret_xatmin = xatmin;
end
%--see if we are done ------------------------------------------%
if tflag == 1
%-- Calculate error if globalmin known
if theglobalmin ~= 0
perror = 100*(minval - theglobalmin)/abs(theglobalmin);
else
perror = 100*minval;
end
else
%-- Have we exceeded the maxits?
if itctr >= maxits
if showits == 1
disp('Exceeded max iterations. Increase maxits')
end
done = 1;
end
%-- Have we exceeded the maxevals?
if fcncounter > maxevals
if showits == 1
disp('Exceeded max fcn evals. Increase maxevals')
end
done = 1;
end
if done == 1
perror = -1;
end
end
if max(max(lengths)) >= maxdeep
%-- We've exceeded the max depth
if showits == 1
disp('Exceeded Max depth. Increse maxdeep')
end
perror = -1;
end
if g_nargout == 3
%-- Store History
maxhist = size(history,1);
history(maxhist+1,1) = itctr;
history(maxhist+1,2) = fcncounter;
history(maxhist+1,3) = minval;
end
%-- New, 06/09/2004
%-- Call replaceinf if impcons flag is set to 1
if impcons == 1
fc = replaceinf(lengths(:,1:fcncounter),c(:,1:fcncounter),...
fc(1:fcncounter),con(1:fcncounter),...
feas_flags(1:fcncounter),pert);
end
%-- show iteration stats
if showits == 1
if (con(fminindex) > 0) | (feas_flags(fminindex) == 1)
fprintf('Iter: %4i f_min: %15.10f* fn evals: %8i\n',...
itctr,minval,fcncounter);
else
fprintf('Iter: %4i f_min: %15.10f fn evals: %8i\n',...
itctr,minval,fcncounter);
end
end
itctr = itctr + 1;
end
%-- Return values
if g_nargout == 2
%-- return x*
final_xatmin = ret_xatmin;
elseif g_nargout == 3
%-- return x*
final_xatmin = ret_xatmin;
%-- chop off 1st row of history
history(1:size(history,1)-1,:) = history(2:size(history,1),:);
history = history(1:size(history,1)-1,:);
end
return
%------------------------------------------------------------------%
% Function: DIRini %
% Written by: Dan Finkel %
% Created on: 10/19/2002 %
% Purpose : Initialization of Direct %
% to eliminate storing floating points %
%------------------------------------------------------------------%
function [l_thirds,l_lengths,l_c,l_fc,l_con, l_feas_flags, minval,xatmin,perror,...
history,szes,fcncounter,calltype] = DIRini(Problem,n,a,b,...
p_lengths,p_c,p_fc,p_con, p_feas_flags, p_szes,theglobalmin,...
maxdeep,tflag,g_nargout,impcons,varargin)
l_lengths = p_lengths;
l_c = p_c;
l_fc = p_fc;
l_con = p_con;
l_feas_flags = p_feas_flags;
szes = p_szes;
%-- start by calculating the thirds array
%-- here we precalculate (1/3)^i which we will use frequently
l_thirds(1) = 1/3;
for i = 2:maxdeep
l_thirds(i) = (1/3)*l_thirds(i-1);
end
%-- length array will store # of slices in each dimension for
%-- each rectangle. dimension will be rows; each rectangle
%-- will be a column
%-- first rectangle is the whole unit hyperrectangle
l_lengths(:,1) = zeros(n,1);
%01/21/04 HACK
%-- store size of hyperrectangle in vector szes
szes(1,1) = 1;
%-- first element of c is the center of the unit hyperrectangle
l_c(:,1) = ones(n,1)/2;
%-- Determine if there are constraints
calltype = DetermineFcnType(Problem,impcons);
%-- first element of f is going to be the function evaluated
%-- at the center of the unit hyper-rectangle.
%om_point = abs(b - a).*l_c(:,1)+ a;
%l_fc(1) = feval(f,om_point,varargin{:});
[l_fc(1),l_con(1), l_feas_flags(1)] = ...
CallObjFcn(Problem,l_c(:,1),a,b,impcons,calltype,varargin{:});
fcncounter = 1;
%-- initialize minval and xatmin to be center of hyper-rectangle
xatmin = l_c(:,1);
minval = l_fc(1);
if tflag == 1
if theglobalmin ~= 0
perror = 100*(minval - theglobalmin)/abs(theglobalmin);
else
perror = 100*minval;
end
else
perror = 2;
end
%-- initialize history
%if g_nargout == 3
history(1,1) = 0;
history(1,2) = 0;
history(1,3) = 0;
%end
%------------------------------------------------------------------%
% Function : find_po %
% Written by : Dan Finkel %
% Created on : 10/19/2002 %
% Purpose : Return list of PO hyperrectangles %
%------------------------------------------------------------------%
function rects = find_po(fc,lengths,minval,ep,szes)
%-- 1. Find all rects on hub
diff_szes = sum(lengths,1);
tmp_max = max(diff_szes);
j=1;
sum_lengths = sum(lengths,1);
for i =1:tmp_max+1
tmp_idx = find(sum_lengths==i-1);
[tmp_n, hullidx] = min(fc(tmp_idx));
if length(hullidx) > 0
hull(j) = tmp_idx(hullidx);
j=j+1;
%-- 1.5 Check for ties
ties = find(abs(fc(tmp_idx)-tmp_n) <= 1e-13);
if length(ties) > 1
mod_ties = find(tmp_idx(ties) ~= hull(j-1));
hull = [hull tmp_idx(ties(mod_ties))];
j = length(hull)+1;
end
end
end
%-- 2. Compute lb and ub for rects on hub
lbound = calc_lbound(lengths,fc,hull,szes);
ubound = calc_ubound(lengths,fc,hull,szes);
%-- 3. Find indeces of hull who satisfy
%-- 1st condition
maybe_po = find(lbound-ubound <= 0);
%-- 4. Find indeces of hull who satisfy
%-- 2nd condition
t_len = length(hull(maybe_po));
if minval ~= 0
po = find((minval-fc(hull(maybe_po)))./abs(minval) +...
szes(hull(maybe_po)).*ubound(maybe_po)./abs(minval) >= ep);
else
po = find(fc(hull(maybe_po)) -...
szes(hull(maybe_po)).*ubound(maybe_po) <= 0);
end
final_pos = hull(maybe_po(po));
rects = [final_pos;szes(final_pos)];
return
%------------------------------------------------------------------%
% Function : calc_ubound %
% Written by : Dan Finkel %
% Created on : 10/19/2002 %
% Purpose : calculate the ubound used in determing potentially %
% optimal hrectangles %
%------------------------------------------------------------------%
function ub = calc_ubound(lengths,fc,hull,szes)
hull_length = length(hull);
hull_lengths = lengths(:,hull);
for i =1:hull_length
tmp_rects = find(sum(hull_lengths,1)<sum(lengths(:,hull(i))));
if length(tmp_rects) > 0
tmp_f = fc(hull(tmp_rects));
tmp_szes = szes(hull(tmp_rects));
tmp_ubs = (tmp_f-fc(hull(i)))./(tmp_szes-szes(hull(i)));
ub(i) = min(tmp_ubs);
else
ub(i)=1.976e14;
end
end
return
%------------------------------------------------------------------%
% Function : calc_lbound %
% Written by : Dan Finkel %
% Created on : 10/19/2002 %
% Purpose : calculate the lbound used in determing potentially %
% optimal hrectangles %
%------------------------------------------------------------------%
function lb = calc_lbound(lengths,fc,hull,szes)
hull_length = length(hull);
hull_lengths = lengths(:,hull);
for i = 1:hull_length
tmp_rects = find(sum(hull_lengths,1)>sum(lengths(:,hull(i))));
if length(tmp_rects) > 0
tmp_f = fc(hull(tmp_rects));
tmp_szes = szes(hull(tmp_rects));
tmp_lbs = (fc(hull(i))-tmp_f)./(szes(hull(i))-tmp_szes);
lb(i) = max(tmp_lbs);
else
lb(i) = -1.976e14;
end
end
return
%------------------------------------------------------------------%
% Function : DIRdivide %
% Written by : Dan Finkel %
% Created on : 10/19/2002 %
% Purpose : Divides rectangle i that is passed in %
%------------------------------------------------------------------%
function [lengths,fc,c,con,feas_flags,szes,fcncounter,pass] = ...
DIRdivide(a,b,Problem,index,thirds,p_lengths,p_fc,p_c,p_con,...
p_feas_flags,p_fcncounter,p_szes,impcons,calltype,varargin)
lengths = p_lengths;
fc = p_fc;
c = p_c;
szes = p_szes;
fcncounter = p_fcncounter;
con = p_con;
feas_flags = p_feas_flags;
%-- 1. Determine which sides are the largest
li = lengths(:,index);
biggy = min(li);
ls = find(li==biggy);
lssize = length(ls);
j = 0;
%-- 2. Evaluate function in directions of biggest size
%-- to determine which direction to make divisions
oldc = c(:,index);
delta = thirds(biggy+1);
newc_left = oldc(:,ones(1,lssize));
newc_right = oldc(:,ones(1,lssize));
f_left = zeros(1,lssize);
f_right = zeros(1,lssize);
for i = 1:lssize
lsi = ls(i);
newc_left(lsi,i) = newc_left(lsi,i) - delta;
newc_right(lsi,i) = newc_right(lsi,i) + delta;
[f_left(i), con_left(i), fflag_left(i)] = CallObjFcn(Problem,newc_left(:,i),a,b,impcons,calltype,varargin{:});
[f_right(i), con_right(i), fflag_right(i)] = CallObjFcn(Problem,newc_right(:,i),a,b,impcons,calltype,varargin{:});
fcncounter = fcncounter + 2;
end
w = [min(f_left, f_right)' ls];
%-- 3. Sort w for division order
[V,order] = sort(w,1);
%-- 4. Make divisions in order specified by order
for i = 1:size(order,1)
newleftindex = p_fcncounter+2*(i-1)+1;
newrightindex = p_fcncounter+2*(i-1)+2;
%-- 4.1 create new rectangles identical to the old one
oldrect = lengths(:,index);
lengths(:,newleftindex) = oldrect;
lengths(:,newrightindex) = oldrect;
%-- old, and new rectangles have been sliced in order(i) direction
lengths(ls(order(i,1)),newleftindex) = lengths(ls(order(i,1)),index) + 1;
lengths(ls(order(i,1)),newrightindex) = lengths(ls(order(i,1)),index) + 1;
lengths(ls(order(i,1)),index) = lengths(ls(order(i,1)),index) + 1;
%-- add new columns to c
c(:,newleftindex) = newc_left(:,order(i));
c(:,newrightindex) = newc_right(:,order(i));
%-- add new values to fc
fc(newleftindex) = f_left(order(i));
fc(newrightindex) = f_right(order(i));
%-- add new values to con
con(newleftindex) = con_left(order(i));
con(newrightindex) = con_right(order(i));
%-- add new flag values to feas_flags
feas_flags(newleftindex) = fflag_left(order(i));
feas_flags(newrightindex) = fflag_right(order(i));
%-- 01/21/04 Dan Hack
%-- store sizes of each rectangle
szes(1,newleftindex) = 1/2*norm((1/3*ones(size(lengths,1),1)).^(lengths(:,newleftindex)));
szes(1,newrightindex) = 1/2*norm((1/3*ones(size(lengths,1),1)).^(lengths(:,newrightindex)));
end
szes(index) = 1/2*norm((1/3*ones(size(lengths,1),1)).^(lengths(:,index)));
pass = 1;
return
%------------------------------------------------------------------%
% Function : CallConstraints %
% Written by : Dan Finkel %
% Created on : 06/07/2004 %
% Purpose : Evaluate Constraints at pointed specified %
%------------------------------------------------------------------%
function ret_value = CallConstraints(Problem,x,a,b,varargin)
%-- Scale variable back to original space
point = abs(b - a).*x+ a;
ret_value = 0;
if isfield(Problem,'constraint')
if ~isempty(Problem.constraint)
for i = 1:Problem.numconstraints
if length(Problem.constraint(i).func) == length(Problem.f)
if double(Problem.constraint(i).func) == double(Problem.f)
%-- Dont call constraint; value was returned in obj fcn
con_value = 0;
else
con_value = feval(Problem.constraint(i).func,point,varargin{:});
end
else
con_value = feval(Problem.constraint(i).func,point,varargin{:});
end
if con_value > 0
%-- Infeasible, punish with associated pen. param
ret_value = ret_value + con_value*Problem.constraint(i).penalty;
end
end
end
end
return
%------------------------------------------------------------------%
% Function : CallObjFcn %
% Written by : Dan Finkel %
% Created on : 06/07/2004 %
% Purpose : Evaluate ObjFcn at pointed specified %
%------------------------------------------------------------------%
function [fcn_value, con_value, feas_flag] = ...
CallObjFcn(Problem,x,a,b,impcon,calltype,varargin)
con_value = 0;
feas_flag = 0;
%-- Scale variable back to original space
point = abs(b - a).*x+ a;
if calltype == 1
%-- No constraints at all
fcn_value = feval(Problem.f,point,varargin{:});
elseif calltype == 2
%-- f returns all constraints
[fcn_value, cons] = feval(Problem.f,point,varargin{:});
for i = 1:length(cons)
if cons > 0
con_value = con_value + Problem.constraint(i).penalty*cons(i);
end
end
elseif calltype == 3
%-- f returns no constraint values
fcn_value = feval(Problem.f,point,varargin{:});
con_value = CallConstraints(Problem,x,a,b,varargin{:});
elseif calltype == 4
%-- f returns feas flag
[fcn_value,feas_flag] = feval(Problem.f,point,varargin{:});
elseif calltype == 5
%-- f returns feas flags, and there are constraints
[fcn_value,feas_flag] = feval(Problem.f,point,varargin{:});
con_value = CallConstraints(Problem,x,a,b,varargin{:});
end
if feas_flag == 1
fcn_value = 10^9;
con_value = 0;
end
return
%------------------------------------------------------------------%
% Function : replaceinf %
% Written by : Dan Finkel %
% Created on : 06/09/2004 %
% Purpose : Assign R. Carter value to given point %
%------------------------------------------------------------------%
function fcn_values = replaceinf(lengths,c,fc,con,flags,pert)
%-- Initialize fcn_values to original values
fcn_values = fc;
%-- Find the infeasible points
infeas_points = find(flags == 1);
%-- Find the feasible points
feas_points = find(flags == 0);
%-- Calculate the max. value found so far
if ~isempty(feas_points)
maxfc = max(fc(feas_points) + con(feas_points));
else
maxfc = max(fc + con);
end
for i = 1:length(infeas_points)
if isempty(feas_points)
%-- no feasible points found yet
found_points = [];found_pointsf = [];
index = infeas_points(i);
else
index = infeas_points(i);
%-- Initialize found points to be entire set
found_points = c(:,feas_points);
found_pointsf = fc(feas_points) + con(feas_points);
%-- Loop through each dimension, and find points who are close enough
for j = 1:size(lengths,1)
neighbors = find(abs(found_points(j,:) - c(j,index)) <= ...
3^(-lengths(j,index)));
if ~isempty(neighbors)
found_points = found_points(:,neighbors);
found_pointsf = found_pointsf(neighbors);
else
found_points = [];found_pointsf = [];
break;
end
end
end
%-- Assign Carter value to the point
if ~isempty(found_pointsf)
%-- assign to index the min. value found + a little bit more
fstar = min(found_pointsf);
if fstar ~= 0
fcn_values(index) = fstar + pert*abs(fstar);
else
fcn_values(index) = fstar + pert*1;
end
else
fcn_values(index) = maxfc+1;
maxfc = maxfc+1;
end
end
return
%------------------------------------------------------------------%
% Function : DetermineFcnType %
% Written by : Dan Finkel %
% Created on : 06/25/2004 %
% Purpose : Determine how constraints are handled %
%------------------------------------------------------------------%
function retval = DetermineFcnType(Problem,impcons)
retval = 0;
if (~isfield(Problem,'constraint'))&(~impcons)
%-- No constraints at all
retval = 1;
end
if isfield(Problem,'constraint')
%-- There are explicit constraints. Next determine where
%-- they are called
if ~isempty(Problem.constraint)
if length(Problem.constraint(1).func) == length(Problem.f)
%-- Constraint values may be returned from objective
%-- function. Investigate further
if double(Problem.constraint(1).func) == double(Problem.f)
%-- f returns constraint values
retval = 2;
else
%-- f does not return constraint values
retval = 3;
end
else
%-- f does not return constraint values
retval = 3;
end
else
if impcons
retval = 0;
else
retval = 1;
end
end
end
if (impcons)
if ~retval
%-- only implicit constraints
retval = 4;
else
%-- both types of constraints
retval = 5;
end
end
%------------------------------------------------------------------%
% GETOPTS Returns options values in an options structure
% USAGE
% [value1,value2,...]=getopts(options,field1,default1,field2,default2,...)
% INPUTS
% options : a structure variable
% field : a field name
% default : a default value
% OUTPUTS
% value : value in the options field (if it exists) or the default value
%
% Variables with the field names will be created in the caller's workspace
% and set to the value in the option variables field (if it exists) or to the
% default value.
%
% Example called from a function:
% getopts(options,'tol',1e-8,'maxits',100);
% where options contains the single field 'tol' with value equal to 1
% The function have two variable defined in the local workspace, tol with a
% value of 1 and maxits with a value of 100.
%
% If options contains a field name not in the list passed to getopts, a
% warning is issued.
%
%
% Many thanks to the author of this function,
% Paul Fackler (pfackler@ncsu.edu)
%
%
%------------------------------------------------------------------%
function varargout=getopts(options,varargin)
K=fix(nargin/2);
if nargin/2==K
error('fields and default values must come in pairs')
end
if isa(options,'struct'), optstruct=1; else, optstruct=0; end
varargout=cell(K,1);
k=0;
ii=1;
for i=1:K
if optstruct & isfield(options,varargin{ii})
assignin('caller',varargin{ii},getfield(options,varargin{ii}));
k=k+1;
else
assignin('caller',varargin{ii},varargin{ii+1});
end
ii=ii+2;
end
if optstruct & k~=size(fieldnames(options),1)
warning('options variable contains improper fields')
end
return
%------------------------------------------------------------------%
% Versions : 1.0 - 1st successful implemenation of DIRect
% : 2.0 - Removed floating point arithmetic
% duplicated Table 5 of Jones et al.
% : 2.1 - increased speed by storing size calcs.
% : 2.2 - utitilized linked lists to increase speed
% : 2.3 - rewrote ubound to increase speed
% : 2.4 - rewrote lbound to increase speed
% : 2.5 - removed call to calcsize
% : 2.6 - added check_for_ties
% : 2.7 - rewrote check_for_ties to compare fp correctly
% : 2.8 - changed output arguments, rewrote help
% : 3.0 - simplified input/output. Put on web.
% : 3.1 - Performanced Tuned! Tremendous speed increase
% : 3.2 - Removed llists; performance tuned
% Many thanks to Ray Muzic and Paul Fackler
% for their suggestions to improve this code
% : 4.0 - Sped up code, and added 2 constraint handling
% mechanisms.
%------------------------------------------------------------------%