ORTOOLS CP SAT - XOR and AND combined - or-tools

I am still learning ORTOOLS CP-SAT which is a great tool!
I am getting into boolean operations difficulties.
For instance in Python, I have an array of an array of expressions (I mean BoundedLinearExpression) organized like this:
my_list = [ [ a<5, b>3, c>5 ],
[ d>8, e<=3, f+a=10 ],
...
]
I want to make a logical AND on each line... and then a XOR on all the results I got. I have no clue how to do that.
Does someone know the answer?
Thanks for your help
# it should end this way:
model.AddBoolXOr(???)

Start by reading This doc section
You need to create Boolean variables for all bounded linear expressions.

Related

How to read formula including turtle values from a input box?

Im having trouble including a formula in the input box into my model and hoped someone could help. In short I want the global variable "subsidy_absolute_threshold" to be read as "100 + m2 / 1600". So that wherever that variable is implemented, that code is implemented instead. The current code reads something like
turtles-own [m2 subsidy_eligible]
globals [subsidy_absolute_threshold]
[...]
set subsidy_absolute_threshold "100 + m2 / 1600"
[...]
ask turtles [
if ambition < read-from-string subsidy_absolute_threshold [set subsidy_eligible true]
]
Technically the global value is an input box set as a string, but it should be functionally the same thing. The error message I get is "Extra characters after literal." which I struggle to understand.
I have tried all the "types" of input boxes, but none work. I have also tried directly writing in the formula, as in replacing subsidy_absolute_threshold with 100 + m2 / 1600, which works.
Use the native command runresult instead of read-from-string.
I'm somewhat unsure exactly why read-from-string does not work, so if someone wants to present an explanation please do.

Plotting group variable excluding some categories based on condition

I want to plot an histogram of a group variable. For this I can use categorical.
I am using an example, using summary(Group), to explain:
variable.group={'one','three','four','one','three','four','two','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four','one','three','four'}
Group=categorical(variable.group)
summary(Group)
figure,histogram(Group),title('Summary Group')
I could also use tabulate(Group) to give this result:
Group_tabulated =
4×3 cell array
'four' [10] [33.3333]
'one' [ 9] [ 30]
'three' [10] [33.3333]
'two' [ 1] [ 3.3333]
Now, as we see in above plot, there is a group with very little occurrence; I want to exclude that category to focus on the 3 most important ones.
Now using condition on tabulate, I have managed to nearly done it. But I have an issue since the category I want to exclude is still showing... just at 0 now.
Group_tabulated = tabulate(Group)
idx_largest=cell2mat(Group_tabulated(:,2))>3
Group_to_display=Group_tabulated(idx_largest,1)
Learn1_1n_largest=Group(ismember(Group,Group_tabulated(idx_largest,1)))
summary(Learn1_1n_largest)
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')
2 questions:
Can we make the solution with tabulate work, so it exclude that category data from Learn1_1n_largest?
Can I use a different method just using some conditions on either categorical or on histogram for this?
Thanks in advance!
I have found the solution to my first question. It is possible to use removecats in order to remove all unused categories. It is simple by just not specifying anything else that the categorical as such Learn1_1n_largest=removecats(Learn1_1n_largest). The full solution to my first question is:
variable.group={'one','three','four','one','three','four','two','three','four','one','three','four','one','three','four','one','three','four', ...
'one','three','four','one','three','four','one','three','four','one','three','four'}
Group=categorical(variable.group)
summary(Group)
figure,histogram(Group),title('Summary Group')
Group_tabulated = tabulate(Group)
idx_largest=cell2mat(Group_tabulated(:,2))>3
Group_to_display=Group_tabulated(idx_largest,1)
Learn1_1n_largest=Group(ismember(Group,Group_tabulated(idx_largest,1)))
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')
Learn1_1n_largest=removecats(Learn1_1n_largest)
figure,histogram(Learn1_1n_largest),title('Summary Group largest only')
I would be glad if anyone has the solution of my second question, as I hope that there would be a simpler solution without having to use both categorical & tabulate.
Thanks!

Find variables and the dependencies between them in code

I am trying to write a function that finds all the variables in a function which contains computing operations. I do this to find out which variables this block of calculations requires as input arguments to perform the calculations.
A calculation function always gets a table with different parameters as input, accesses certain parameters of that table to calculate certain metrics.
For example my data table T contains the double arrays Power and Time. This table is passed to the function calc_energy which then calculates Energy:
function [ T ] = calc_energy( T )
T.Energy = T.Power .* T.Time;
end
Say whenever a new data table is generated, different calculations as the above one are run automatically. Now if Power itself is calculated by the function calc_power, but calc_energy is run before or parallel to calc_power, then I have a problem because calc_energy doesn't find the required variables in T.
In order to prevent such an error, I want my function check_required_variables to be called inside the calc_energy and check beforehand whether T.Power exists. The thing is that check_required_variables should be a general function that is called in every single calculation function and therefore doesn't know the required variables. It has to find them in the function that it is called by.
function [ T ] = calc_energy( T )
OK = check_required_variables( T, 'calc_energy.m' );
if OK == 1
T.Energy = T.Power .* T.Time;
else
error('Required fields not found');
end
end
Are there any functions that spot the variables Energy, Power and Time used in my code? And are there functions that maybe analyse the dependencies between these variables (Energy dependent on Power and Time)? What are generally clever ways to figure out the ocurring variables just from the code? Any ideas?
I know this is a tough one so I'm thankful for any suggestions.
use isfield
function [ T ] = calc_energy( T )
if all(isfield(T,{'Power','Time'}))
T.Energy = T.Power .* T.Time;
else
error('Nooooope')
end
end
However, knowing which ones are required seems a bit harder.... You can always try to read the .m file, and regexp for T.____, then put the input of that to isfield.
This however, just hints of bad software design. A function should know what it requires to run. What happens if OK is false? It just skips the computation? You can then cascade to hundreds of calls (depend on the application) because the original structure failed to have a variable, or had a typo. I'd take the radically opposite approach to software design:
function [ T ] = calc_energy( T )
assert(all(isfield(T,{'Power','Time'}))); %error if there are not.
T.Energy = T.Power .* T.Time;
end

GAMS: retrieve information from solution

GAMS: I think I have a pretty simple question, however I'm stuck and was wondering if someone could help here.
A simplified version of my model looks like this:
set(i,t) ;
parameter price
D;
variable p(i,t)
e(i,t);
equations
Equation1
obj.. C=sum((i,t), p(i,t)*price);
Model file /all/ ;
Solve file minimizing C using MIP ;
Display C.l;
p(i,t) and e(i,t) are related:
Equation1 .. e(i,t)=e=e(i,t-1)+p(i,t)*D
Now I want to retrieve information from the solution: lets say I want to know at what t e(i,t) has a certain value for example --> e(i,t)= x(i) or otherwise formulated e(i,t=TD)=x(i) find TD, where x(i) thus is depending on i. Does anyone know how I can write this in to my GAMs model? To be clear I do not want to change anything about my solution and the model I have runs; I just want to retrieve this information from the solution given.
So far I tried a couple of thing and nothing worked. I think that this must be simple, can anyone help? Thank you!
Try something like this:
set i /i1*i10/
t /t1*t10/;
variable e(i,t);
*some random dummy "solution"
e.l(i,t) = uniformInt(1,10);
set find5(i,t) 'find all combinations of i and t for which e.l=5';
find5(i,t)$(e.l(i,t)=5) = yes;
display e.l,find5;
Hope that helps,
Lutz

Equivalent of InputParser on an Older Version of Matlab?

I am currently running an older version of Matlab - 7.0.4 to be exact, and I'm trying to convert code that involves the newer InputParser into code that would work with this older version. I was wondering if there are any similar commands like inputParser that could be used.
This is the section of code that I'm struggling to convert.
p=inputParser;
p.addParamValue('clusters', repmat(2,k,1), #(x)isvector(x) && length(x)==k);
p.addParamValue('numit', 1000, #(x)x>0 && mod(x,1)==0);
p.addParamValue('abort', 1e-10, #(x)x>=0);
p.addParamValue('verbose', true, #islogical);
p.addParamValue('verbosecompact', true, #islogical);
p.parse(varargin{:});
res=p.Results;
r=res.clusters;
if res.verbose
fprintf('starting graphclustering of %i-partite graph with partition sizes: ',k);
disp(n');
end
Prior to InputParser I used to use 50 to 100 lines of code at the beginning of some complex functions. (Or you can try to roll your own equivalent of the InputParser class.)
Manual input handling is not hard, just a little tedious. The code woudl look something like:
%Check for Clusters
ix = find(cellfun(#(x)strcmpi(x,'clusters'),varargin));
if ~isempty(ix) && (ix+1)<length(varargin)
rec.clusters = varargin{ix+1};
else
rec.clusters = repmat(2,k,1);
end
%Check for 'numit'
% ... following the template above
That will work. For extra credit,. and improved maintainability, you can define the a cell array or structure of with parameter names and default values, and write a loop over that structure, rather than copying the same template code and risking copy/paste errors.
Edit:
This purports to be an example of an input parsing function. I have not tested it, but it may be somewhere to start.
http://www.mathworks.com/matlabcentral/fileexchange/10670-parseargs-simplifies-input-processing-for-functions-with-multiple-options