Skip to content

Commit

Permalink
update objective value checks according to new default solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdullahsoyturk committed Jul 5, 2024
1 parent b5a1d16 commit fd0e20d
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 59 deletions.
51 changes: 38 additions & 13 deletions models/MeanVarMip/MeanVarMip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import annotations

import os
from pathlib import Path

from gamspy import (
Expand All @@ -31,7 +32,9 @@


def main():
m = Container()
m = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# Read from MeanVarMip.gdx the data needed to run the mean-variance model
m.read(
Expand Down Expand Up @@ -78,7 +81,9 @@ def main():
domain=i,
description="Indicator variable for assets included in the portfolio",
)
PortVariance = Variable(m, name="PortVariance", description="Portfolio variance")
PortVariance = Variable(
m, name="PortVariance", description="Portfolio variance"
)
PortReturn = Variable(m, name="PortReturn", description="Portfolio return")

# In case short sales are allowed these bounds must be set properly.
Expand Down Expand Up @@ -166,7 +171,9 @@ def main():
if lamda_loop > 1:
break
lamda[...] = lamda_loop
MeanVarMip.solve(options=Options(minlp="SBB", relative_optimality_gap=0))
MeanVarMip.solve(
options=Options(minlp="SHOT", relative_optimality_gap=0)
)
MeanVarianceMIP += f"{round(lamda_loop,1)},{round(MeanVarMip.objective_value,4)},{round(PortVariance.records.level[0],4)},{round(PortReturn.records.level[0],4)},"
x_recs = [str(round(x_rec, 4)) for x_rec in x.records.level.tolist()]
MeanVarianceMIP += ",".join(x_recs)
Expand Down Expand Up @@ -273,15 +280,23 @@ def main():
if lamda_loop > 1:
break
lamda[...] = lamda_loop
MeanVarWithCost.solve(options=Options(minlp="SBB", relative_optimality_gap=0))
MeanVarWithCost.solve(
options=Options(minlp="SHOT", relative_optimality_gap=0)
)
MeanVarianceWithCost += f"{round(lamda_loop,1)},{round(MeanVarWithCost.objective_value,4)},{round(PortVariance.records.level[0],4)},{round(PortReturn.records.level[0],4)},"
x0_recs = [str(round(x_rec, 4)) for x_rec in x_0.records.level.tolist()]
x1_recs = [str(round(x_rec, 4)) for x_rec in x_1.records.level.tolist()]
x0_recs = [
str(round(x_rec, 4)) for x_rec in x_0.records.level.tolist()
]
x1_recs = [
str(round(x_rec, 4)) for x_rec in x_1.records.level.tolist()
]
MeanVarianceWithCost += ",".join(x0_recs) + ","
MeanVarianceWithCost += ",".join(x1_recs) + "\n"
lamda_loop += 0.1

with open("MeanVarianceWithCost.csv", "w", encoding="UTF-8") as FrontierHandleTwo:
with open(
"MeanVarianceWithCost.csv", "w", encoding="UTF-8"
) as FrontierHandleTwo:
FrontierHandleTwo.write(MeanVarianceWithCost)

# ***** Portfolio Revision *****
Expand All @@ -292,7 +307,9 @@ def main():

BuyLimits = Parameter(m, name="BuyLimits", domain=[Bound, i])
SellLimits = Parameter(m, name="SellLimits", domain=[Bound, i])
InitHold = Parameter(m, name="InitHold", domain=i, description="Current holdings")
InitHold = Parameter(
m, name="InitHold", domain=i, description="Current holdings"
)

# We set the curret holding to the optimal unconstrained mean-variance portfolio
# with lamda = 0.5
Expand Down Expand Up @@ -393,7 +410,9 @@ def main():
objective=ObjDef,
)

MeanVarianceRevision = '"Model status","Lambda","z","Variance","ExpReturn",'
MeanVarianceRevision = (
'"Model status","Lambda","z","Variance","ExpReturn",'
)

MeanVarianceRevision += ",".join(i_recs) + ","
MeanVarianceRevision += ",".join(i_recs) + ","
Expand All @@ -404,11 +423,17 @@ def main():
if lamda_loop > 1:
break
lamda[...] = lamda_loop
MeanVarRevision.solve(options=Options(minlp="SBB", relative_optimality_gap=0))
MeanVarRevision.solve(
options=Options(minlp="SHOT", relative_optimality_gap=0)
)
MeanVarianceRevision += f"{MeanVarRevision.status},{round(lamda_loop,1)},{round(MeanVarRevision.objective_value,4)},{round(PortVariance.records.level[0],4)},{round(PortReturn.records.level[0],4)},"
x_recs = [str(round(x_rec, 4)) for x_rec in x.records.level.tolist()]
buy_recs = [str(round(x_rec, 4)) for x_rec in buy.records.level.tolist()]
sell_recs = [str(round(x_rec, 4)) for x_rec in sell.records.level.tolist()]
buy_recs = [
str(round(x_rec, 4)) for x_rec in buy.records.level.tolist()
]
sell_recs = [
str(round(x_rec, 4)) for x_rec in sell.records.level.tolist()
]
MeanVarianceRevision += ",".join(x_recs) + ","
MeanVarianceRevision += ",".join(buy_recs) + ","
MeanVarianceRevision += ",".join(sell_recs) + "\n"
Expand All @@ -421,4 +446,4 @@ def main():


if __name__ == "__main__":
main()
main()
22 changes: 15 additions & 7 deletions models/carseq/carseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

from __future__ import annotations

import os

import gamspy.math as gams_math
import numpy as np
import pandas as pd
Expand All @@ -52,7 +54,7 @@
from gamspy.math import ifthen


def main(mip=False):
def main(mip=True):
classData_recs = np.array(
[
[1, 1, 0, 1, 1, 0],
Expand All @@ -73,7 +75,9 @@ def main(mip=False):
id_vars="index", var_name="Category", value_name="Value"
)

m = Container()
m = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# Sets
p = Set(
Expand Down Expand Up @@ -138,9 +142,9 @@ def main(mip=False):
description="positions in the blocks",
)

blkc[o, p, pp].where[Ord(p) <= Card(p) - bs[o] + 1] = (Ord(pp) >= Ord(p)) & (
Ord(pp) < Ord(p) + bs[o]
)
blkc[o, p, pp].where[Ord(p) <= Card(p) - bs[o] + 1] = (
Ord(pp) >= Ord(p)
) & (Ord(pp) < Ord(p) + bs[o])
blk[o, p] = Sum(pp.where[blkc[o, p, pp]], 1)

# Variables
Expand Down Expand Up @@ -255,8 +259,12 @@ def main(mip=False):
rep = Parameter(m, name="rep", domain=[p, c, o])
rep[p, c, o].where[(cp.l[c, p] > 0.5)] = classData[c, o]

print("Objective Function Value: ", carseqLS.objective_value)
print("Objective Function Value: ", carseqMIP.objective_value)

import math

assert math.isclose(carseqMIP.objective_value, -9)


if __name__ == "__main__":
main()
main()
25 changes: 20 additions & 5 deletions models/circuit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

from __future__ import annotations

import os

import gamspy.math as gams_math
import numpy as np
from gamspy import Container, Equation, Model, Parameter, Set, Variable


def main():
cont = Container()
cont = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# SETS #
n = Set(cont, name="n", records=[f"c{c}" for c in range(1, 5)])
Expand Down Expand Up @@ -70,7 +74,12 @@ def main():
* x3
* (
gams_math.exp(
x5 * (g["r1", n] - g["r3", n] * x7 / 1000 - g["r5", n] * x8 / 1000)
x5
* (
g["r1", n]
- g["r3", n] * x7 / 1000
- g["r5", n] * x8 / 1000
)
)
- 1
)
Expand All @@ -84,7 +93,12 @@ def main():
* x3
* (
gams_math.exp(
x5 * (g["r1", n] - g["r3", n] * x7 / 1000 - g["r5", n] * x8 / 1000)
x5
* (
g["r1", n]
- g["r3", n] * x7 / 1000
- g["r5", n] * x8 / 1000
)
)
- 1
)
Expand Down Expand Up @@ -182,8 +196,9 @@ def main():

import math

assert math.isclose(circuit.objective_value, 4.4498522089320064e-09, rel_tol=0.001)
print(circuit.objective_value)
assert math.isclose(circuit.objective_value, 0.1349902012126008)


if __name__ == "__main__":
main()
main()
18 changes: 11 additions & 7 deletions models/control3/control3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from __future__ import annotations

import os

import gamspy.math as gams_math
from gamspy import (
Card,
Expand All @@ -33,7 +35,9 @@


def main():
m = Container()
m = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# SETS #
n = Set(m, name="n", records=["state1"], description="states")
Expand Down Expand Up @@ -68,9 +72,9 @@ def main():
domain=[n, k],
description="state equation",
)
stateq[n, k.lead(1)] = x[n, k.lead(1)] == 2 * x[n, k] + 2 * u[k] * gams_math.sqrt(
x[n, k]
)
stateq[n, k.lead(1)] = x[n, k.lead(1)] == 2 * x[n, k] + 2 * u[
k
] * gams_math.sqrt(x[n, k])

# OBJECTIVE #
j = 0.5 * Sum([k, n], (x[n, k]) + 0.5 * Sum(ku, (u[ku]) * rk * (u[ku])))
Expand All @@ -84,12 +88,12 @@ def main():
objective=j,
)

control3.solve()
control3.solve(solver="conopt")

import math

assert math.isclose(control3.objective_value, 0, rel_tol=0.001)
assert math.isclose(control3.objective_value, 0)


if __name__ == "__main__":
main()
main()
14 changes: 10 additions & 4 deletions models/diffusion2/diffusion2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from __future__ import annotations

import os

import numpy as np
from gamspy import (
Card,
Expand Down Expand Up @@ -483,7 +485,9 @@ def data_records():


def main():
m = Container()
m = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# SETS #
time = Set(m, name="time", records=[f"t{t}" for t in range(1, 26)])
Expand Down Expand Up @@ -527,7 +531,9 @@ def main():

# VARIABLES #
# Model description
t = Variable(m, name="t", domain=[x, y], description="field of temperature")
t = Variable(
m, name="t", domain=[x, y], description="field of temperature"
)
Q = Variable(m, name="Q", description="temperature on boundaries")

# Variable bounds
Expand Down Expand Up @@ -644,7 +650,7 @@ def main():
)

for tu in time.toList():
Diffusion2.solve()
Diffusion2.solve(solver="CONOPT")
print(f"\t --- \t Time interval = {tu} \t --- \n")
print(t.pivot().round(4))
print("\n")
Expand All @@ -655,4 +661,4 @@ def main():


if __name__ == "__main__":
main()
main()
13 changes: 8 additions & 5 deletions models/fiat/fiat.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@

from __future__ import annotations

import os
import sys

import gamspy.math as gams_math
from gamspy import Container, Equation, Model, Variable


def main():
m = Container()
m = Container(
system_directory=os.getenv("GAMSPY_GAMS_SYSDIR", None),
)

# VARIABLES #
q1 = Variable(m, name="q1")
Expand Down Expand Up @@ -304,13 +309,11 @@ def main():
objective=k,
)

fiat.solve()
import math
fiat.solve(output=sys.stdout)

assert math.isclose(fiat.objective_value, 1.4594, rel_tol=0.001)
print("Objective Function Value: ", fiat.objective_value)
# End Fiat


if __name__ == "__main__":
main()
main()
Loading

0 comments on commit fd0e20d

Please sign in to comment.