我有 3 種藥瓶尺寸,400 毫克、200 毫克和 80 毫克。我想找到盡可能接近 840mg 總給藥體積的這些小瓶的組合。它需要盡可能接近840mg,但不能更少。我猜它最多需要(840 毫克加上最小的小瓶)- 1
我不確定如何使用 SAS 和或 SQL 來解決這個組合問題。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
我調整了http://rosettacode.org/wiki/Count_the_coins上的 SAS 解決方案,為我提供最大劑量 (840) 和 Maxdisage 最小的 Vial (80) - 1 之間的所有組合,即 919mg
%Let maxDose = 840;
%let Vials = {400,200,80};
%let SmallestVial = 80;
/* call OPTMODEL procedure in SAS/OR */
proc optmodel;
/* declare set and names of coins */
set Vials = {&vials};
str name {Vials} = ['Vial1','Vial2','Vial3'];
/* declare variables and constraint */
var NumVials {Vials} >= 0 integer;
con MAXDosage:
&maxDose <= sum {i in Vials} i * NumVials[i] <= (&maxDose &SmallestVial) - 1;
/* call CLP solver */
solve with CLP / findallsolns;
/* write solutions to SAS data set */
create data sols(drop=s) from [s]=(1.._NSOL_) {i in Vials} <col(name[i])=NumVials[i].sol[s]>;
quit;
/* print all solutions */
proc print data=sols;
run;
現在我只需要將所有這些放在一個宏中即可用于任何組合或小瓶和劑量,謝謝
uj5u.com熱心網友回復:
我可能會做類似的事情
data solutions (keep=total count1-count3);
vialsize1 = 400; vialsize2 = 200; vialsize3 = 80; mintotal = 840;
do count1 = 0 to floor(mintotal/vialsize1) 1;
do count2 = 0 to floor(mintotal/vialsize2) 1;
do count3 = 0 to floor(mintotal/vialsize3) 1;
total = (count1*vialsize1) (count2*vialsize2) (count3*vialsize3);
** Only output those that qualify **;
if total >= mintotal then output;
end;
end;
end;
run;
proc sort data=solutions;
by total;
** Print smallest solution (closest to 840) **;
proc print data=solutions (obs=1);
如果存在,則至少列印一個最佳解決方案。您也可以通過洗掉 (obs=1) 來列印出有效解決方案的完整串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349942.html
