How can i use the rem function with a variable? - optimization

i want to use the rem function with every element in a string v[j], i tried with rem(v[j],3) but i get the following error MethodError: no method matching rem(::VariableRef, ::Int64).
Can i use rem with a string? is there any other function that does the same?

You cannot use rem with JuMP variables. Use a mixed-integer linear reformulation instead:
model = Model()
#variable(model, x)
#variable(model, y, Int)
#variable(model, rem)
#constraint(model, x == 3y + rem)
p.s. Since you've posted a few questions in quick succession, come join the JuMP community forum: https://discourse.julialang.org/c/domain/opt/13. It has more JuMP-specific questions than stack overflow

s = "some string"
rem.(Int32.(collect(s)), 3)
collect yields all Chars of a string. This will work regardless of what unicode characters are contained within the String

Related

CBLAS - ** On entry to SGEMM / DGEMM, parameter number X had an illegal value?

I am calling cblas_sgemm using the following parameters:
order: CblasRowMajor
transA, transB: either CblasTrans or CblasNoTrans
M: the number of rows (height) of the matrix op(A) and of the matrix C
N: the number of columns (width) of the matrix op(B) and of the matrix C
K: the number of columns (width) of the matrix op(A) and the number of rows (height) of the matrix op(B)
alpha: scalar
A: pointer to matrix A
LDA: When transA = CblasNoTrans then LDA = M, otherwise LDA = K
B: pointer to matrix B
LDB: when transB = CblasNoTrans then LDB = K, otherwise LDB = N
beta: scalar
C: pointer to matrix C (bias on entry and the result on exit)
LDC = M
where, op(M) = M if transM is CblasNoTrans, and Transpose(M) otherwise
The parameters are correct (according to the documentation) but I am getting am error:
"** On entry to SGEMM, parameter number X had an illegal value" - How do I fix this error?
TL;DR
When using: CblasRowMajor
Keep M,N,K setting as listed above.
Flip Transpose value when computing LDA,LDB, and set LDC = N
Details:
I ran into this problem using multiplication with Transpose and could not find a detailed answer that answers my problem. I write this Q/A in hope it will be useful for others.
The root problem, as others have noted, is that cblas is a wrapper over BLAS, which is written in FORTRAN that does not have an order parameter and expect column major matrix representation. The available documentation (usually) is the BLAS documentation - which I used above in the question.
However, While M,N,and K are logical values (the width/height of matrix regardless of its representation) the leading dimensions (LDA, LDB, LDC) are not. Therefore the computation of M,N, and K should stay the same when using CblasRowMajor. However, the transpose of op(A), op(B) and C, should be use when computing LDA, LDB, and LDC.
If CblasColMajor is used then it is the same representation as Fortran and the parameter setting shown in the question is the correct one.
Also note that when you get and error in parameter X, X is shifted by 1 because the error originated in BLAS that does not have an order parameter.

How to use PySCIPOpt for feasibility-only problem

I have used CVXPY and some of its LP solvers to determine whether a solution to an A*x <= b problem is feasible, and now I would like to try PySCIPOpt. I could not find an example of this in the docs, and I'm having trouble figuring out the right syntax. With CVXPY the code is simply:
def do_cvxpy(A, b, solver):
x = cvxpy.Variable(A.shape[1])
constraints = [A#x <= b] #The # denotes matrix multiplication in CVXPY
obj = cvxpy.Minimize(0)
prob = cvxpy.Problem(obj, constraints)
prob.solve(solver=solver)
return prob.status
I think with PySCIPOpt one cannot use matrix notation as above, but must treat vectors and matrices as collections of scalar variables, each of which has to be added individually, so I tried this:
def do_scip(A, b):
model = Model("XYZ")
x = {}
for i in range(A.shape[1]):
x[i] = model.addVar(vtype="C", name="x(%s)" % i)
model.setObjective(0) #Is this right for a feasibility-only problem?
model.addCons(A*x <= b) #This is certainly the wrong syntax
model.optimize()
return model.getStatus()
Could anyone please help me out with the correct form for the constraint in addCons() for this kind of problem, and confirm that an acceptable way to ask whether a solution is feasible is to simply pass 0 as the objective?
I'm still not positive about the setObjective(0), but at least I can get the code to run without errors by "unpacking" the A matrix and the b vector and adding each element as a constraint:
for i in range(ncols):
for j in range(nrows):
model.addCons(A[j,i]*x[i] <= b[i])
I also discovered that CVXPY actually has an interface to SCIP, but it gives me an error when I try to use it:
getSolObjVal cannot only be called in stage SOLVING without a valid solution
which seems to suggest that the interface cannot be used for feasibility-only problems.

Objective function for Optimization problem

I am dealing with an optimization problem where I have to optimize model parameters to minimize errors in the model predictions (y_pred) w.r.t. observations (y_obs). My objective is to minimize Root Mean Square Error (RMSE) and maximize the correlation coefficient (CORR). I came up with following objective function:
minimize(f) = minimize(lambda*RMSE/CORR)
where lambda is some negative large value (e.g., -1e6) if CORR < 0
else lambda = 1
Did I define the objective function correctly or It can be defined in better way?
Try ^^
I think you need to search in minimize variable and put your equation inside the function or variable.
minimize = RMSE/CORR # if it's a variable
# for function need search ^^
study = optuna.create_study(directions["minimize"],...)
func = lambda trial: objective(trial, X_enc, y_train_full)
study.optimize(func, n_trials=10)
For more information, see this Optuna tutorial.

struggling minimizing non linear function

I am looking forward to minimize a non linear function with 3 arguments (x1,x2 and x3)
My sources of information are:
the explanation of the minimization function:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
And an example they provide:
https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html
I do not belong to a mathematical area, so first off forgive me if I am using incorrect wording / expressions.
This is my code :
import numpy as np
from scipy.optimize import minimize
def rosen(x1,x2,x3):
return np.sqrt(((x1**2)*0.002)+((x2**2)*0.0035)+((x3**2)*0.0015)+(2*x1*x2*0.015)+(2*x1*x3*0.01)+(2*x2*x3*0.02))
I think that the first step is okey up to here..
Then it is required to state the:
x0 : ndarray
Initial guess. len(x0) is the dimensionality of the minimization problem.
Given that I am stating 3 args in the minimization function I shall state a 3 dim array , such like this?
x0=np.array([1,1,1])
res = minimize(rosen, x0)
print(res.x)
The undesired output is:
rosen() missing 2 required positional arguments: 'x2' and 'x3'
Which I do not really understand where shall I state the positional arguments.
Apart from that I would like to set some bounds for the outputing values for x1,x2,x3 .
Which I tried
res = minimize(rosen, x0, bounds=([0,None]),options={"disp": False})
Which outputs also that :
ValueError: length of x0 != length of bounds
How should I then express the bounds inside the res then?
The desired output would be simply to output an array for x1,x2,x3 according to the minimization of the function where each value is minimun 0, as stated in the bounds and that the sum of the args add up to 1.
Function-definition
Read the docs carefully, e.g. for your function-def:
fun : callable
The objective function to be minimized. Must be in the form f(x, *args). The
optimizing argument, x, is a 1-D array of points, and args is a tuple of any
additional fixed parameters needed to completely specify the function.
Your function should take a 1d-array, while you implement the multi-argument for multi-variables approach!
Changing:
def rosen(x1,x2,x3):
return np.sqrt(((x1**2)*0.002)+((x2**2)*0.0035)+((x3**2)*0.0015)+(2*x1*x2*0.015)+(2*x1*x3*0.01)+(2*x2*x3*0.02))
def rosen(x):
x1,x2,x3 = x # unpack vector for your kind of calculations
return np.sqrt(((x1**2)*0.002)+((x2**2)*0.0035)+((x3**2)*0.0015)+(2*x1*x2*0.015)+(2*x1*x3*0.01)+(2*x2*x3*0.02))
should work. This is a bit a repair-something-to-keep-my-other-code approach but won't hurt much in this example. Usually you implement your function-definition on the 1d-array-input assumption!
Bounds
Again from the docs:
bounds : sequence, optional
Bounds for variables (only for L-BFGS-B, TNC and SLSQP). (min, max) pairs for each
element in x, defining the bounds on that parameter. Use None for one of min or max
when there is no bound in that direction.
So you need n_vars pairs! Easily achieved by using a list-comprehension, deducing the necessary info from x0.
res = minimize(rosen, x0, bounds=[[0,None] for i in range(len(x0))],options={"disp": False})
Make variables sum up to 1 / Constraints
Your comment implies you want the variables to sum up to 1. You would need to use an equality-constraint then (only 1 solver supporting this and inequality-constraints; one other only inequality-constraints; the rest no constraints; solver will be picked automatically if none explicitly given).
It looks somewhat like:
cons = ({'type': 'eq', 'fun': lambda x: sum(x) - 1}) # read docs to understand!
# to think about:
# sum vs. np.sum
# (not much diff here)
res = minimize(rosen, x0, bounds=[[0,None] for i in range(len(x0))],options={"disp": False}, constraints=cons)
For the case of x nonnegative, the constraint is usually called the probability-simplex.
(untested code; conceptually correct!)

optimize.root with a matrix equation

I am trying to solve the following linear system using optimize.root
AX = b
With the following code.
A = [[0,1,0],[2,1,0],[1,4,1]]
def foo(X):
b = np.matrix([2,1,1])
out = np.dot(A,X) - b
return out.tolist()
sol = scipy.optimize.root(foo,[0,0,0])
I know that I can simply use the numpy.linalg.solve to do this easily. But I am actually trying to solve a non linear system that is in matrix form. See my question here. So I need to find a way to make this method work. To do that I am trying to solve this problem in this simple case. But I get the error
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'foo'.Shape should be (3,) but it is (1, 3).
From what I have read from other similar stackoverflow questions this happens because the out put of the foo function is not compatible with the shape of the initial guess [0,0,0]
Surely there is a way to solve this equation using scipy.optimize.root. Can anyone please help?
(I'm assuming the capital B in your .dot is a typo for A.)
Try using np.array for b. np.matrix creates a "row vector", i.e. shape (1, 3) whereas your initial guess has shape (3,).