MOD for optimization variable

15 次查看(过去 30 天)
Hi,
I have to optimize matrix to obtain quantities that will be divisible by certain numbers. When I use MOD on numbers, it works fine, for example MOD(10,3)=1. However, when I try to create constraint that I want my optimization variable x to be divisible by let's say 3 MOD(x,3), I get an error
Undefined function 'mod' for input arguments of type 'optim.problemdef.OptimizationVariable'.
Is there anything I can do to solve this issue?
Thanks in advance.

采纳的回答

Matt J
Matt J 2019-8-24
编辑:Matt J 2019-8-24
No need to use constraints. Just declare x as an OptimizationExpression,
x=3*optimvar('z','Type', 'Integer','LowerBound',0)
x =
OptimizationExpression
3*z
Now use x freely to build the objective and constraints as you did previously. E.g.,
x=3*optimvar('z','Type', 'integer','LowerBound',0);
y=optimvar('y','LowerBound',0);
prob=optimproblem;
prob.Objective=2*x+4*y;
prob.Constraints=x+y>=7;
sol=solve(prob);
The solver will solve for z, not x,
>> sol
sol =
struct with fields:
y: 1.0000
z: 2.0000
but you can easily convert the optimal z to a corresponding result for x.
>> x_optimal=evaluate(x,sol)
x_optimal =
6.0000
  3 个评论
Matt J
Matt J 2019-8-25
annazy's comment moved here:
This is wonderful. What I actually need is this 'z', as I optimize quantities of products which must be divisible by number of products per cartoon. However this number varies depending on products. Also, I have multiple percentage constraints on quantities (not cartoons) which is why I can't focus on cartoons only, but in the end number of cartoons is what I need.
You are both amazing people, I hope one day I will be clever enough to help other lost souls like you do help me :)
Matt J
Matt J 2019-8-25
I'm glad it's what you need but please Accept-click the answer if it addresses your question.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-8-22
Introduce an extra integer variable and constrain equality x-3*extra = 1
  4 个评论
annazy
annazy 2019-8-24
Wow, thank you a lot for such detailed answer and your patience. Honestly, this is my first experience with Matlab and I am a bit confused with its options. I used to solve simple optimization problems with Excel Solver, however due to the dimension of the matrix, in this case I cannot use Solver as it can't hadle such large size. I was forced to learn basic optimization tools in Matlab within a week :(
I understand the process, I just don't know how to create such Integer variable correctly. I only add new variables with numbers or create optimization variables for objective funtion. Is it supposed to be a new optimization variable? Something like N=optimvar('N','Type','integer')?
Thanks in advance, you're saving my life.
Walter Roberson
Walter Roberson 2019-8-24
Yes, N=optimvar('N','Type','integer') is good.
If you have upper and/or lower bound on x then you can use it to figure out upper and lower bounds on N to make the optimization more efficient.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by