Here is part of my AMPL scripts that worked as expected:
var pos{i in insts}= diffpos[i]+diffneg[i]+curpos[i] ;
var value_z=sum{j in insts} mids[j]*pos[j];
But when I changed it into following I can’t get the same results. The solvers can’t find a solution:
var value_z=sum{j in insts} (mids[j]*curpos[j]+mids[j]*diffpos[j]+mids[j]*diffneg[j]);
The reason I tried the latter is because ultimately I want to do something like following.
var value_z=sum{j in insts} (mids[j]*curpos[j]+asks[j]*diffpos[j]+bids[j]*diffneg[j]);
I assume if I set asks=bids=mids, they should give me the same results. But I stuck at the above step. Please help me to make it work.
Just in case, here is the whole script that worked:
set insts;
param mids{insts};
param asks{insts};
param bids{insts};
param prices1{insts,priceidx};
param curpos{insts};
param probs{priceidx};
var diffs{insts} integer ;
var pos{i in insts}= diffs[i]+curpos[i] ;
param U = 3000;
var POSdiffs {insts} binary;
var NEGdiffs {insts} binary;
subj to POSdef {i in insts}: U * POSdiffs[i] >= diffs[i];
subj to NEGdef {i in insts}: -U * NEGdiffs[i] <= diffs[i];
subj to BOTHDef{i in insts}: POSdiffs[i]+ NEGdiffs[i]<=1;
var diffpos{i in insts} =POSdiffs[i]*diffs[i] ;
var diffneg{i in insts} =NEGdiffs[i]*diffs[i] ;
var value_z=sum{j in insts} mids[j]*pos[j];
# var value_z=sum{j in insts} (mids[j]*curpos[j]+mids[j]*diffpos[j]+mids[j]*diffneg[j]);
# var value_z=sum{j in insts} (mids[j]*curpos[j]+asks[j]*diffpos[j]+bids[j]*diffneg[j]);
var R_1{i in 1..22}=(sum{j in insts} prices1[j,i]*pos[j])-value_z;
var avggain=sum{i in 1..22} R_1[i]*probs[i];
maximize Maxgoal:avggain;
Unfortunately, I was not able to reproduce this issue regarding the distributive law of multiplication.
Firstly, I executed your script and fixed the following error
distlaw.mod, line 9 (offset 78):
priceidx is not defined
context: param >>> prices1{insts,priceidx} <<< ;
by defining a new set priceidx. I also created a new model called distlaw2.mod that performs the distributive law of multiplication (using your comments in the model). After using some made-up data, I compared the expanded models and they are the same.
Can you please provide two models (one model for which the distributive law is not performed and the other for which the distributive law is performed by hand) for which you encounter this problem and the data?
Hi Jurgen, thanks for taking look into it.
Sorry for missing the definition of priceidx.
I fixed it by changing the way to construct diffpos and diffneg. now the code look like:
var POSdiffs {insts} binary;
var NEGdiffs {insts} binary;
subj to POSdef0 {i in insts}: U * POSdiffs[i] >= diffs[i];
subj to POSdef1 {i in insts}: (1-POSdiffs[i]) * diffs[i]<=0;
subj to NEGdef0 {i in insts}: -U * NEGdiffs[i] <= diffs[i];
subj to NEGdef1 {i in insts}: (1-NEGdiffs[i])* diffs[i]>=0;
subj to BOTHDef{i in insts}: POSdiffs[i]+ NEGdiffs[i]<=1;
var diffpos{i in insts} =POSdiffs[i]*diffs[i] ;
var diffneg{i in insts} =NEGdiffs[i]*diffs[i] ;
the previous code allows POSdiffs and NEGdiffs to be 0 and still meet the requirement. And now they can’t.
please confirm that these are the two models that should be equivalent
set insts;
set priceidx;
param mids{insts};
param asks{insts};
param bids{insts};
param prices1{insts, priceidx};
param curpos{insts};
param probs{priceidx};
param U = 3000;
var diffs{insts} integer ;
var POSdiffs{insts} binary;
var NEGdiffs{insts} binary;
subj to POSdef0{i in insts}: U * POSdiffs[i] >= diffs[i];
subj to POSdef1{i in insts}: (1 - POSdiffs[i]) * diffs[i]<=0;
subj to NEGdef0{i in insts}: -U * NEGdiffs[i] <= diffs[i];
subj to NEGdef1{i in insts}: (1 - NEGdiffs[i])* diffs[i] >= 0;
subj to BOTHDef{i in insts}: POSdiffs[i] + NEGdiffs[i] <= 1;
var diffpos{i in insts} = POSdiffs[i] * diffs[i];
var diffneg{i in insts} = NEGdiffs[i] * diffs[i];
var pos{i in insts} = diffpos[i] + diffneg[i] + curpos[i];
var value_z = sum{j in insts} mids[j] * pos[j];
var R_1{i in 1..22}=(sum{j in insts} prices1[j, i] * pos[j]) - value_z;
var avggain = sum{i in 1..22} R_1[i] * probs[i];
maximize Maxgoal: avggain;
and
set insts;
set priceidx;
param mids{insts};
param asks{insts};
param bids{insts};
param prices1{insts, priceidx};
param curpos{insts};
param probs{priceidx};
param U = 3000;
var diffs{insts} integer;
var POSdiffs{insts} binary;
var NEGdiffs{insts} binary;
subj to POSdef0{i in insts}: U * POSdiffs[i] >= diffs[i];
subj to POSdef1{i in insts}: (1 - POSdiffs[i]) * diffs[i] <= 0;
subj to NEGdef0{i in insts}: -U * NEGdiffs[i] <= diffs[i];
subj to NEGdef1{i in insts}: (1 - NEGdiffs[i]) * diffs[i] >= 0;
subj to BOTHDef{i in insts}: POSdiffs[i] + NEGdiffs[i] <= 1;
var diffpos{i in insts} = POSdiffs[i] * diffs[i];
var diffneg{i in insts} = NEGdiffs[i] * diffs[i];
var value_z = sum{j in insts} (mids[j] * curpos[j] + mids[j] * diffpos[j] + mids[j] * diffneg[j]);
var R_1{i in 1..22}=(sum{j in insts} (prices1[j, i] * diffpos[j] + prices1[j, i] * diffneg[j] + prices1[j, i] * curpos[j])) - value_z;
var avggain = sum{i in 1..22} R_1[i] * probs[i];
maximize Maxgoal: avggain;
The second model is created by performing the distributive law.
If these two models are not the ones you are using please provide the models. If these two models are the ones you are using please provide the data you are using for the models because I cannot reproduce your problem.