로드 중...
검색 중...
일치하는 항목 없음
core_example2.py
1가져오기argparse
2가져오기os
3가져오기하위 프로세스
4가져오기sys
5fromgams.core가져오기크레이지 슬롯, 선택
6
7
8def종료(gdx_h, opt_h, rc):
9opt.optFree(opt_h)
10크레이지 슬롯gdxFree(gdx_h)
11sys.exit(rc)
12
13
14defreport_gdx_error(gdx_h, s):
15error_msg = 크레이지 슬롯gdxErrorStr(gdx_h, 크레이지 슬롯gdxGetLastError(gdx_h))[1]
16인쇄(f"오류 s: error_msg")
17
18
19defwrite_model_data(gdx_h, gdx_file):
20rc, error_nr = 크레이지 슬롯gdxOpenWrite(gdx_h, gdx_file,"xp_example1")
21 if 아님rc:
22인쇄(f"오류 gdxOpenWrite: 크레이지 슬롯gdxErrorStr(gdx_h, error_nr)[1]")
23 반환 거짓
24
25 if 아님크레이지 슬롯gdxDataWriteStrStart(
26gdx_h,"수요", "수요 데이터", 1, 크레이지 슬롯GMS_DT_PAR, 0
27):
28report_gdx_error(gdx_h,"gdxDataWriteStrStart")
29 반환 거짓
30
31값 = 크레이지 슬롯doubleArray(크레이지 슬롯GMS_VAL_MAX)
32값[크레이지 슬롯GMS_VAL_LEVEL] = 324.0
33크레이지 슬롯gdxDataWriteStr(gdx_h, ["뉴욕"], 값)
34값[크레이지 슬롯GMS_VAL_LEVEL] = 299.0
35크레이지 슬롯gdxDataWriteStr(gdx_h, ["시카고"], 값)
36값[크레이지 슬롯GMS_VAL_LEVEL] = 274.0
37크레이지 슬롯gdxDataWriteStr(gdx_h, ["토피카"], 값)
38
39 if 아님크레이지 슬롯gdxDataWriteDone(gdx_h):
40report_gdx_error(gdx_h,"gdxDataWriteDone")
41
42error_nr = 크레이지 슬롯gdxGetLastError(gdx_h)
43 iferror_nr:
44인쇄(f"크레이지 슬롯 파일을 쓰는 중 오류 발생: 크레이지 슬롯gdxErrorStr(gdx_h, error_nr)[1]")
45 반환 거짓
46
47error_nr = 크레이지 슬롯gdxClose(gdx_h)
48 iferror_nr:
49인쇄(f"오류 gdxClose: 크레이지 슬롯gdxErrorStr(gdx_h, error_nr)[1]")
50 반환 거짓
51
52 반환
53
54
55defcall_gams(opt_h, sys_dir, 모델):
56def_file = os.path.join(sys_dir,"optgams.def")
57
58 ifopt.optReadDefinition(opt_h, def_file):
59인쇄(f"optReadDefinition 오류, def 파일을 읽을 수 없습니다: def_file")
60 반환 거짓
61
62opt.optSetStrStr(opt_h,"입력", 모델)
63opt.optSetIntStr(opt_h,"로그옵션", 3)
64opt.optWriteParameterFile(opt_h,"gams.opt")
65
66 반환 (
67하위 프로세스.run(
68[os.path.join(sys_dir,"감"), "더미", "pf=gams.opt"], 확인=사실입니다
69).반환코드
70== 0
71 )
72
73
74defread_solution_data(gdx_h, gdx_file):
75rc = 크레이지 슬롯gdxOpenRead(gdx_h, gdx_file)[1]
76 ifrc:
77인쇄(f"오류 gdxOpenRead: 크레이지 슬롯gdxErrorStr(gdx_h, rc)[1]")
78 반환 거짓
79
80rc, Sym_nr = 크레이지 슬롯gdxFindSymbol(gdx_h,"결과")
81 if 아님rc:
82인쇄("오류 gdxFindSymbol: 변수 결과를 찾을 수 없습니다")
83 반환 거짓
84
85rc, 기호 이름, 희미, 기호 유형 = 크레이지 슬롯gdxSymbolInfo(gdx_h, 기호_nr)
86 if어두움 != 2또는sym_type != 크레이지 슬롯GMS_DT_VAR:
87인쇄("오류 gdxSymbolInfo: 결과는 2차원 변수가 아닙니다")
88 반환 거짓
89
90rc, nr_recs = 크레이지 슬롯gdxDataReadStrStart(gdx_h, Sym_nr)
91 if 아님rc:
92report_gdx_error(gdx_h,"gdxDataReadStrStart")
93 반환 거짓
94
95 foriin범위(nr_recs):
96rc, 요소, 값, afdim = 크레이지 슬롯gdxDataReadStr(gdx_h)
97 if 아님rc:
98인쇄(f"오류 gdxDataReadStr: 크레이지 슬롯gdxErrorStr(gdx_h, rc)[1]")
99 반환 거짓
100수준 = 값[크레이지 슬롯GMS_VAL_LEVEL]
101 if레벨 != 0:
102요소 = [요소[d]ford범위(어두움)]
103인쇄(f"'.'.join(elements) = 레벨")
104인쇄("모든 솔루션 값이 표시됨")
105크레이지 슬롯gdxDataReadDone(gdx_h)
106크레이지 슬롯크레이지 슬롯닫기(gdx_h)
107 반환 사실입니다
108
109
110if__name__ =="__main__":
111파서 = argparse.ArgumentParser()
112parser.add_argument("sysDir")
113parser.add_argument("모델", nargs="?", 기본값=없음)
114args = 파서.parse_args()
115
116gdx_h = 크레이지 슬롯new_gdxHandle_tp()
117opt_h = opt.new_optHandle_tp()
118
119sys_dir = args.sysDir
120모델 = (
121args.model
122 ifargs.model
123 그밖에os.path.join(sys_dir,"api파일", "GAMS", "model2.gms")
124 )
125
126인쇄(f"GAMS 시스템 디렉토리 사용: sys_dir")
127
128rc = 크레이지 슬롯gdxCreateD(gdx_h, sys_dir, 크레이지 슬롯GMS_SSSIZE)
129 if 아님rc[0]:
130 인상예외(rc[1])
131
132rc = opt.optCreateD(opt_h, sys_dir, 크레이지 슬롯GMS_SSSIZE)
133 주장rc[0], rc[1]
134
135status = write_model_data(gdx_h,"demanddata.크레이지 슬롯")
136 if 아님상태:
137인쇄("모델 데이터가 기록되지 않았습니다")
138종료(gdx_h, opt_h, 1)
139
140status = call_gams(opt_h, sys_dir, 모델)
141 if 아님상태:
142인쇄("GAMS 호출 실패")
143종료(gdx_h, opt_h, 1)
144
145상태 = read_solution_data(gdx_h,"results.크레이지 슬롯")
146 if 아님상태:
147인쇄("솔루션을 다시 읽을 수 없습니다.")
148종료(gdx_h, opt_h, 1)
149
150종료(gdx_h, opt_h, 0)