title: Piecewise function Pyomo
body: <p>I want to use piecewise function to modify values in my pyomo model :</p>
  <ul>
  <li>2 -&gt; 1</li>
  <li>-3 -&gt; 0</li>
  <li>5 -&gt; 0.5</li>
  <li>0 -&gt; 0</li>
  </ul>
  <p>I have a variable called model.test taking values in [-3,0,2,5] :</p>
  <pre><code>h_change : Size=11, Index=vehicules
      Key : Lower : Value : Upper : Fixed : Stale : Domain
        0 :    -3 :   0.0 :     5 : False : False : Integers
        1 :    -3 :   2.0 :     5 : False : False : Integers
        2 :    -3 :   2.0 :     5 : False : False : Integers
        3 :    -3 :   2.0 :     5 : False : False : Integers
        4 :    -3 :   0.0 :     5 : False : False : Integers
        5 :    -3 :  -3.0 :     5 : False : False : Integers
        6 :    -3 :   0.0 :     5 : False : False : Integers
        7 :    -3 :   0.0 :     5 : False : False : Integers
        8 :    -3 :   0.0 :     5 : False : False : Integers
        9 :    -3 :   0.0 :     5 : False : False : Integers
       10 :    -3 :   5.0 :     5 : False : False : Integers
  </code></pre>
  <p>I used this next piecewise constraint :</p>
  <pre><code>model.test = Var(model.vehicules, initialize=0, bounds=(0,1))
  
  DOMAIN_PTS = [-3.,0.,2.,5.]
  RANGE_PTS  = [0.,0.,1.,0.5]
  
  model.con = Piecewise(model.vehicules, #Indexation
                          model.test, # Var res
                          model.h_change, #Var observées
                            pw_pts=DOMAIN_PTS,
                            pw_constr_type='EQ',
                            f_rule=RANGE_PTS)
  </code></pre>
  <p>My variable model.test resulting is like this :</p>
  <pre><code>test : Size=11, Index=vehicules
      Key : Lower : Value               : Upper : Fixed : Stale : Domain
        0 :     0 :                 0.0 :     1 : False : False :  Reals
        1 :     0 : 0.20000000000000004 :     1 : False : False :  Reals
        2 :     0 : 0.20000000000000004 :     1 : False : False :  Reals
        3 :     0 : 0.20000000000000004 :     1 : False : False :  Reals
        4 :     0 :                 0.0 :     1 : False : False :  Reals
        5 :     0 :                 0.0 :     1 : False : False :  Reals
        6 :     0 :                 0.0 :     1 : False : False :  Reals
        7 :     0 :                 0.0 :     1 : False : False :  Reals
        8 :     0 :                 0.0 :     1 : False : False :  Reals
        9 :     0 :                 0.0 :     1 : False : False :  Reals
       10 :     0 :  0.5000000000000001 :     1 : False : False :  Reals
  </code></pre>
  <p>I don't understand why my 2 values (index 1, 2, 3) became 0.2 instead of 1.</p>
  <p><strong>Can you help me to understand what is wrong with my code ?</strong></p>
  <hr />
  <h2>UPDATE :</h2>
  <hr />
  <p>I tried this code to test (from <a href="http://yetanothermathprogrammingconsultant.blogspot.com/2019/02/piecewise-linear-functions-and.html" rel="nofollow noreferrer">http://yetanothermathprogrammingconsultant.blogspot.com/2019/02/piecewise-linear-functions-and.html</a>)</p>
  <pre><code>#
  # expected solution X=5, Y=6
  #
  
  xdata = [1., 3., 6., 10.]
  ydata  = [6.,2.,8.,7.]
  
  from pyomo.core import *
  
  model = ConcreteModel()
  
  model.X = Var(bounds=(1,10))
  model.Y = Var(bounds=(0,100))
  
  model.con = Piecewise(model.Y,model.X,
                        pw_pts=xdata,
                        pw_constr_type='EQ',
                        f_rule=ydata,
                        pw_repn='SOS2')
  
  # see what we get for Y when X=5
  def con2_rule(model):
      return model.X==5
  
  model.con2 = Constraint(rule=con2_rule)
  
  model.obj = Objective(expr=model.Y, sense=maximize)
  </code></pre>
  <p>But with my solver (Couenne) I got</p>
  <blockquote>
  <p>Y=7.600000072980164.</p>
  </blockquote>
  <p>I think there is then a issue with my solver and not with the PieceWise code ...</p>