CONOPT
Loading...
Searching...
No Matches
std.py
Go to the documentation of this file.
1# std.py
2#
3# Some standard methods for running the tutorials
4
5import sys
6
7import pyconopt as copt
8
9class TutMessageHandler(copt.MessageHandler):
10 def __init__(self, name):
11 self.name = name
12 self.fs = open(f'{name}.sta', 'w')
13 self.fd = open(f'{name}.lst', 'w')
14 super().__init__()
15
16 def message(self, smsg, dmsg, nmsg, msgv):
17 if smsg > 0:
18 print('\n'.join(msgv[:smsg]))
19 self.fs.write('{}\n'.format('\n'.join(msgv[:smsg])))
20
21 if dmsg > 0:
22 self.fd.write('{}\n'.format('\n'.join(msgv[:dmsg])))
23
24 return 0
25
26 def errorMessage(self, rowno, colno, posno, msg):
27 errormsg = ""
28 if rowno == -1:
29 errormsg += f'Variable {colno} : '
30 elif colno == -1:
31 errormsg += f'Equation {rowno} : '
32 else:
33 errormsg += f'Variable {colno} appearing in Equation {rowno} : '
34
35 errormsg += f'{msg}\n'
36
37 self.fd.write(errormsg)
38 self.fs.write(errormsg)
39
40 return 0
41
42def _log(conopt, msg):
43 name = conopt.getName()
44 with open(f'{name}.rc', 'w') as fc:
45 fc.write(f'{name}: {msg}\n')
46
47
48def checkSolve(conopt, expected_objective, coi_error, tol = 0.000001):
49 # checking the statuses and objective value
50 if conopt.modelStatus() != 2 or conopt.solutionStatus() != 1:
51 _log(conopt, "Incorrect Model or Solver Status")
52 return -1
53 elif abs( conopt.objectiveValue() - expected_objective ) > tol:
54 _log(conopt, "Incorrect objective returned")
55 return -1
56
57 # printing the final status of the optimisation
58 conopt.printStatus();
59
60 _log(conopt, "Successful Solve");
61
62 return coi_error
__init__(self, name)
Definition std.py:10
errorMessage(self, rowno, colno, posno, msg)
Definition std.py:26
message(self, smsg, dmsg, nmsg, msgv)
Definition std.py:16
checkSolve(conopt, expected_objective, coi_error, tol=0.000001)
Definition std.py:48
_log(conopt, msg)
Definition std.py:42