-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsurface.py
More file actions
84 lines (64 loc) · 1.82 KB
/
Copy pathsurface.py
File metadata and controls
84 lines (64 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
## GAMSSOURCE: https://www.gams.com/latest/noalib_ml/libhtml/noalib_surface.html
## LICENSETYPE: Demo
## MODELTYPE: NLP
Minimal surface problem
Find a function f that minimizes the array of its graph subject to some
constraints on the boundary of the domain of f.
Boyd, S., Vandenberghe, L., Convex Optimization, Cambridge University Press,
Cambridge, 2004.
"""
from __future__ import annotations
import gamspy.math as gams_math
from gamspy import (
Card,
Container,
Domain,
Model,
Ord,
Parameter,
Set,
Sum,
Variable,
)
from gamspy.math import sqr
def main():
m = Container()
# SETS #
X = Set(m, name="X", records=[f"I{i}" for i in range(1, 22)])
Y = Set(m, name="Y", records=[f"J{j}" for j in range(1, 22)])
inside = Set(m, name="inside", domain=[X, Y])
# Exclude i1 and i21 from inside
inside[X, Y].where[~((Ord(X) == 1) & (Ord(X) == Card(X)))] = True
# display inside
# SCALAR #
K = Parameter(m, name="K", records=10)
# VARIABLES #
f = Variable(m, name="f", domain=[X, Y], type="positive")
# Bounds on variables, initial conditions, fixing conditions:
f.up[X, Y] = 1
f.l[X, Y] = 1.0
f.fx[X, Y].where[(Ord(X) == 1) | (Ord(X) == Card(X))] = 1
# EQUATION #
objfun = (1 / sqr(K)) * Sum(
Domain(X, Y).where[inside[X, Y]],
gams_math.sqrt(
sqr((f[X + 1, Y] - f[X, Y]) / K)
+ sqr((f[X, Y + 1] - f[X, Y]) / K)
+ 1
),
)
surface = Model(
m,
name="surface",
equations=m.getEquations(),
problem="nlp",
sense="MIN",
objective=objfun,
)
surface.solve()
print("Objective Function Value: ", round(surface.objective_value, 4))
print("f(X,Y): \n", f.pivot())
# End surface
if __name__ == "__main__":
main()