JuMP @constraint - "begin end" block not working

Hi,

I am trying to set some constraints in JuMP within Pluto. If I specify each constraint on a separate line then I don’t get an error message but if I try to put all the constraints in a begin-end block then Pluto gives me the following error.

LoadError: At C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:31: `@constraint(model, begin

#= C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:32 =#

(default_col[i = 1:7], x[i, 8] == pd_table[1, i])

#= C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:33 =#

(default_row[i = 1:7], x[8, i] == 0)

#= C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:34 =#

(default_row[8], x[8, 8] == 1)

#= C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:35 =#

(sum_of_rows[i = 1:8], sum(x[i, :]) == 1)

end)`: Code block passed as constraint. Perhaps you meant to use @constraints instead?

in expression starting at C:\Users\shash\Documents\Actuarial\Julia\MA_FS\calibrating_trans_matrix.jl#==#ed816000-2055-11eb-273a-f1207b058a14:31

error(::String, ::String)@error.jl:42
_macro_error(::Symbol, ::Array{Any,1}, ::LineNumberNode, ::String)@macros.jl:924
(::JuMP.var"#_error#73"{Symbol,LineNumberNode})(::String)@macros.jl:390
_constraint_macro(::Tuple{Symbol,Expr}, ::Symbol, ::typeof(JuMP.parse_constraint_expr), ::LineNumberNode)@macros.jl:424
@constraint(::LineNumberNode, ::Module, ::Vararg{Any,N} where N)@macros.jl:514

My code is as follows:

function calibrate_trans_rate(filepath_pd)
	
	# Get PD values
	pd_table = zeros(Float64, 30, 7)
	open(filepath_pd, "r") do fp
		for row in 1:30
			line = readline(fp)
			pd_table[row, :] .= parse.(Float64, split(line, ",")) 
		end
	end

	# Set up solver
	model = Model()  # need to decide what solver to use
	
	# Set variable to be transition values
	@variable(model, 0 <= x[1:8, 1:8] <= 1)
	
	### This works
	# Default column is known
	#@constraint(model, default_col[i = 1:7], x[i, 8] == pd_table[1, i])
	
	# Default row is known
	#@constraint(model, default_row[i = 1:7], x[8, i] == 0)
	#@constraint(model, default_row[8], x[8, 8] == 1)
	
	# Sum of all row is equal to 1 (probabilities)
	#@constraint(model, sum_of_rows[i = 1:8], sum(x[i, :]) == 1)
	
	###
	
	@constraint(model, begin
			default_col[i = 1:7], x[i, 8] == pd_table[1, i]
			default_row[i = 1:7], x[8, i] == 0
			default_row[8], x[8, 8] == 1
			sum_of_rows[i = 1:8], sum(x[i, :]) == 1
		end)
	
	# Define statistic to minimize
	
	# Calculate holdings at various year
	x_power = [x^i for i = 1:30]
	
	# Calculate sum of squared difference between calibrated PD and actual PD
	sum_of_sq_diff = sum((x_power[i][1:7, 8] - pd_table[2, :]).^2)
	
	@objective(model, Min, sum_of_sq_diff)
	
	JuMP.optimize!(model)

    obj_value = JuMP.objective_value(model)
    trans_matrix = JuMP.value(x)
end

Any idea what I am doing wrong?

Try using @constraints instead.

1 Like

Sorry, new to programming - the answer was right there:

“Perhaps you meant to use @constraints instead?”