Display commend in ampl - ampl

I have a 2 dimension variable in ampl and I want to display it. I want to change the order of the indices but I do not know how to do that! I put my code , data and out put I described what kind of out put I want to have.
Here is my code:
param n;
param t;
param w;
param p;
set Var, default{1..n};
set Ind, default{1..t};
set mode, default{1..w};
var E{mode, Ind};
var B{mode,Var};
var C{mode,Ind};
param X{mode,Var,Ind};
var H{Ind};
minimize obj: sum{m in mode,i in Ind}E[m,i];
s.t. a1{m in mode, i in Ind}: sum{j in Var} X[m,j,i]*B[m,j] -C[m,i] <=E[m,i];
solve;
display C;
data;
param w:=4;
param n:=9;
param t:=2;
param X:=
[*,*,1]: 1 2 3 4 5 6 7 8 9 :=
1 69 59 100 70 35 1 1 0 0
2 34 31 372 71 35 1 0 1 0
3 35 25 417 70 35 1 0 0 1
4 0 10 180 30 35 1 0 0 0
[*,*,2]: 1 2 3 4 5 6 7 8 9 :=
1 64 58 68 68 30 2 1 0 0
2 44 31 354 84 30 2 0 1 0
3 53 25 399 85 30 2 0 0 1
4 0 11 255 50 30 2 0 0 0
The output of this code using glpksol is like tis:
C[1,1].val = -1.11111111111111
C[1,2].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[2,2].val = -1.11111111111111
C[3,1].val = -0.915032679738562
C[3,2].val = -1.11111111111111
C[4,1].val = 0.141414141414141
C[4,2].val = 0.2003367003367
but I want the result to be like this:
C[1,1].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[3,1].val = -0.915032679738562
C[4,1].val = 0.141414141414141
C[1,2].val = -1.11111111111111
C[2,2].val = -1.11111111111111
C[3,2].val = -1.11111111111111
C[4,2].val = 0.2003367003367
any idea?

You can use for loops and printf commands in your .run file:
for {i in Ind}
for {m in mode}
printf "C[%d,%d] = %.4f\n", m, i, C[m,i];
or even:
printf {i in Ind, m in mode} "C[%d,%d] = %.4f\n", m, i, C[m,i];
I don't get the same numerical results as you, but anyway the output works:
C[1,1] = 0.0000
C[2,1] = 0.0000
C[3,1] = 0.0000
C[4,1] = 0.0000
C[1,2] = 0.0000
C[2,2] = 0.0000
C[3,2] = 0.0000
C[4,2] = 0.0000

Related

Why does this piecewise linear mixed model not produce equal estimates at the knot

I am wondering if someone could help me interpret my piecewise lmm results. Why does ggpredict() produce different estimates for the knot at 10 weeks (end of tx; see ‘0’ in graph at end)? I've structured the data like so:
bpiDat <- bpiDat %>%
mutate(baseToEndTx = ifelse(week <= 10, week, 1)) %>%
mutate(endOfTxToFu = case_when(
week <= 10 ~ 0,
week == 18 ~ 8,
week == 26 ~ 16,
week == 34 ~ 24
)) %>%
select(id, treatment, baseHamd, week, baseToEndTx, endOfTxToFu,
painInterferenceMean, painSeverityMean, bpiTotal) %>%
mutate(baseHamd = scale(baseHamd, scale=F))
Which looks like this:
id treatment baseHamd week baseToEndTx endOfTxToFu painSeverityMean
1 1 4.92529343 0 0 0 6.75
1 1 4.92529343 2 2 0 7.25
1 1 4.92529343 4 4 0 8.00
1 1 4.92529343 6 6 0 NA
1 1 4.92529343 8 8 0 8.25
1 1 4.92529343 10 10 0 8.00
1 1 4.92529343 18 1 8 8.25
1 1 4.92529343 26 1 16 8.25
1 4.92529343 34 1 24 8.00
The best fitting model:
model8 <- lme(painSeverityMean ~ baseHamd + baseToEndTx*treatment + endOfTxToFu + I(endOfTxToFu^2)*treatment,
data = bpiDat,
method = "REML",
na.action = "na.exclude",
random = ~baseToEndTx | id)
This is how I’m visualizing:
test1 <- ggpredict(model8, c("baseToEndTx", "treatment"), ci.lvl = NA) %>%
mutate(x = x - 10) %>%
mutate(phase = "duringTx")
test2 <- ggpredict(model8, c("endOfTxToFu", "treatment"), ci.lvl = NA) %>%
mutate(phase = "followUp")
t <- rbind(test1, test2)
t <- t %>%
pivot_wider(names_from = "phase",
values_from = "predicted")
ggplot(t) +
geom_smooth(aes(x,duringTx,col=group),method="lm",se=FALSE) +
geom_smooth(aes(x,followUp,col=group),method="lm",se=FALSE) +
geom_point(aes(x,duringTx,col=group)) +
geom_point(aes(x,followUp,col=group)) +
ylim(2,6)
Which produces this:


generate date feature column using pandas

I have a timeseries data frame that has columns like these:
Date temp_data holiday day
01.01.2000 10000 0 1
02.01.2000 0 1 2
03.01.2000 2000 0 3
..
..
..
26.01.2000 200 0 26
27.01.2000 0 1 27
28.01.2000 500 0 28
29.01.2000 0 1 29
30.01.2000 200 0 30
31.01.2000 0 1 31
01.02.2000 0 1 1
02.02.2000 2500 0 2
Here, holiday = 0 when there is data present - indicates a working day
holiday = 1 when there is no data present - indicated a non-working day
I am trying to extract three new columns from this data -second_last_working_day_of_month and third_last_working_day_of_month and the fourth_last_wday
the output data frame should look like this
Date temp_data holiday day secondlast_wd thirdlast_wd fouthlast_wd
01.01.2000 10000 0 1 1 0 0
02.01.2000 0 1 2 0 0 0
03.01.2000 2000 0 3 0 0 0
..
..
25.01.2000 345 0 25 0 0 1
26.01.2000 200 0 26 0 1 0
27.01.2000 0 1 27 0 0 0
28.01.2000 500 0 28 1 0 0
29.01.2000 0 1 29 0 0 0
30.01.2000 200 0 30 0 0 0
31.01.2000 0 1 31 0 0 0
01.02.2000 0 1 1 0 0 0
02.02.2000 2500 0 2 0 0 0
Can anyone help me with this?
Example
data = [['26.01.2000', 200, 0, 26], ['27.01.2000', 0, 1, 27], ['28.01.2000', 500, 0, 28],
['29.01.2000', 0, 1, 29], ['30.01.2000', 200, 0, 30], ['31.01.2000', 0, 1, 31],
['26.02.2000', 200, 0, 26], ['27.02.2000', 0, 0, 27], ['28.02.2000', 500, 0, 28],['29.02.2000', 0, 1, 29]]
df = pd.DataFrame(data, columns=['Date', 'temp_data', 'holiday', 'day'])
df
Date temp_data holiday day
0 26.01.2000 200 0 26
1 27.01.2000 0 1 27
2 28.01.2000 500 0 28
3 29.01.2000 0 1 29
4 30.01.2000 200 0 30
5 31.01.2000 0 1 31
6 26.02.2000 200 0 26
7 27.02.2000 0 0 27
8 28.02.2000 500 0 28
9 29.02.2000 0 1 29
Code
for example make secondlast_wd column (n=2)
n = 2
s = pd.to_datetime(df['Date'])
result = df['holiday'].eq(0) & df.iloc[::-1, 2].eq(0).groupby(s.dt.month).cumsum().eq(n)
result
0 False
1 False
2 True
3 False
4 False
5 False
6 False
7 True
8 False
9 False
Name: holiday, dtype: bool
make result to secondlast_wd column
df.assign(secondlast_wd=result.astype('int'))
output:
Date temp_data holiday day secondlast_wd
0 26.01.2000 200 0 26 0
1 27.01.2000 0 1 27 0
2 28.01.2000 500 0 28 1
3 29.01.2000 0 1 29 0
4 30.01.2000 200 0 30 0
5 31.01.2000 0 1 31 0
6 26.02.2000 200 0 26 0
7 27.02.2000 0 0 27 1
8 28.02.2000 500 0 28 0
9 29.02.2000 0 1 29 0
you can change n and can get third, forth and so on..
Update for comment
chk workday(reverse index)
df.iloc[::-1, 2].eq(0) # 2 means location of 'holyday'. can use df.loc[::-1,"holiday"]
9 False
8 True
7 True
6 True
5 False
4 True
3 False
2 True
1 False
0 True
Name: holiday, dtype: bool
reverse cumsum by group(month). then when workday is +1 above value and when holyday is still same value with above.(of course in reverse index)
df.iloc[::-1, 2].eq(0).groupby(s.dt.month).cumsum()
9 0
8 1
7 2
6 3
5 0
4 1
3 1
2 2
1 2
0 3
Name: holiday, dtype: int64
find holiday == 0 and result == 2, that is secondlast_wd
df['holiday'].eq(0) & df.iloc[::-1, 2].eq(0).groupby(s.dt.month).cumsum().eq(2)
0 False
1 False
2 True
3 False
4 False
5 False
6 False
7 True
8 False
9 False
Name: holiday, dtype: bool
This operation returns index as it was.(not reverse)
Other Way
A more understandable code would be:
s = pd.to_datetime(df['Date'])
idx1 = df[df['holiday'].eq(0)].groupby(s.dt.month, as_index=False).nth(-2).index
df.loc[idx1, 'lastsecondary_wd'] = 1
df['lastsecondary_wd'] = df['lastsecondary_wd'].fillna(0).astype('int')
same result

Python optimization of loop in data frame with max and min values

I have question how can I optimize my code, in fact only the loops. I use to calculate solutions maximum of two rows, or sometimes max of row and number.
I tried to change my code using .loc and .clip but when it is about max or min which shows up multiple times I have some troubles with logical expressions.
That it was looking at the begining:
def Calc(row):
if row['Forecast'] == 0:
return max(row['Qty'],0)
elif row['def'] == 1:
return 0
elif row['def'] == 0:
return round(max(row['Qty'] - ( max(row['Forecast_total']*14,(row['Qty_12m_1']+row['Qty_12m_2'])) * max(1, (row['Total']/row['Forecast'])/54)),0 ))
df['Calc'] = df.apply(Calc, axis=1)
I menaged to change it using functions that I pointed but I have a problem how to write this max(max())
df.loc[(combined_sf2['Forecast'] == 0),'Calc'] = df.clip(0,None)
df.loc[(combined_sf2['def'] == 1),'Calc'] = 0
df.loc[(combined_sf2['def'] == 0),'Calc'] = round(max(df['Qty']- (max(df['Forecast_total']
*14,(df['Qty_12m_1']+df['Qty_12m_2']))
*max(1, (df['Total']/df['Forecast'])/54)),0))
First two functions are working, the last one doesn't.
id Forecast def Calc Qty Forecast_total Qty_12m_1 Qty_12m_2 Total
31551 0 0 0 2 0 0 0 95
27412 0,1 0 1 3 0,1 11 0 7
23995 0,1 0 0 4 0 1 0 7
27411 5,527 1 0,036186 60 0,2 64 0 183
28902 5,527 0 0,963814 33 5,327 277 0 183
23954 5,527 0 0 6 0 6 0 183
23994 5,527 0 0 8 0 0 0 183
31549 5,527 0 0 6 0 1 0 183
31550 5,527 0 0 6 0 10 0 183
Use numpy.select and instead max use numpy.maximum:
m1 = df['Forecast'] == 0
m2 = df['def'] == 1
m3 = df['def'] == 0
s1 = df['Qty'].clip(lower=0)
s3 = round(np.maximum(df['Qty'] - (np.maximum(df['Forecast_total']*14,(df['Qty_12m_1']+df['Qty_12m_2'])) * np.maximum(1, (df['Total']/df['Forecast'])/54)),0 ))
df['Calc2'] = np.select([m1, m2, m3], [s1, 0, s3], default=None)

extracting lines with pivot column

Infile,
S 235 1365 * 0 * * * 15 1 c81 592
H 235 296 99.7 + 0 0 3I296M1066I 14 1 s15018 1
H 235 719 95.4 + 0 0 174D545M820I 15 1 c2664 10
H 235 764 99.1 + 0 0 55I764M546I 15 1 c6519 4
H 235 792 100 + 0 0 180I792M393I 14 1 c407 107
S 236 1365 * 0 * * * 15 1 c474 152
H 236 279 95 + 0 0 765I279M321I 10-1 1 s7689 1
H 236 301 99.7 - 0 0 908I301M156I 15 1 s8443 1
H 236 563 95.2 - 0 0 728I563M74I 17 1 c1725 12
H 236 97 97.9 - 0 0 732I97M536I 17 1 s11472 1
S 237 1365 * 0 * * * 15 1 c474 152
H 237 279 95 + 0 0 765I279M321I 15 1 s7689 1
S 238 1365 * 0 * * * 12 1 c474 152
H 238 279 95 + 0 0 765I279M321I 10-1 1 s7689 1
H 238 301 99.7 - 0 0 908I301M156I 15 1 s8443 1
H 238 563 95.2 - 0 0 728I563M74I 17 1 c1725 12
H 238 97 97.9 - 0 0 732I97M536I 17 1 s11472 1
Outfile what I want is below,
Example 1 by specifying ninth column "10-1", "15", and "17".
S 236 1365 * 0 * * * 15 1 c474 152
H 236 279 95 + 0 0 765I279M321I 10-1 1 s7689 1
H 236 301 99.7 - 0 0 908I301M156I 15 1 s8443 1
H 236 563 95.2 - 0 0 728I563M74I 17 1 c1725 12
H 236 97 97.9 - 0 0 732I97M536I 17 1 s11472 1
Example 2 by specifying ninth column "14" and "15".
S 235 1365 * 0 * * * 15 1 c81 592
H 235 296 99.7 + 0 0 3I296M1066I 14 1 s15018 1
H 235 719 95.4 + 0 0 174D545M820I 15 1 c2664 10
H 235 764 99.1 + 0 0 55I764M546I 15 1 c6519 4
H 235 792 100 + 0 0 180I792M393I 14 1 c407 107
Example 3 by specifying ninth column "15".
S 237 1365 * 0 * * * 15 1 c474 152
H 237 279 95 + 0 0 765I279M321I 15 1 s7689 1
So I would like to extract set of lines those have same value in the second column. At this time, I need to extract only set of lines which have specific values in 9th column. In that case, the set of lines need to have "all of the specified values".
The set 238 has "12" in the ninth column, which is not specified. So I do not want them to be extracted.
This question is very similar to this question.
Extracting lines using two criteria
There's many possible approaches but IMHO the most robust and easiest to expand upon later is to create a hash table of the desired values (goodVals[] below) and then just test if the current $9 is a value that's not in that table:
BEGIN { split("10-1 15 17",tmp); for (i in tmp) goodVals[tmp[i]] }
$2 != prevPivot { prtCurrSet() }
!($9 in goodVals) { isBadSet=1 }
{ currSet = currSet $0 ORS; prevPivot = $2 }
END { prtCurrSet() }
function prtCurrSet() {
if ( !isBadSet ) {
printf "%s", currSet
}
currSet = ""
isBadSet = 0
}
Given the new requirement from your comment, here's a solution for one possible interpretation of that requirement:
$ cat tst.awk
BEGIN { split("10-1 15 17",tmp); for (i in tmp) goodVals[tmp[i]] }
$2 != prevPivot { prtCurrSet() }
{ seen[$9]; currSet = currSet $0 ORS; prevPivot = $2 }
END { prtCurrSet() }
function prtCurrSet( val,allGoodPresent) {
allGoodPresent = 1
for (val in goodVals) {
if ( !(val in seen) ) {
allGoodPresent = 0
}
}
if ( allGoodPresent ) {
printf "%s", currSet
}
currSet = ""
delete seen
}
$ awk -f tst.awk file
S 236 1365 * 0 * * * 15 1 c474 152
H 236 279 95 + 0 0 765I279M321I 10-1 1 s7689 1
H 236 301 99.7 - 0 0 908I301M156I 15 1 s8443 1
H 236 563 95.2 - 0 0 728I563M74I 17 1 c1725 12
H 236 97 97.9 - 0 0 732I97M536I 17 1 s11472 1
and here's another:
$ cat tst.awk
BEGIN { split("10-1 15 17",tmp); for (i in tmp) goodVals[tmp[i]] }
$2 != prevPivot { prtCurrSet() }
{ seen[$9]; currSet = currSet $0 ORS; prevPivot = $2 }
END { prtCurrSet() }
function prtCurrSet( val,allGoodPresent,someBadPresent) {
allGoodPresent = 1
for (val in goodVals) {
if ( !(val in seen) ) {
allGoodPresent = 0
}
delete seen[val]
}
someBadPresent = length(seen)
if ( allGoodPresent && !someBadPresent ) {
printf "%s", currSet
}
currSet = ""
delete seen
}
$ awk -f tst.awk file
S 236 1365 * 0 * * * 15 1 c474 152
H 236 279 95 + 0 0 765I279M321I 10-1 1 s7689 1
H 236 301 99.7 - 0 0 908I301M156I 15 1 s8443 1
H 236 563 95.2 - 0 0 728I563M74I 17 1 c1725 12
H 236 97 97.9 - 0 0 732I97M536I 17 1 s11472 1
Unfortunately your posted sample input/output isn't adequate to test the differences.

Delete set of lines between two strings in Excel VBA

I have a task to read a bunch of notepad files and in each of the file, I need to delete the set of lines which are present in between the strings "dynamics" and "end dynamics" and also delete those two strings as well.
The attached image shows the part which needs to be removed, wherever it has occurred (dynamics to end dynamics – any content can be present with these two boundaries) in that text file.
L3.3
resizeText 1
zoomLines 0
zoomArrows 0
doLasso 1
opaqueMove 1
selectDistance 30
adjustFonts 1
doubleBuffer 1
clipping 1
nCopyAreas 0
drawTextLimit 0.5
saveObjects 1
canvasBackground #050048007800
defaultForeground #000000000000
layers 1
layerName 0 0
layerName 1 1
layerName 2 2
layerName 3 3
layerName 4 4
layerName 5 5
layerName 6 6
layerName 7 7
layerName 8 8
layerName 9 9
layerName 10 10
layerName 11 11
layerName 12 12
layerName 13 13
layerName 14 14
layerName 15 15
layerName 16 16
layerName 17 17
layerName 18 18
layerName 19 19
layerName 20 20
layerName 21 21
layerName 22 22
layerName 23 23
layerName 24 24
layerName 25 25
layerName 26 26
layerName 27 27
layerName 28 28
layerName 29 29
layerName 30 30
layerName 31 31
gend
N 0
P 0 0
T -1
R 0 0
0
0 4 1 0
Name #WVP
0 1 1
!
27e
054878
-1-1-1
0
0
0
0 0
dynamics
script
//***GblSymDetails***
;DTLS; GSA_TEXT = "CIUXX"
//***ApplReplace***
//GEMTool = 1
// = ASPECTLINK
end script
end dynamics
0 0 1920 1080 0 0
N 2
P 34.7792 181.549
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 3 0
Name STAT5495
0 1 1
!
27e
a5a5a5
a5a5a5
0
0
0
2 0
0 0 0
0 0 0 0 1
4
0 12.7627
11.5724 0
192.874 0.255264
185.159 12.3798
N 4
P 221.604 181.887
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 5 0
Name STAT5496
0 1 1
!
27e
7c7c7c
7c7c7c
0
0
0
2 0
dynamics
script
func ip_FillColor() {
return FILLCOLOR;
}
func ip_LineColor() {
return LINECOLOR;
}
func ip_TEXT() {
return TEXT;
}
func BackColor() {
return RGB(124,124,124);
}
func ForeColor() {
return RGB(124,124,124);
}
// when ...
object.background = BackColor();
object.foreground = ForeColor();
end script
end dynamics
0 0 0
0 0 0 0 1
4
0 11.6149
6.04944 0
6.04944 54.445
0 72.5933
N 6
P 81.7124 181.888
T 2 21071 1 0 0
0 0
R 0 0
0
0 0 7 0
Name STAT5497
0 1 1
!
27e
616161
616161
0
0
0
2 0
dynamics
script
func ip_FillColor() {
return FILLCOLOR;
}
func ip_LineColor() {
return LINECOLOR;
}
func ip_TEXT() {
return TEXT;
}
func BackColor() {
return RGB(97,97,97);
}
func ForeColor() {
return RGB(97,97,97);
}
// when ...
object.background = BackColor();
object.foreground = ForeColor();
end script
end dynamics
0 1 0
0 0 0 0 1
72
93.8536 5.86588
Something like the code below. You should be able to add code to dump all the files into a folder and DIR through them reading, stripping text and saving the output files automatically. Depending on the number of files it may or may not be time-efficient to do.
Sub cleantext()
Dim lineOfText As String
Dim skipLines As Boolean
'Open files for writing
Open "D:\inputfile.txt" For Input As #1
Open "D:\outputfile.txt" For Output As #2
skipLine = False
Do Until EOF(1)
Line Input #1, lineOfText
If lineOfText = "dynamics" Then skipLines = True
If lineOfText = "end dynamics" Then skipLines = False
If Not skipLines And Not lineOfText = "end dynamics" Then Print #2, lineOfText
Loop
Close #1
Close #2
End Sub