Skip to content

Commit

Permalink
refactor: skip tests if solver is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
edkerk committed May 2, 2024
1 parent 1fcb910 commit f9f1a7a
Show file tree
Hide file tree
Showing 12 changed files with 908 additions and 863 deletions.
2 changes: 1 addition & 1 deletion doc/core/parallelPoolRAVEN.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0046 <span class="keyword">else</span>
0047 pool = gcp(<span class="string">'nocreate'</span>);
0048 <span class="keyword">if</span> isempty(pool)
0049 parpool(<span class="string">'IdleTimeout'</span>,120)
0049 parpool(<span class="string">'IdleTimeout'</span>,120);
0050 <span class="keyword">end</span>
0051 <span class="keyword">end</span>
0052 <span class="keyword">end</span>
Expand Down
131 changes: 68 additions & 63 deletions doc/core/randomSampling.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,70 +225,75 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0129 sols = num2cell(sols,1);
0130
0131 <span class="comment">%Main loop</span>
0132 PB = ProgressBar2(nSamples,<span class="string">'Performing random sampling'</span>,<span class="string">'cli'</span>);
0133 parfor i=1:nSamples
0134 badSolutions = 1;
0135 tmpModel = model;
0136 <span class="keyword">while</span> lt(0,badSolutions)
0137 rxns = <a href="#_sub1" class="code" title="subfunction I=randsample(n,k,replacement)">randsample</a>(numel(goodRxns),nObjRxns);
0138 tmpModel.c = zeros(numel(tmpModel.rxns),1);
0139 multipliers = <a href="#_sub1" class="code" title="subfunction I=randsample(n,k,replacement)">randsample</a>([-1 1],nObjRxns,true);
0140 multipliers(tmpModel.rev(goodRxns(rxns))==0) = 1;
0141 tmpModel.c(goodRxns(rxns)) = rand(nObjRxns,1).*multipliers;
0142 <span class="keyword">if</span> true(minFlux)
0143 sol=solveLP(tmpModel,1,[],hsSol);
0144 <span class="keyword">else</span>
0145 sol=solveLP(tmpModel,0,[],hsSol);
0146 <span class="keyword">end</span>
0147 <span class="keyword">if</span> any(sol.x) &amp;&amp; abs(sol.f)&gt;10^-8
0148 sols{i} = sol.x;
0149 badSolutions = 0;
0150 <span class="keyword">else</span>
0151 badSolutions = badSolutions+1;
0152 <span class="comment">%If it only finds bad solutions then throw an error.</span>
0153 <span class="keyword">if</span> badSolutions == 100 &amp;&amp; supressErrors==false
0154 error(<span class="string">'The program is having problems finding non-zero solutions (ignoring reactions that might be involved in loops). Review the constraints on your model. Set supressErrors to true to ignore this error'</span>);
0155 <span class="keyword">end</span>
0156 <span class="keyword">end</span>
0157 <span class="keyword">end</span>
0158 count(PB);
0159 <span class="keyword">end</span>
0160
0161 <span class="comment">%Map to original model</span>
0162 sols = cell2mat(sols);
0163 [~, I]=ismember(model.rxns,originalRxns);
0164 solutions=zeros(numel(originalRxns),nSamples);
0165 solutions(I,:)=sols;
0166 solutions=sparse(solutions);
0167 <span class="comment">% Reset original Parallel setting</span>
0168 ps.Pool.AutoCreate = oldPoolAutoCreateSetting;
0169 <span class="keyword">end</span>
0170
0171 <span class="comment">%To use instead of the normal Matlab randsample function. This is in order</span>
0172 <span class="comment">%to not depend on the Matlab statistical toolbox.</span>
0173 <a name="_sub1" href="#_subfunctions" class="code">function I=randsample(n,k,replacement)</a>
0174 <span class="keyword">if</span> nargin&lt;3
0175 replacement=false;
0176 <span class="keyword">end</span>
0177 <span class="comment">%n can be a integer, which leads to I being sampled from 1:n, or it can be</span>
0178 <span class="comment">%a population to sample from.</span>
0179 <span class="keyword">if</span> numel(n)==1 &amp;&amp; isnumeric(n)
0180 n=1:n;
0132 <span class="keyword">if</span> nSamples &gt; 0
0133
0134 PB = ProgressBar2(nSamples,<span class="string">'Performing random sampling'</span>,<span class="string">'cli'</span>);
0135 parfor i=1:nSamples
0136 badSolutions = 1;
0137 tmpModel = model;
0138 <span class="keyword">while</span> lt(0,badSolutions)
0139 rxns = <a href="#_sub1" class="code" title="subfunction I=randsample(n,k,replacement)">randsample</a>(numel(goodRxns),nObjRxns);
0140 tmpModel.c = zeros(numel(tmpModel.rxns),1);
0141 multipliers = <a href="#_sub1" class="code" title="subfunction I=randsample(n,k,replacement)">randsample</a>([-1 1],nObjRxns,true);
0142 multipliers(tmpModel.rev(goodRxns(rxns))==0) = 1;
0143 tmpModel.c(goodRxns(rxns)) = rand(nObjRxns,1).*multipliers;
0144 <span class="keyword">if</span> true(minFlux)
0145 sol=solveLP(tmpModel,1,[],hsSol);
0146 <span class="keyword">else</span>
0147 sol=solveLP(tmpModel,0,[],hsSol);
0148 <span class="keyword">end</span>
0149 <span class="keyword">if</span> any(sol.x) &amp;&amp; abs(sol.f)&gt;10^-8
0150 sols{i} = sol.x;
0151 badSolutions = 0;
0152 <span class="keyword">else</span>
0153 badSolutions = badSolutions+1;
0154 <span class="comment">%If it only finds bad solutions then throw an error.</span>
0155 <span class="keyword">if</span> badSolutions == 100 &amp;&amp; supressErrors==false
0156 error(<span class="string">'The program is having problems finding non-zero solutions (ignoring reactions that might be involved in loops). Review the constraints on your model. Set supressErrors to true to ignore this error'</span>);
0157 <span class="keyword">end</span>
0158 <span class="keyword">end</span>
0159 <span class="keyword">end</span>
0160 count(PB);
0161 <span class="keyword">end</span>
0162
0163 <span class="comment">%Map to original model</span>
0164 sols = cell2mat(sols);
0165 [~, I]=ismember(model.rxns,originalRxns);
0166 solutions=zeros(numel(originalRxns),nSamples);
0167 solutions(I,:)=sols;
0168 solutions=sparse(solutions);
0169 <span class="keyword">else</span>
0170 solutions=[];
0171 <span class="keyword">end</span>
0172 <span class="comment">% Reset original Parallel setting</span>
0173 ps.Pool.AutoCreate = oldPoolAutoCreateSetting;
0174 <span class="keyword">end</span>
0175
0176 <span class="comment">%To use instead of the normal Matlab randsample function. This is in order</span>
0177 <span class="comment">%to not depend on the Matlab statistical toolbox.</span>
0178 <a name="_sub1" href="#_subfunctions" class="code">function I=randsample(n,k,replacement)</a>
0179 <span class="keyword">if</span> nargin&lt;3
0180 replacement=false;
0181 <span class="keyword">end</span>
0182 <span class="comment">%Loop and get random numbers until the list is unique. This is only a good</span>
0183 <span class="comment">%option is the number of samples is small compared to the population. There</span>
0184 <span class="comment">%are several checks that should be made here, for example regarding size</span>
0185 <span class="comment">%and that the number of samples is &lt;=population size if replacement==false.</span>
0186 <span class="comment">%This is not the case in randomSampling, so such checks are ignored</span>
0187 <span class="keyword">while</span> true
0188 J=randi(numel(n),[k,1]);
0189 <span class="keyword">if</span> replacement==true || numel(J)==numel(unique(J))
0190 I=n(J);
0191 <span class="keyword">break</span>;
0192 <span class="keyword">end</span>
0193 <span class="keyword">end</span>
0194 I=I(:);
0195 <span class="keyword">end</span></pre></div>
0182 <span class="comment">%n can be a integer, which leads to I being sampled from 1:n, or it can be</span>
0183 <span class="comment">%a population to sample from.</span>
0184 <span class="keyword">if</span> numel(n)==1 &amp;&amp; isnumeric(n)
0185 n=1:n;
0186 <span class="keyword">end</span>
0187 <span class="comment">%Loop and get random numbers until the list is unique. This is only a good</span>
0188 <span class="comment">%option is the number of samples is small compared to the population. There</span>
0189 <span class="comment">%are several checks that should be made here, for example regarding size</span>
0190 <span class="comment">%and that the number of samples is &lt;=population size if replacement==false.</span>
0191 <span class="comment">%This is not the case in randomSampling, so such checks are ignored</span>
0192 <span class="keyword">while</span> true
0193 J=randi(numel(n),[k,1]);
0194 <span class="keyword">if</span> replacement==true || numel(J)==numel(unique(J))
0195 I=n(J);
0196 <span class="keyword">break</span>;
0197 <span class="keyword">end</span>
0198 <span class="keyword">end</span>
0199 I=I(:);
0200 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>
6 changes: 3 additions & 3 deletions doc/installation/checkInstallation.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,16 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0230 <span class="comment">% Do not change old solver if functional</span>
0231 <span class="keyword">if</span> solverIdx~=0 &amp;&amp; res(solverIdx).Passed == 1
0232 fprintf([oldSolver <span class="string">'\n'</span>])
0233 <span class="comment">% Order of preference: gurobi &gt; glpk &gt; soplex &gt; cobra</span>
0233 <span class="comment">% Order of preference: gurobi &gt; glpk &gt; scip &gt; cobra</span>
0234 <span class="keyword">elseif</span> res(2).Passed == 1
0235 fprintf(<span class="string">'gurobi\n'</span>)
0236 setRavenSolver(<span class="string">'gurobi'</span>);
0237 <span class="keyword">elseif</span> res(1).Passed == 1
0238 fprintf(<span class="string">'glpk\n'</span>)
0239 setRavenSolver(<span class="string">'glpk'</span>);
0240 <span class="keyword">elseif</span> res(3).Passed == 1
0241 fprintf(<span class="string">'soplex\n'</span>)
0242 setRavenSolver(<span class="string">'soplex'</span>);
0241 fprintf(<span class="string">'scip\n'</span>)
0242 setRavenSolver(<span class="string">'scip'</span>);
0243 <span class="keyword">elseif</span> res(4).Passed == 1
0244 fprintf(<span class="string">'cobra\n'</span>)
0245 setRavenSolver(<span class="string">'cobra'</span>);
Expand Down
2 changes: 1 addition & 1 deletion doc/solver/optimizeProb.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0178 res.obj = fmin;
0179 res.dual = -extra.lambda*prob.osense;
0180 res.rcost = -extra.redcosts*prob.osense;
0181 <span class="comment">%% Use SoPlex</span>
0181 <span class="comment">%% Use scip</span>
0182 <span class="keyword">case</span> {<span class="string">'soplex'</span>,<span class="string">'scip'</span>} <span class="comment">% Old 'soplex' option also allowed</span>
0183 [xopt,fval,exitflag] = scip([], prob.c, prob.A,-prob.b, prob.b, prob.lb, prob.ub, prob.vartype);
0184
Expand Down
Loading

0 comments on commit f9f1a7a

Please sign in to comment.