|
发表于 2024-7-27 10:29
来自手机
|
显示全部楼层
<?php
function readTxt($fileName='1.txt'){
$strContents=file_get_contents($fileName);
$arrLines=explode("\n",trim($strContents));
return $arrLines;
}
function getOneCol($colVarName,$colcoefficient){
$templateCols=<<<eof
x02 c1 vCol
eof;
$col=str_replace('x02',$colVarName,$templateCols);
$col=str_replace('vCol',$colcoefficient,$col);
return $col;
}
function getAllCols($arrLines){
$allCols='';
foreach ($arrLines as $key=>$line){
$key+=1;
$colVarName='X'.sprintf('%02d',$key);
//$colVarName='x'.$key;
$colcoefficient=$line;
$newLine=getOneCol($colVarName,$colcoefficient);
$allCols.=$newLine."\n";
}
return $allCols;
}
function getAllBounds($arrLines){
$templateBnd=<<<eof
BV bnd x01 1
eof;
$allBounds[]='BOUNDS // 变量上下界';
foreach ($arrLines as $key=>$line){
$key+=1;
$newKey='X'.sprintf('%02d',$key);
//$newKey='x'.$key;
$newLine=str_replace('x01',$newKey,$templateBnd);
$allBounds[]=$newLine;
}
return implode("\n",$allBounds);
}
function createMps($vHRS='12928.1'){
$templateHeaders=<<<eof
NAME example.mps
ROWS
N obj
E c1
COLUMNS\n
eof;
$templateRhs=<<<eof
RHS
rhs c1 vHRS
eof;
$templateDataEnd=<<<eof
ENDATA
eof;
$contents=$templateHeaders;
$arrLines=readTxt();
//echo getAllCols($arrLines);
$contents.=getAllCols($arrLines);
$contents.=str_replace('vHRS',$vHRS,$templateRhs);
$contents.="\n";
$contents.=getAllBounds($arrLines);
$contents.=$templateDataEnd;
//echo $contents;
//file_put_contents('makeupNum.mps',$contents);
file_put_contents('my.mps',$contents);
}
//createMps(3);
$vHRS='467341.62';
$vHRS='77855.06';
$vHRS='259809.89';
//不存在合计
$vHRS='1493045.73';
createMps($vHRS);
$output = shell_exec('bash run.sh');
echo "<pre>$output</pre>";
$sol=file_get_contents('my.sol');
echo $sol;
|
-
|