Reputation: 1
I am writing a program in Julia that maximizes the size of an image block based on a constraint (with ** around it) as seen in the code below:
using Images, Colors, JuMP, Gurobi
m = Model(solver=GurobiSolver(OutputFlag=0))
Idiff=load("Idiff.png")
a=16; b=16;
Block=Idiff[1:a,1:b]; #initial block size
squareBlock=sum(Block.^2) #sum of squares of block
@variable(m,x>=0,Int) #variable determining size of block in x
@variable(m,y>=0,Int) #variable determining size of block in y
@constraint(m,x<=192,Int)
@constraint(m,y<=144,Int)
**@constraint(m,abs(sum(Idiff[1:x,1:y]).^2-squareBlock)<=.1) #attempt to use
the variable I am optimizing as an index**
@objective(m,Max,x+y)
When I try to use the variable I am optimizing as an index, I receive an error. Is there a way I could work around this issue?
Upvotes: 0
Views: 221
Reputation: 69829
I am not sure if your constraint is correct (it seems it is not, but I am not an expert in the field).
Here is the solution using MIP solver and Bin
variables (I had to generate fake data as you provided no source; also I have CPLEX not Gurobi, but this should not be a big difference):
using JuMP, CPLEX
m = Model(solver=CplexSolver())
mx = 192
my = 144
Idiff = rand(mx, my)
a=16; b=16;
Block=Idiff[1:a,1:b]; #initial block size
squareBlock=sum(Block.^2) #sum of squares of block
@variable(m, xi[1:mx], Bin)
@variable(m, yi[1:my], Bin)
for i in 1:mx-1
@constraint(m, xi[i] >= xi[i+1])
end
for i in 1:my-1
@constraint(m, yi[i] >= yi[i+1])
end
@constraint(m,sum(Idiff[i,j]*xi[i]*yi[j] for i in 1:mx for j in 1:my) <= sqrt(squareBlock+0.1))
@constraint(m,sum(Idiff[i,j]*xi[i]*yi[j] for i in 1:mx for j in 1:my) >= sqrt(squareBlock-0.1))
@objective(m,Max,sum(xi)+sum(yi))
solve(m)
However, for this problem (as it is relatively small and effectively has only 2 variables and 1 constraint) it is probably faster not to use a MIP solver, but rather do a brute force search over all possible values of x
and y
(you can do cut-off pretty fast as the crucial constraint is strictly increasing with x
and y
so if you are above ).
Upvotes: 1