iterator.py
Go to the documentation of this file.
9
10from __future__ import print_function
11from gams import *
12import sys
13from time import time
14
16 return '''
17set i explanatory text for i /i1*i1000/;
18set j explanatory text for j /j1*j1000/;
19parameter p(i,j) explanatory text for p;
20p(i,j) = uniform(0,100);'''
21
22if __name__ == "__main__":
23 if len(sys.argv) > 1:
24 ws = GamsWorkspace(system_directory = sys.argv[1])
25 else:
26 ws = GamsWorkspace()
27
28 job = ws.add_job_from_string(get_data_text())
29 job.run()
30
31 # iterate over all symbols of job.out_db
32 print("---- GamsSymbols in GamsDatabase: ----")
33 for symbol in job.out_db:
34 print(symbol.name + " (" + type(symbol).__name__ + "):")
35 print(" dim = " + str(symbol.dimension))
36 print(" #records = " + str(len(symbol)))
37 print(" text = " + symbol.text)
38
39 print("\n---- iterate over all records of 'p' using a for loop ----")
40 t_start = time()
41 for rec in job.out_db['p']:
42 if rec.value < 0.001:
43 rec_tmp = rec
44 print(str(rec.keys) + " value=" + str(rec.value))
45 print("rec_tmp: " + str(rec_tmp.keys) + " value = " + str(rec_tmp.value))
46 print("duration of for-loop: " + str(time() - t_start))
47
48 print("\n---- iterate over all records of 'p' using record.move_next() ----")
49 t_start = time()
50 rec = job.out_db['p'].first_record()
51 while True:
52 if rec.value < 0.001:
53 rec_tmp = rec
54 print(str(rec.keys) + " value=" + str(rec.value))
55 if not rec.move_next(): break
56 print("rec_tmp: " + str(rec_tmp.keys) + " value = " + str(rec_tmp.value))
57 print("duration of move_next(): " + str(time() - t_start))
def get_data_text()
Definition: iterator.py:15