Need Help With Conditional Logic IF Y = 0 THEN Z = 0 Where Z is Integer

36 views
Skip to first unread message

Fulton Loebel

unread,
Sep 19, 2017, 6:31:00 AM9/19/17
to AMPL Modeling Language
Suppose you have IF Y = 0 THEN Z = 0

If both Y and Z are binary this becomes two OR statements

Y >= 1 - M * YTEMP
Z <= 0 + M * (1 - YTEMP)    where YTEMP is binary and M is BigM

However, my problem is that Z is integer. 
In fact, Z = A - B + 1 where A and B are integer.

In my example, A is the first arc counter and B is the second arc counter of network flow of arc A into arc B.

How can I formulate Z = 0 when Z is integer?

Any help would be appreciated.

Regards
Fulton Loebel

duanem...@gmail.com

unread,
Sep 19, 2017, 2:47:42 PM9/19/17
to AMPL Modeling Language
I'm not exactly sure what you are asking....trying to define the starting network node (z=0) in a sequence?

I don't know if this will help, but you might look at Gabor Pataki' 2003 Teaching Integer Programming FOrmulation Using the Traveling Salesman Problem,

http://epubs.siam.org/doi/pdf/10.1137/S00361445023685

where he defines an additional array that defince the node order and then uses that in a constraint.  I used this when I was experiment with the Dantzig Fulkerson Johnson subtour elimination method for a vehicle delivery problem.


#========Section of AMPL code follows========  "u" is an array that defines the node visited order  "x" is the network node connection binary matrix.
# Dantzig, Fulkerson, Johnson (DFJ)
subject to NoSubtour1 {i in Nodes, j in Nodes: i<>j }:
   x[i,j] + x[j,i] <= 1;

# NoSubtour1, doesn't work by itself. Need Pickup before drop-off
#subject to NoSubtour1 {i in Nodes, j in Nodes: i<>j and i>=2 and j>=2 }:
#   u[i] - u[j] + N*x[i,j] <= N-1;
# NoSubtour2, doesn't work by itself
subject to NoSubtour2 {i in Nodes: i>=2}:
   u[i] <= N-1 - (N-2)*x[1,i];              #First Connection
# NoSubtour3
subject to NoSubtour3 {i in Nodes: i>=2}:
   u[i] >= 1 + (N-2)*x[i,1];                #Last Connection




---------------------------------------------------------------------------------------------------------------

Robert Fourer

unread,
Sep 19, 2017, 4:21:13 PM9/19/17
to am...@googlegroups.com

Fulton Loebel

unread,
Sep 19, 2017, 6:07:37 PM9/19/17
to AMPL Modeling Language
Below is the code I am using for my problem.  I have developed my own preprocessor so this syntax will not look familiar.  I also attached some additional pages which better describe what I am doing.  My concept uses only one dimensional arrays of ARCS rather than two dimensional arrays of NODE to NODE.   

LU1, LU2, ..., LU6 are not automatically being solved for.  I populate them after the solution completes.  I want to populate them within the solution.  They represent the order of the ARCS.



! MIN COST FLOW PROBLEM WITH 6 NODES AND 8 ARCS AND VISIT EACH NODE AT LEAST ONCE (INCLUDING SINGLE BACKTRACKING)

 Model

 Constants
    ARC_IN_A = 1
    ARC_IN_B = 2
    ARC_B_A = 3
    ARC_A_C = 4
    ARC_B_C = 5
    ARC_B_D = 6
    ARC_D_B = 7
    ARC_C_OUT = 8

    ARCS = 8
    N = 8

    M_TEMP = 2

    LU1 = 2    ! lookup position  arc 2 has order 1
    LU2 = 6
    LU3 = 7
    LU4 = 3
    LU5 = 4
    LU6 = 8    ! only 6 nonzero arcs in optimum solution

 End constants

 Variables

  INLINE, CHECK_SHORT_ARRAY,  NONE

  INLINE, INTEGER,  I_MILES[1:N],      ARC DISTANCE IN MILES, 1D ARRAY,   GIVEN
  INLINE, INTEGER,  I_HOURS[1:N],      ARC TIME IN HOURS,     1D ARRAY,   GIVEN
  INLINE, INTEGER,  X[1:N],            X=1 IF ARC ACTIVE,     1D ARRAY,   UNKNOWN
  INLINE, INTEGER,  ARC_SEQ[1:N],      ARC SEQUENCE,          1D ARRAY,   UNKNOWN
  INLINE, INTEGER,  ARC_CUM[1:N],      ARC CUMULATIVE TIME,   1D ARRAY,   UNKNOWN
  INLINE, INTEGER,  ARC_MILES,         TOTAL MILES ALL ARCS,  1D ARRAY,   UNKNOWN
  INLINE, INTEGER,  ARC_HOURS,         TOTAL HOURS ALL ARCS,  1D ARRAY,   UNKNOWN

! INLINE, INTEGER,  U[1:N],            ARRAY OF POSITIONS OF NODE I,     UNDER DEVELOPMENT,   UNKNOWN
  INLINE, BINARY,   Y[1:M_TEMP],       ARRAY OF BINARY Y,                BOOLEAN,             UNKNOWN
  INLINE, INTEGER,  M[1:M_TEMP],       ARRAY OF BIG M,                   BIG_M,               UNKNOWN

  End Variables

  Equations

   X[1:N] <= 1

  ! INLINE, ARRAY1D, ARC_SEQ,        0,      1,   4,      5,    0,     2,   3,      6
  ARC_CUM[1] = ARC_SEQ[LU1]                                                                    ! 1 = 1
  ARC_CUM[2] = ARC_SEQ[LU1] + ARC_SEQ[LU2]                                                       ! 1 2 = 3
  ARC_CUM[3] = ARC_SEQ[LU1] + ARC_SEQ[LU2] + ARC_SEQ[LU3]                                          ! 1 2 3 = 6
  ARC_CUM[4] = ARC_SEQ[LU1] + ARC_SEQ[LU2] + ARC_SEQ[LU3] + ARC_SEQ[LU4]                             ! 1 2 3 4 = 10
  ARC_CUM[5] = ARC_SEQ[LU1] + ARC_SEQ[LU2] + ARC_SEQ[LU3] + ARC_SEQ[LU4] + ARC_SEQ[LU5]                ! 1 2 3 4 5 = 15
  ARC_CUM[6] = ARC_SEQ[LU1] + ARC_SEQ[LU2] + ARC_SEQ[LU3] + ARC_SEQ[LU4] + ARC_SEQ[LU5] + ARC_SEQ[LU6]   ! 1 2 3 4 5 6 = 21
  ARC_CUM[7] = 0  ! only 6 nonzero arcs
  ARC_CUM[8] = 0  ! only 6 nonzero arcs

 ! now calculate total miles ...
  ARC_MILES = I_MILES[LU1] + I_MILES[LU2] + I_MILES[LU3] + I_MILES[LU4] + I_MILES[LU5] + I_MILES[LU6]

 ! now calculate total hours ...
  ARC_HOURS = I_HOURS[LU1] + I_HOURS[LU2] + I_HOURS[LU3] + I_HOURS[LU4] + I_HOURS[LU5] + I_HOURS[LU6]

   minimize  I_MILES[1] * X[1] + I_MILES[2] * X[2] + I_MILES[3] * X[3] + I_MILES[4] * X[4] + I_MILES[5] * X[5] + I_MILES[6] * X[6] + I_MILES[7] * X[7] + I_MILES[8] * X[8]

!  FLOW IN = FLOW OUT

   X[ARC_IN_A] + X[ARC_IN_B] = 1                 ! NODE IN
   X[ARC_IN_A] + X[ARC_IN_B] = X[ARC_A_C]        ! NODE A
   X[ARC_IN_B] + X[ARC_D_B] = + X[ARC_B_A] + X[ARC_B_C] + X[ARC_B_D]   ! NODE B
   X[ARC_B_C] + X[ARC_A_C] = X[ARC_C_OUT]        ! NODE C
   X[ARC_B_D] = X[ARC_D_B]                       ! NODE D
   X[ARC_C_OUT] = 1                              ! NODE OUT

 ! ENSURE NO SUBTOURS ... SUM OF FLOW INTO EACH NODE >= 1

   X[ARC_IN_A] + X[ARC_B_A] >= 1              ! NODE A
   X[ARC_IN_B] + X[ARC_D_B] >= 1              ! NODE B
   X[ARC_A_C] + X[ARC_B_C] >= 1               ! NODE C
   X[ARC_B_D] >= 1                            ! NODE D
   X[ARC_C_OUT] >= 1                          ! NODE OUT

 ! ENSURE NO SUBTOURS ... SUM OF FLOW OUT OF EACH NODE >= 1

   X[ARC_IN_A] + X[ARC_IN_B] >= 1                ! NODE IN
   X[ARC_A_C] >= 1                               ! NODE A
   X[ARC_B_A] + X[ARC_B_C] + X[ARC_B_D] >= 1     ! NODE B
   X[ARC_C_OUT] >= 1                             ! NODE C
   X[ARC_B_D] >= 1                               ! NODE D

 ! ENSURE NO SUBTOURS ... CONSIDER BACK TRACKING ...

  X[ARC_B_D] + X[ARC_D_B] <= 2    ! only backtrack once between NODE B and NODE D

 ! IF X[ARC_B_D] = X[ARC_D_B]
 ! THEN DO NOT COUNT THE BACK TRACKS ...
 ! THEN X[ARC_IN_B] >= 1                         ! B

  X[ARC_B_D] + X[ARC_D_B] <= 1 + M[1] * Y[1]    ! EITHER
  X[ARC_IN_B] >= 1 - M[1] * ( 1 - Y[1] )        ! OR

! BIG M VALUES

  M[1:M_TEMP]   = 100

    ! ARC NAMES
  INLINE, aRowName,               IN_A,  IN_B,  B_A,   A_C,  B_C,   B_D,  D_B,   C_OUT

  INLINE, ARRAY1D, I_MILES,        40,     60,  30,     20,   50,    20,  10,     70
  INLINE, ARRAY1D, I_HOURS,         4,      6,   3,      2,    5,     2,   1,      7

! this is determined after the solution completes now
! i want to calculate this array while the solution is created in the application
  INLINE, ARRAY1D, ARC_SEQ,         0,      1,   4,      5,    0,     2,   3,      6

  End Equations

  End Model


Discussuion of Problem.PDF

Robert Fourer

unread,
Sep 21, 2017, 11:06:43 AM9/21/17
to am...@googlegroups.com
By saying that you want to "populate" LU1, LU2, ..., LU6 "within the solution", do you mean that you want the solver to determine values for them? Then you just need to define them as variables in the AMPL model. It is hard to say more, however, since I do not know what AMPL model is being built by your preprocessor.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages