warehouse.cpp 파일 참조
이 예는 다양한 데이터 세트의 창고에 매장을 병렬로 할당하기 위해 간단한 슬롯 게임 모델을 해결하는 방법을 보여줍니다.더 보기...
#include <iostream>#include <fstream>#include <뮤텍스>#include <스레드>#include "슬롯 게임h"기능 | |
| 문자열 | getModelText () |
| 모델을 문자열로 가져옵니다. | |
| 무효 | solveWarehouse (슬롯 게임작업공간*ws, int numberOfWarehouses,슬롯 게임데이터베이스*resultDB, 뮤텍스 *dbMutex) |
| int | 메인(int argc, char *argv[]) |
상세 설명
이 예는 다양한 데이터 세트에 대해 매장을 창고에 병렬로 할당하기 위해 간단한 슬롯 게임 모델을 해결하는 방법을 보여줍니다.
모델이 매개변수화되었습니다. 데이터는 창고 수, 매장 수, 일부 고정 비용 스칼라 등 몇 가지 숫자에서 파생될 수 있습니다. 모델의 결과는 뮤텍스를 통해 병렬 스레드 전체에서 보호되는 단일 결과 데이터베이스에 기록됩니다.
파일의 정의warehouse.cpp.
함수 문서
◆ getModelText()
| string getModelText | ( | ) |
모델을 문자열로 가져옵니다.
라인 정의38파일warehouse.cpp.
39{
40 반환 "$title Warehouse.gms \n"
41 " \n"
42 "$eolcom // \n"
43 "$SetDDList 창고 저장소 고정 분리 // 허용 가능한 정의 \n"
44 "$창고가 설정되지 않은 경우 $창고 10으로 설정 \n"
45 "$설정되지 않은 상점 $set 상점 50 \n"
46 "$설정되지 않은 경우 고정됨 $설정된 20 \n"
47 "$if set not set disaggregate $set disaggregate 1 // 더 엄격한 bigM 제약 조건에 대한 표시 \n"
48 "$ife %store%<=%warehouse% $abort 매장 수 증가 (>%warehouse) \n"
49 " \n"
50 "응답 응답 코드 설정 / 0 일반 \n"
51 " 1 라이센스 오류 \n"
52 " 2 해결책 없음 \n"
53 " 3 기타 오류 / \n"
54 " 아르(res) / 3 /; \n"
55 " \n"
56 "창고 설정 /w1*w%warehouse% / \n"
57 " 저장 /s1*s%store% / \n"
58 "별칭 (창고,w), (상점,s); \n"
59 "스칼라 \n"
60 " 창고 개설에 대한 고정 고정 비용 / %fixed% / \n"
61 "매개변수\n"
62 " 용량(WareHouse) \n"
63 " 공급 비용(상점,창고); \n"
64 " \n"
65 "$eval storeDIVwarehouse trunc(카드(스토어)/카드(창고)) \n"
66 "용량(w) = %storeDIVwarehouse% + mod(ord(w),%storeDIVwarehouse%); \n"
67 "공급 비용(s,w) = 1+mod(ord(s)+10*ord(w), 100); \n"
68 " \n"
69 "변수 \n"
70 " 열기(창고) \n"
71 " 공급(상점,창고) \n"
72 " obj; \n"
73 "이진 변수 열기, 공급; \n"
74 " \n"
75 "방정식 \n"
76 " defobj \n"
77 " 하나의 창고 \n"
78 " defopen(w); \n"
79 " \n"
80 "defobj.. obj =e= 합계(w, 고정*개방(w)) + 합계((w,s), 공급 비용(s,w)*공급(s,w)); \n"
81 " \n"
82 "하나의 창고.. 합계(w, 공급(들,w)) =e= 1; \n"
83 " \n"
84 "defopen(w).. sum(s, 공급(s,w)) =l= open(w)*capacity(w); \n"
85 " \n"
86 "$ifthen %disaggregate%==1 \n"
87 "방정식 \n"
88 " defopen2(s,w); \n"
89 "defopen2(s,w).. 공급(s,w) =l= 개방(w); \n"
90 "$endif \n"
91 " \n"
92 "모델 분포 /all/; \n"
93 "Mip를 사용하여 최소 obj 분포를 해결합니다. \n"
94 " \n"
95 "$macro setResult(n) 옵션clear=ares; ares(n) = yes; \n"
96 "if (distrib.modelstat=%ModelStat.LicensingProblem% 또는 \n"
97 " distrib.solvestat=%Solvestat.LicensingProblems%, \n"
98 " setResult('1'); \n"
99 " '라이센스 오류'를 중단합니다. \n"
100 "); \n"
101 "if (distrib.solvestat<>%SolveStat.NormalCompletion% 또는 \n"
102 " distrib.modelstat<>%ModelStat.Optimal% 및 \n"
103 " distrib.modelstat<>%ModelStat.IntegerSolution%, \n"
104 " setResult('2'); \n"
105 " '해결 방법 없음'을 중단합니다. \n"
106 "); \n"
107 "setResult('0'); \n";
108}
참조자solveWarehouse().
◆ 메인()
| int 메인 | ( | int | argc, |
| 문자 * | argv[] ) |
라인 정의175파일warehouse.cpp.
176{
177cout <<"------------ 창고 ------------"<< endl;
178
179 시도해 보세요{
180 GAMSWorkspaceInfowsInfo;
181 if(인수 > 1)
182wsInfo.setSystemDirectory(argv[1]);
183 슬롯 게임작업공간ws(wsInfo);
184
185 // 결과에 대한 GAMSDatabase 생성
186 슬롯 게임데이터베이스resultDB = ws.addDatabase();
187
190 // 여러 병렬 작업 실행
191뮤텍스 dbMutex;
192벡터<스레드> v;
193 for(intnr창고=10; nr창고<22; nr창고++)
194v.emplace_back([&ws, nrWarehouses, &resultDB, &dbMutex]solveWarehouse(&ws, nrWarehouses, &resultDB, &dbMutex););
195 for (자동& t : v)
196t.join();
197 if(상태 > 0)
198 던지기 GAMSExceptionExecution("슬롯 게임 실행 중 오류: " + GAMSEnum::text((GAMSEnum::GAMSExitCode) 상태) +" "+ statusString, 상태, &ws);
199 그밖에 if(상태 == -1)
201 그밖에 if(상태 == -2)
202 던지기예외();
203 // 결과 데이터베이스를 GDX 파일로 내보내기
206cout <<"슬롯 게임예외 발생: "<< ex.what() << endl;
207}잡기(예외 &ex)
208cout << ex.what() << endl;
209}
210 반환상태;
211}
GAMSSet addSet(const std::string &name, const int 차원, const std::string &explanatoryText="", GAMSEnum::SetType setType=GAMSEnum::SetType::Multi)
GAMSParameter addParameter(const std::string &name, const int 차원, const std::string &explanatoryText="")
void doExport(const std::string &filePath="")
정적 std::string 텍스트(GAMSEnum::SymbolType 유형)
GAMSExitCode
void setSystemDirectory(const std::string &systemDir)
◆ solveWarehouse()
| voidsolvWarehouse | ( | 슬롯 게임작업공간 * | ws, |
| int | numberOfWarehouses, | ||
| 슬롯 게임데이터베이스 * | 결과DB, | ||
| 뮤텍스 * | dbMutex ) |
라인 정의110파일warehouse.cpp.
111{
113 시도해 보세요{
114 // GAMSOptions를 인스턴스화하고 일부 스칼라를 정의
121gmsOpt.setOptCR(0.0);
122
123 // 문자열에서 GAMSJob을 생성하고 결과 데이터베이스에 결과를 씁니다.
124gmsJ.실행(gmsOpt);
125
126 // 데이터베이스 쓰기 작업을 잠가야 함
127{
128lock_guard<mutex> dbLock(*dbMutex);
129결과DB->getParameter("objrep").추가기록(to_string(numberOfWarehouses)).세트값(gmsJ.outDB().get변수("obj").findRecord().레벨());
130}
131
133{
134 if(supplyRec.level() > 0.5)
135{
136lock_guard<mutex> dbLock(*dbMutex);
138}
139}
140}
142{
144{
145lock_guard<mutex> dbLock(*dbMutex);
146statusString = gmsJ.outDB().getSet("res").findRecord(gmsJ.outDB().getSet("아레스").첫 번째 레코드().키(0)).텍스트();
147}
148{
149lock_guard<mutex> dbLock(*dbMutex);
150상태 = e.rc();
151}
152}
154{
155cout << e.what() << endl;
156lock_guard<mutex> dbLock(*dbMutex);
157상태 = -1;
158}
159 잡기(예외&e)
160{
161cout << e.what() << endl;
162lock_guard<mutex> dbLock(*dbMutex);
163상태 = -2;
164}
165}
GAMSSet getSet(const std::string &name)
GAMSParameter getParameter(const std::string &name)
GAMSVariable getVariable(const std::string &name)
슬롯 게임데이터베이스 outDB()
void run()
void setAllModelTypes(const std::string &solver)
void setOptCR(const double value)
void setDefine(const std::string &key, const std::string &value)
void setValue(const double val)
GAMSParameterRecord addRecord(const std::벡터< std::string > &keys)
std::string 텍스트()
GAMSSetRecord findRecord(const std::벡터< std::string > &keys)
GAMSSetRecord firstRecord(const std::벡터< std::string > &slice)
GAMSSetRecord addRecord(const std::벡터< std::string > &keys)
std::string 키(int 인덱스)
GAMSVariableRecord findRecord(const std::벡터< std::string > &keys)
GAMSOptions addOptions()
GAMSJob addJobFromString(const std::string &gamsSource, const std::string &jobName="")