product of two variable in Google Optimization Tools python - or-tools

I have a optimization problem. The object is to max two variable x and y. How to represent it in google optimization tools python version.
what i can do now is:
from ortools.linear_solver import pywraplp
solver = pywraplp.Solver('RunIntegerExampleCppStyleAPI',pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
x=solver.IntVar(0, 1, 'x')
y=solver.Intvar(0,1,'y')
objective = solver.Objective()
#so how to get the object function of max x*y?

The question I want to define is quadratic programming, so it can't be solved in linear programming.

Related

MATLAB: Undefined function or variable 'bayesreg'

I want to use the bayesreg package within the MATLAB environment, which enables flexible Bayesian penalized regression modelling. I am following a paper, which details the software implementation within the MATLAB environment. I am using the exact instructions available in the paper but do not seem to get the code to work.
I have imported the table containing the data that I want to analyze. I then type the following code:
varnames=finale.Properties.VariableNames;
% count regression
X = finale{:,2:9}
y = finale{:,11}
[beta, beta0, retval] = bayesreg(X,y,'poisson','g','nsamples',1e5,'burnin',1e5,'thin',5,'displayor',true,'varnames',varnames(2:9),'display',true);
X and y are simply my dependent and independent variables, and all arguments of bayesreg are in agreement with the software implementation paper instructions.
I get the below error:
Undefined function or variable 'bayesreg'.
Could anyone shed any light?
In my case, the problem was that I didn't have the Neural Network toolbox installed, as well as another 1-2 packages. Installing those solved the issue.

Reference for Solution Value in MATLAB using CPLEX as solver

I am looking at solving optimizaton problems using MATLAB to model the problem and CPLEX as the solver. Everything works and I am able to get my solution. However lets say I have 3 optimization variables all of different sizes, when I obtain the solution, I get the values of all the variables in 1 variable which I now have to sought through to get the values for each inidivual variable. Now if I were doing this in python and one of my variables is x, I would just use value.x to get the value of the x variable. Is there a similar way to get the values for the individual optimization variables without have to manually sought it out myself. This is what I use to obtain my solution:
options = cplexoptimset('cplex');
options.timelimit = 300;
[sol3,fval3, exitflag3, output3] = cplexmiqp(PP.H, PP.f, PP.Aineq, PP.bineq, ...
PP.Aeq, PP.beq,[], [], [], PP.lb, PP.ub, ctype, [], options);
If I had 3 optim. variables x1, x2 and x3 which are not scalar but arrays, my solution is stored in sol3 as [x1 x2 x3]. I want to just reference each variable without doing it manually.
To CPLEX a variable is just an index and CPLEX does not know about the fact that subsets of your variables are organized in arrays, list, matrices, etc. So in general, the answer is "you can't do that". However, you are using the toolbox API which is designed to be a drop-in replacement for the matlab toolbox functions and thus has limited functionality.
You could switch to the class API. With this API you can explicitly create variables one by one or array by array and keep indices to the various variables. This is a bit more similar to what you would do in Python.

Can Matlab fmincon take a data-structure as input instead of a vector?

While trying to learn how to use Matlab's function fmincon, I am wondering: is it possible to use data-structures as inputs (for the design variables and boundaries) instead of vectors?
Here's some background details for clarification: I have a number of optimization variables (WingWeight, FuelWeight, ...). Instead of storing them in a vectors:
X(1) = FuelWeight
X(2) = WingWeight
...
Xub(1) = FuelWeightub
Xub(2) = WingWeightub
...
Xlb(1) = FuelWeightlb
Xlb(2) = WingWeightlb
...
I would like to store them in a data structure:
X.WingWeight
X.FuelWeight
Xub.FuelWeightub
Xub.WingWeightub
Xlb.FuelWeightlb
Xlb.WingWeightlb
My overall questions is, will fmincon allow for data-structures as inputs?
I would really like to use structures because the calculation and optimization assignment is really complex and it will take me quite a while to fully understand all the computations needed (I would have to re-edit the design vector many times and that seems really really time consuming to edit all elements everywhere in the code).
It is not possible at the current version of Matlab.

Calculating the expected value of a transformed random variable in MATLAB?

I am trying to compute the following expected value for Z being lognormally distributed
E[Z^eta w(F_Z (Z))^-eta]
where eta is a real number, F_Z the distribution function of Z and w:[0,1]->[0,1] an increasing function.
First of all, I am pretty new to Matlab so I don't know which way of integrating is the better one, numerically or symbolically. I tried symbolically.
My idea was to subsequently define functions:
syms x;
g_1(x) = x^eta;
g_2(x) = logncdf(x);
g_2(x) = w(x)^-eta;
g_4(x) = g_1(x) * g_3(g_2(x));
And then
exp = int(g_4(x),x,0,inf)
Unfortunately this doesn't work and MATLAB just posts the whole expression of g_4...
Is it better to use the numerical integration quadqk? What am I doing wrong here? I already read something about MATLAB not being the best program for integration but I have to use it so switching to a different program does not help.
Thanks a lot!

Using coupled system of PDEs in modelica

Just few questions, i hope someone will find time to answer :).
What if we have COUPLED model example: system of n indepedent variables X and n nonlinear partial differential equations PDEf(X,PDEf(X)) with respect to TIME that depends of X,PDEf(X)(partial differential equation depending of variables X ). Can you give some advice? Here is one example:
Let’s say that c is output, or desired variable. Let’s say that r is independent variable.Partial differential equation looks like:
∂c/∂t=D*1/r+∂c/∂r+2(D* (∂^2 c)/(∂r^2 ))
D=constant
r=0:0.1:Rp- Matlab syntaxis, how to represent same in Modelica (I use integrator,but didn't work)?
Here is a code (does not work):
model PDEtest
/* Boundary conditions
1. delta(c)/delta(r)=0 for r=0
2. delta(c)/delta(r)=-j*d for r=Rp*/
parameter Real Rp=88*1e-3; // length
parameter Real initialConc=1000;
parameter Real Dp=1e-14;
parameter Integer np=10; // num. of points
Real cp[np](start=fill(initialConc,np));
Modelica.Blocks.Continuous.Integrator r(k=1); // independent x1
Real j;
protected
parameter Real dr=Rp/np;
parameter Real ts= 0.01; // for using when loop (sample(0,ts) )
algorithm
j:=sin(time); // this should be indepedent variable like x2
r.u:=dr;
while r.y<=Rp loop
for i in 2:np-1 loop
der(cp[i]):=2*Dp/r.y+(cp[i]-cp[i-1])/dr+2*(Dp*(cp[i+1]-2*cp[i]+cp[i-1])/dr^2);
end for;
if r.y==Rp then
cp[np]:=-j*Dp;
end if;
cp[1]:=if time >=0 then initialConc else initialConc;
end while;
annotation (uses(Modelica(version="3.2")));
end PDEtest;
Here are more questions:
This code don’t work in OpenModelica 1.8.1, also don’t work in Dymola 2013demo. How can we have continuos function of variable c, not array of functions ?
Can we place values of array cp in combiTable? And how?
If instead “algorithm” stay “equation” code can’t be succesfull checked.Why? In OpenModelica, error is :could not flattening model :S.
Is there any simplified way to use a set of equation (PDE’s) that are coupled? I know for PDEs library in Modelica, but I think they are complicated. I want to write a function for solving PDE and call these function in “main model”, so that output of function be continuos function of “c”.I don’t know what for doing with array of functions.
Can you give me advice how to understand Modelica language, if we “speak” like in Matlab? For example: Values of independent variable r,we can specife in Matlab, like r=0:TimeStep:Rp…How to do same in Modelica? And please explain me how section “equation” works, is there similarity with Matlab, and is there necessary sequancial approach?
Cheers :)
It's hard to answer your question, since you assuming that Modelica ~ Matlab, but that's not the case. So I won't comment your code, since it's really wrong. Let me give you an example model to the burger equation. Maybe you could use it as starting point.
model burgereqn
Real u[N+2](start=u0);
parameter Real h = 1/(N+1);
parameter Integer N = 10;
parameter Real v = 234;
parameter Real Pi = 3.14159265358979;
parameter Real u0[N+2]={((sin(2*Pi*x[i]))+0.5*sin(Pi*x[i])) for i in 1:N+2};
parameter Real x[N+2] = { h*i for i in 1:N+2};
equation
der(u[1]) = 0;
for i in 2:N+1 loop
der(u[i]) = - ((u[i+1]^2-u[i-1]^2)/(4*(x[i+1]-x[i-1])))
+ (v/(x[i+1]-x[i-1])^2)*(u[i+1]-2*u[i]+u[i+1]);
end for;
der(u[N+2]) = 0;
end burgereqn;
Your further questions:
cp is an continuous variable and the array is representing
every discretization point.
Why you should want to do that, as far as I understand cp is
your desired solution variable.
You should try to use almost always equation section
algorithm sections are usually used in functions. I'm pretty
sure you can represent your desire behaviour with equations.
I don't know that library, but the hard thing on a pde is the
discretization and the solving it self. You may run into issues
while solving the pde with a modelica tool, since usually
a Modelica tool has no specialized solving algorithm for pdes.
Please consider for that question further references. You could
start with Modelica.org.