# Examodels: using constraint as empty object for constraint!

**URL:** <https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911>\
**Category:** Optimization (Mathematical)\
**Created:** [October 4, 2024, 12:54pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911 "2024-10-04T12:54:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [October 4, 2024, 12:54pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911/1 "2024-10-04T12:54:38Z")

</div>

I am trying to implement control neutrality constraint. That is, a constraint that requires that if some generator increased power that others decrease (sum is zero).

In this demo test case there are only two variables with data stored in data.cp\_gen which is 2 element vector of NamedTuples.

```julia
		con_en_neutrality1 = constraint(w,1;)

		con_en_neutrality2 = constraint!(w, con_en_neutrality1, Pgen[v.i] - v.pg for v in data.cp_gen)

```

Solution reveals that the constraint is not satisfied despite full solver convergence (using CPU backend).

`sum([solution(res,Pgen)[v.i]-v.pg for v in data.cp_gen])`  
results in  
`-0.031260009999725696`

That is 3MW difference instead of 0.

Do you know what could be wrong here @sshin23 ?

---

<div class="post-metadata">

**Author:** ![sshin23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sshin23/32/205225_2.png) [@sshin23](https://discourse.julialang.org/u/sshin23)\
**Post date:** [October 4, 2024, 1:20pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911/2 "2024-10-04T13:20:03Z")

</div>

Could you share the full code? One issue I notice is that `constraint!` needs to be expressed in terms of iterator of `Pair`. So you might need to make changes, e.g.,

```julia
con_en_neutrality2 = constraint!(w, con_en_neutrality1, v.i => Pgen[v.i] - v.pg for v in data.cp_gen)

```

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [October 4, 2024, 1:26pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911/3 "2024-10-04T13:26:41Z")

</div>

Unfortunately I can not share the full code (hundreds of lines of code), but this part is very isolated model wise. The issue is only with this constraint. If I remove it, I get exactly the same result with some of my other ampl codes.

I tried using

```julia
con_en_neutrality2 = constraint!(w, con_en_neutrality1, 1 => Pgen[v.i] - v.pg for v in data.cp_gen)

```

as neutrality constraint is single constraint, but I am receiving error:

> LoadError: MethodError: objects of type Int64 are not callable  
> Maybe you forgot to use an operator such as \*, ^, %, / etc. ?

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [October 4, 2024, 1:36pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911/4 "2024-10-04T13:36:55Z")

</div>

I used your approach, but had to put constant 1 into named tuple. Using `1` directly results in an error as shown in my last reply. Not sure why it does not, or should it accept direct `1`.

`con_en_neutrality2 = constraint!(w, con_en_neutrality1, v.one => Pgen[v.i] - v.pg for v in data.cp_gen)`

---

<div class="post-metadata">

**Author:** ![sshin23](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sshin23/32/205225_2.png) [@sshin23](https://discourse.julialang.org/u/sshin23)\
**Post date:** [October 7, 2024, 2:09pm UTC](https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for-constraint/120911/5 "2024-10-07T14:09:38Z")

</div>

This might be a bug in ExaModels. Thanks for reporting this. An issue is opened.

> <https://github.com/exanauts/ExaModels.jl/issues/110>
>
> https://discourse.julialang.org/t/examodels-using-constraint-as-empty-object-for…-constraint/120911/3
