| |||||||||||||||||||||
Polygon construction under constraints Description The set of examples describe models to create a polygon with various constraints and goals:
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
Data Files polygon3.mos (!********************************************************************* Mosel NL examples ================= file polygon3.mos ````````````````` 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. -- Formulation using a multi-valued Mosel user function -- (c) 2008 Fair Issac Corporation Creation: 2002, rev. Feb. 2013 *********************************************************************!) model "Polygon 3" uses "mmxnlp" parameters N=5 ! Number of vertices SOLVER=0 ! 0: SLP, 1: Knitro end-parameters declarations RN = 1..N Area: nlctr rho : array(RN) of mpvar ! Distance of vertex from the base point theta : array(RN) of mpvar ! Angle from x-axis D: array(RN,RN) of nlctr ! Limit on side length FunctionArg: array(RN,{"rho","theta"}) of nlctr ! User function arguments AreaFunction : userfunc ! User function definition k: integer end-declarations ! Objective: sum of areas. Definition of a user function AreaFunction := userfuncMosel("MoselArea") ! Create function arguments forall (i in 1..N) do FunctionArg(i, "rho") := rho(i) FunctionArg(i, "theta") := theta(i) end-do ! Use the Mosel user function in a formula for the objective Area := F(AreaFunction, FunctionArg, 1) ! Bounds and start values for decision variables forall (i in 1..N-1) do rho(i) >= 0.1 rho(i) <= 1 setinitval(rho(i),4*i*(N + 1 - i)/((N+1)^2)) setinitval(theta(i),M_PI*i/N) end-do ! Third side of all triangles <= 1 k := 2; forall(i in 1..N-2, j in i+1..N-1, k as counter) D(i,j) := F(AreaFunction, FunctionArg, k) <= 1 ! Vertices in increasing order forall (i in 2..N-1) theta(i) >= theta(i-1) +.01 ! Boundary conditions (last vertex above x-axis) theta(N-1) <= M_PI ! Uncomment to display user function info ! userfuncinfo(AreaFunction) ! Optional parameter settings setparam("xnlp_verbose", true) ! Enable XNLP output log setparam("xnlp_solver", SOLVER) ! Select the solver ! Solve the problem maximise(Area) ! Solution output writeln("Area = ", getobjval) forall (i in 1..N-1) writeln("V",i,": r=",getsol(rho(i))," theta=",getsol(theta(i))) ! **** Definition of the Mosel user function **** public procedure MoselArea(I: array(Indices: range, Types: set of string) of real, CalcValues: array(RetIndices: set of integer) of real) ! Area CalcValues(1) := (sum (i in 2..N-1) (I(i,"rho")*I(i-1,"rho")*sin(I(i,"theta")-I(i-1,"theta")))) * 0.5 ! Distances k := 2 forall (i in 1..N-2,j in i+1..N-1, k as counter) CalcValues(k) := I(i,"rho")^2 + I(j,"rho")^2 - I(i,"rho")*I(j,"rho")*2*cos(I(j,"theta")-I(i,"theta")) end-procedure end-model | |||||||||||||||||||||
© Copyright 2023 Fair Isaac Corporation. |