66from numpy import int32 , float64
77from numpy import argpartition , repeat
88from numpy import zeros , square , sqrt , full , around , array
9+ import numpy as np
910
1011
1112def get_ocba (means , vars , delta , verbose = False ) -> array :
@@ -47,63 +48,66 @@ def get_ocba(means, vars, delta, verbose=False) -> array:
4748 get_ocba(mean_y, var_y, 50)
4849 array([11, 9, 19, 9, 2])
4950 """
50- n_designs = means .shape [0 ]
51- allocations = zeros (n_designs , int32 )
52- ratios = zeros (n_designs , float64 )
53- budget = delta
54- ranks = get_ranks (means )
55- best , second_best = argpartition (ranks , 2 )[:2 ]
56- ratios [second_best ] = 1.0
57- select = [i for i in range (n_designs ) if i not in [best , second_best ]]
58- temp = (means [best ] - means [second_best ]) / (means [best ] - means [select ])
59- ratios [select ] = square (temp ) * (vars [select ] / vars [second_best ])
60- select = [i for i in range (n_designs ) if i not in [best ]]
61- temp = (square (ratios [select ]) / vars [select ]).sum ()
62- ratios [best ] = sqrt (vars [best ] * temp )
63- more_runs = full (n_designs , True , dtype = bool )
64- add_budget = zeros (n_designs , dtype = float )
65- more_alloc = True
66- if verbose :
67- print ("\n In get_ocba():" )
68- print (f"means: { means } " )
69- print (f"vars: { vars } " )
70- print (f"delta: { delta } " )
71- print (f"n_designs: { n_designs } " )
72- print (f"Allocations: { allocations } " )
73- print (f"Ratios: { ratios } " )
74- print (f"Budget: { budget } " )
75- print (f"Ranks: { ranks } " )
76- print (f"Best: { best } " )
77- print (f"Second best: { second_best } " )
78- print (f"Select: { select } " )
79- print (f"Temp: { temp } " )
80- print (f"More runs: { more_runs } " )
81- print (f"Add budget: { add_budget } " )
82- print (f"More allocations: { more_alloc } " )
83- while more_alloc :
84- more_alloc = False
85- ratio_s = (more_runs * ratios ).sum ()
86- add_budget [more_runs ] = (budget / ratio_s ) * ratios [more_runs ]
87- add_budget = around (add_budget ).astype (int )
88- mask = add_budget < allocations
89- add_budget [mask ] = allocations [mask ]
90- more_runs [mask ] = 0
51+ if np .all (vars > 0 ) and (means .shape [0 ] > 2 ):
52+ n_designs = means .shape [0 ]
53+ allocations = zeros (n_designs , int32 )
54+ ratios = zeros (n_designs , float64 )
55+ budget = delta
56+ ranks = get_ranks (means )
57+ best , second_best = argpartition (ranks , 2 )[:2 ]
58+ ratios [second_best ] = 1.0
59+ select = [i for i in range (n_designs ) if i not in [best , second_best ]]
60+ temp = (means [best ] - means [second_best ]) / (means [best ] - means [select ])
61+ ratios [select ] = square (temp ) * (vars [select ] / vars [second_best ])
62+ select = [i for i in range (n_designs ) if i not in [best ]]
63+ temp = (square (ratios [select ]) / vars [select ]).sum ()
64+ ratios [best ] = sqrt (vars [best ] * temp )
65+ more_runs = full (n_designs , True , dtype = bool )
66+ add_budget = zeros (n_designs , dtype = float )
67+ more_alloc = True
9168 if verbose :
92- print ("\n In more_alloc:" )
93- print (f"ratio_s: { ratio_s } " )
94- print (f"more_runs: { more_runs } " )
95- print (f"add_budget: { add_budget } " )
96- if mask .sum () > 0 :
97- more_alloc = True
98- if more_alloc :
99- budget = allocations .sum () + delta
100- budget -= (add_budget * ~ more_runs ).sum ()
101- t_budget = add_budget .sum ()
102- add_budget [best ] += allocations .sum () + delta - t_budget
103- return add_budget - allocations
69+ print ("\n In get_ocba():" )
70+ print (f"means: { means } " )
71+ print (f"vars: { vars } " )
72+ print (f"delta: { delta } " )
73+ print (f"n_designs: { n_designs } " )
74+ print (f"Allocations: { allocations } " )
75+ print (f"Ratios: { ratios } " )
76+ print (f"Budget: { budget } " )
77+ print (f"Ranks: { ranks } " )
78+ print (f"Best: { best } " )
79+ print (f"Second best: { second_best } " )
80+ print (f"Select: { select } " )
81+ print (f"Temp: { temp } " )
82+ print (f"More runs: { more_runs } " )
83+ print (f"Add budget: { add_budget } " )
84+ print (f"More allocations: { more_alloc } " )
85+ while more_alloc :
86+ more_alloc = False
87+ ratio_s = (more_runs * ratios ).sum ()
88+ add_budget [more_runs ] = (budget / ratio_s ) * ratios [more_runs ]
89+ add_budget = around (add_budget ).astype (int )
90+ mask = add_budget < allocations
91+ add_budget [mask ] = allocations [mask ]
92+ more_runs [mask ] = 0
93+ if verbose :
94+ print ("\n In more_alloc:" )
95+ print (f"ratio_s: { ratio_s } " )
96+ print (f"more_runs: { more_runs } " )
97+ print (f"add_budget: { add_budget } " )
98+ if mask .sum () > 0 :
99+ more_alloc = True
100+ if more_alloc :
101+ budget = allocations .sum () + delta
102+ budget -= (add_budget * ~ more_runs ).sum ()
103+ t_budget = add_budget .sum ()
104+ add_budget [best ] += allocations .sum () + delta - t_budget
105+ return add_budget - allocations
106+ else :
107+ return None
104108
105109
106- def get_ocba_X (X , means , vars , delta ) -> float64 :
110+ def get_ocba_X (X , means , vars , delta , verbose = False ) -> float64 :
107111 """
108112 This function calculates the OCBA allocation and repeats the input array X along the specified axis.
109113
@@ -112,6 +116,7 @@ def get_ocba_X(X, means, vars, delta) -> float64:
112116 means (list): List of means for each alternative.
113117 vars (list): List of variances for each alternative.
114118 delta (float): Indifference zone parameter.
119+ verbose (bool): If True, print the results.
115120
116121 Returns:
117122 (numpy.ndarray): Repeated array of X along the specified axis based on the OCBA allocation.
@@ -130,5 +135,8 @@ def get_ocba_X(X, means, vars, delta) -> float64:
130135 [4, 5, 6]])
131136
132137 """
133- o = get_ocba (means = means , vars = vars , delta = delta )
134- return repeat (X , o , axis = 0 )
138+ if np .all (vars > 0 ) and (means .shape [0 ] > 2 ):
139+ o = get_ocba (means = means , vars = vars , delta = delta , verbose = verbose )
140+ return repeat (X , o , axis = 0 )
141+ else :
142+ return None
0 commit comments