@IF( logical_condition, true_result, false_result)
如果逻辑表达式为真,则返回真的结果,为假,返回假的结果;返回值可以为其他变量赋值
constValue = if(pi > e, pi, e);
!返回值是pi
@IF( logical_condition, true_result, false_result) The @IF function evaluates logical_condition and, if true, returns true_result, otherwise it returns false_result. For example, consider the following simple model that uses @IF to compute fixed production costs: MIN = COST; COST = XCOST + YCOST; XCOST = @IF( X #GT# 0, 100, 0) + 2 * X; YCOST = @IF( Y #GT# 0, 60, 0) + 3 * Y; X + Y >= 30; Model: IFCOST We produce two products—X and Y. We want to minimize total cost, subject to producing at least 30 total units of X and Y. If we produce X, there is a fixed charge of 100 along with a variable cost of 2. Similarly, for Y, these respective values are 60 and 3. We use the @IF function to determine if either of the products are being produced in order to apply the relevant fixed cost. This is accomplished by testing to see if their production levels are greater than 0. If so, we return the fixed cost value, otherwise, we return zero. Experienced modelers know that, without the benefit of an @IF function, modeling fixed costs requires invoking some "tricks" using binary integer variables. The resulting models are not as intuitive as models constructed using @IF. The caveat, however, is that the @IF function is not a linear function. At best, the graph of an @IF function will be piecewise linear. In our current example, the @IF functions are piecewise linear with a discontinuous break at the origin. As we discuss in the chapter On Mathematical Modeling, it is always best to try and keep a model linear. Barring this, it is best for all functions in a nonlinear model to be continuous. Clearly, then, the @IF function is a problem in that it violates both these conditions. Thus, models containing @IF functions may be tough to solve to global optimality. Fortunately, LINGO has two options that can help overcome the difficult nature of models containing @IF functions—linearization and global optimization. To illustrate the difficulty in solving models with discontinuous functions such as @IF, we will solve our example model with both linearization and global optimization disabled. When we do this, we get the following solution: Local optimal solution found at iteration: 42 Objective value: 160.0000
Variable Value COST 160.0000 XCOST 160.0000 YCOST 0.000000 X 30.00000 Y 0.000000 This solution involves producing only X at a total cost of 160. Clearly, this is merely a locally optimal point, given that producing only Y and not X will result in a lower total cost of 150. In order to find the globally optimal point we must resort to either the linearization or global optimization features in LINGO. Briefly, linearization seeks to reformulate a nonlinear model into a mathematically equivalent linear model. This is desirable for two reasons. First, and most important, linear models can always be solved to global optimality. Secondly, linear models will tend to solve much faster than equivalent nonlinear models. Unfortunately, linearization can’t always transform a model into an equivalent linear state, in which case, it may be of no benefit. Fortunately, our sample model can be entirely linearized. To enable the linearization option, run the LINGO|Options command and set the Linearization Degree to High on the General Solver tab. Global optimization breaks a model down into a series of smaller, local models. Once this series of local models has been solved, a globally optimal solution can be determined. To enable global optimization, run the LINGO|Options command, select the Global Solver tab, then click on the Global Solver checkbox. Note that the global solver is an add-on option to LINGO. The global solver feature will not be enabled for some installations. Run the Help|About LINGO command to determine if your installation has the global solver capability enabled. Regardless, whether using the linearization option or the global solver, LINGO obtains the true, global solution: Global optimal solution found at iteration: 6 Objective value: 150.0000
Variable Value COST 150.0000 XCOST 0.000000 YCOST 150.0000 X 0.000000 Y 30.00000
@WARN( 'text', logical_condition) This function displays the message ‘text’ if the logical_condition is met. This feature is useful for verifying the validity of a model's data. In the following example, if the user has entered a negative interest rate, the message "INVALID INTEREST RATE" is displayed: ! A model of a home mortgage;
DATA: ! Prompt the user for the interest rate, years, and value of mortgage. We will compute the monthly payment; YRATE = ?; YEARS = ?; LUMP = ?; ENDDATA
! Number of monthly payment; MONTHS = YEARS * 12;
! Monthly interest rate; ( 1 + MRATE) ^ 12 = 1 + YRATE;
! Solve next line for monthly payment; LUMP = PAYMENT * @FPA( MRATE, MONTHS);
! Warn them if interest rate is negative @WARN( 'INVALID INTEREST RATE', YRATE #LT# 0); @USER( user_determined_arguments) The user can supply this in an external DLL or object code file. For a detailed example on the use of @USER, see User Defined Functions. |