import com.dashoptimization.ColumnType; import com.dashoptimization.DefaultMessageListener; import com.dashoptimization.IntHolder; import com.dashoptimization.XPRSconstants; import com.dashoptimization.XPRSenumerations.ObjSense; import com.dashoptimization.XPRSprob; import com.dashoptimization.XPRSprob.MapFunction; import com.dashoptimization.XPRSprob.MapFunctor; import java.util.ArrayList; /** Code example that uses a user function of type "map".
Xpress Optimizer Examples ========================= Maximize the area of polygon of N vertices and diameter of 1 The position of vertices is indicated as (rho,theta) coordinates where rho denotes the distance to the base point (vertex with number N) and theta the angle from the x-axis. (c) 2021-2025 Fair Isaac CorporationPolygon example: maximise the area of an N sided polygon *** Demonstrating using a simple map (R->R) userfunction ***
Variables:
rho : 0..N-1 ! Distance of vertex from the base point
theta : 0..N-1 ! Angle from x-axis
Objective:
(sum (i in 1..N-2) (rho(i)*rho(i-1)*sin(theta(i)-theta(i-1)))) * 0,5
Constraints:
Vertices in increasing degree order:
theta(i) >= theta(i-1) +.0001 : i = 1..N-2
Boundary conditions:
theta(N-1) <= Pi
0,1 <= rho(i) <= 1 : i = 0..N-2
Third side of all triangles <= 1
rho(i)^2 + rho(j)^2 - rho(i)*rho(j)*2*cos(theta(j)-theta(i)) <= 1 : i in 0..N-3, j in i..N-2
*/
public final class PolygonMap {
/** User function that maps a double to a double.
* This just forwards to sin().
*/
private static final MapFunctor mySin = new MapFunctor() {
@Override
public double map(double value) {
return Math.sin(value);
}
};
/** User function that maps a double to a double.
* This just forwards to cos(). Here we just use the
* existing cos() function without explicitly creating
* a wrapper. Any function that takes a double and
* returns a double would do here.
*/
private static final MapFunctor myCos = new MapFunctor() {
@Override
public double map(double value) {
return Math.cos(value);
}
};
public static void main(String[] args) {
try (XPRSprob prob = new XPRSprob(null)) {
prob.addMessageListener(new DefaultMessageListener());
// Number of sides of the Polygon
int nSide = 5;
// Theta
int[] theta = prob.addColumns(nSide - 1)
.withUB(Math.PI)
.withName(i -> String.format("THETA%d", i + 1))
.toArray();
// Rho
int[] rho = prob.addColumns(nSide - 1)
.withLB(0,01)
.withUB(1)
.withName(i -> String.format("RHO%d", i + 1))
.toArray();
// Add the user functions
MapFunction sin = prob.nlpAddUserFunction("mySin", 0, mySin);
MapFunction cos = prob.nlpAddUserFunction("myCos", 0, myCos);
// Objective function. We build the objective function as
// a formula in infix notation. See below for submitting a
// formula as string.
// Tokens are always integers, while values may be integers
// (for example operator or delimiter constants) or double
// values (actual numbers). That is why the `val` list has
// elements of type Number.
ArrayList