Forum Discussion

FICO's avatar
FICO
Community Manager
5 years ago

How to debug E-108: Logical expression expected here for If statement.

I am trying to minimize the absolute difference between some value dependent on "g" and some constant. I have attached some partial code that I would like to fix. The thing is that I just don't understand why `Points_Total(g) - mean_points >= 0` is not considered a logical expression.

  
 
  forall(g in Groups) Points_Total(g):= sum(n in Nations) Groupings(g,n)*Points(n)
 
  mean_points := (sum(g in Groups) Points_Total(g))/8
 
  forall(g in Groups) do
  if Points_Total(g) >= mean_points then
    points_diff(g) := Points_Total(g) - mean_points
  else
    points_diff(g) := mean_points - Points_Total(g)
  end-if
  end-do
  
  total_error := sum(g in Groups)(points_diff(g))

6 Replies

  • FICO's avatar
    FICO
    Community Manager

    Could you share the declaration of the different arrays and scalars that are used in your code extract?

  • FICO's avatar
    FICO
    Community Manager
      declarations
        Groups = 1..8
        Nations: set of string
        Points: array(Nations) of real
        Groupings: array(Groups, Nations) of mpvar
      end-declarations
     
      forall(g in Groups, n in Nations) Groupings(g,n) is_binary
  • FICO's avatar
    FICO
    Community Manager

    I would suggest that you simply use the 'abs' constraint relation to state lines 7-13 of your first code snippet:

    forall(g in Groups) points_diff(g):=abs(Points_Total(g) - mean_points)

    or if you declare 'points_diff' as mpvar:

    forall(g in Groups) points_diff(g)=abs(Points_Total(g) - mean_points)

    Note that you need to work with the 'mmxnlp' module in place of 'mmxprs', but the resulting model will still be handled by the MIP solver.

    For further discussion of formulation alternatives for absolute value constraints please refer to the 'MIP formulations' whitepaper: https://www.fico.com/fico-xpress-optimization/docs/latest/mipform/dhtml/chap2s1_sec_ssecabsval.html

  • FICO's avatar
    FICO
    Community Manager

    I think the constraint works now, thanks!

    It takes about 3-4 min to compute but I really needed that constraint implemented.

  • FICO's avatar
    FICO
    Community Manager

    And just out of curiosity, do you have any idea why my first idea did not work. More precisely, why:

    Points_Total(g) - mean_points >= 0

    was not considered logical expression?

  • FICO's avatar
    FICO
    Community Manager

    'Points_Total(g)' is of type linctr and so is 'mean_points', so this whole expression is of type 'linctr'. It is not possible to use a 'linctr' within an 'if' for stating a logical constraint: you need to work with an 'implies' relation in this case (using package 'advmod').