user25433359
user25433359

Reputation: 1

The SolverFactory was unable to create the solver "_glpk_shell"

I have developed a processing script in qgis which calls and solves an optimization model. The solver is glpk. However, access to the solver does not seem to work, neither via the relative nor via the absolute path. The solver is recognized via my normal Python environment, but not via QGIS.The glpk folder is available to me as an already installed folder with glpsol.exe. I have not installed it myself. However, it works for my colleague.

Simplified example:

# -*- coding: utf-8 -*-

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing, 
                       QgsProcessingException, 
                       QgsProcessingParameterNumber, 
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSink)
from pyomo.environ import ConcreteModel, Var, Objective, Constraint, NonNegativeReals, SolverFactory
import os
import sys
import io

class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
    OUTPUT = 'OUTPUT'
    limit1 = 'limit1'
    limit2 = 'limit2'

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleProcessingAlgorithm()

    def name(self):
        return 'testglpk'

    def displayName(self):
        return self.tr('testglpk')

    def group(self):
        return self.tr('test')

    def groupId(self):
        return 'test'

    def shortHelpString(self):
        return self.tr("Example algorithm short description")

    def initAlgorithm(self, config=None):
        # parameter
        self.addParameter(
            QgsProcessingParameterNumber(
                'limit1',
                self.tr('limit1'),
                type=QgsProcessingParameterNumber.Double,
                minValue=0,
                defaultValue=8
            )
        )
        self.addParameter(
            QgsProcessingParameterNumber(
                'limit2',
                self.tr('limit2'),
                type=QgsProcessingParameterNumber.Double,
                minValue=0,
                defaultValue=8
            )
        )

        ## results
        self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Output layer')))

    def processAlgorithm(self, parameters, context, feedback):
        # parameter
        limit1 = self.parameterAsDouble(parameters, self.limit1, context)
        limit2 = self.parameterAsDouble(parameters, self.limit2, context)

        # Pyomo-model
        model = ConcreteModel()
        model.x = Var(domain=NonNegativeReals)
        model.y = Var(domain=NonNegativeReals)
        model.cost = Objective(expr=3*model.x + 4*model.y, sense=1)
        model.constr1 = Constraint(expr=2*model.x + model.y >= limit1)
        model.constr2 = Constraint(expr=model.x + 2*model.y >= limit2)

        opt = SolverFactory('glpk')

        # Umleitung von stdout und stderr
        old_stdout = sys.stdout
        old_stderr = sys.stderr
        sys.stdout = io.StringIO()
        sys.stderr = io.StringIO()

        try:
            results = opt.solve(model)
        finally:
            output = sys.stdout.getvalue()
            error_output = sys.stderr.getvalue()
            sys.stdout = old_stdout
            sys.stderr = old_stderr

        feedback.pushInfo("Solution:")
        feedback.pushInfo(f"x = {model.x()}")
        feedback.pushInfo(f"y = {model.y()}")
        feedback.pushInfo(f"Kosten = {model.cost()}")

        feedback.pushInfo("Solver-Output:")
        feedback.pushInfo(output)
        if error_output:
            feedback.pushInfo("Solver-Error-Output:")
            feedback.pushInfo(error_output)

        return {}

The error code is as follows: RuntimeError: Attempt to use an unavailable solver. The SolverFactory was not able to create the solver "_glpk_shell" and returned an UnknownSolver object. and returned an UnknownSolver object. This error is triggered at the point where the UnknownSolver object was used as if it were valid (by calling the method "solve"). The original solver was created with the following parameters Executable: C:\Users\xxx\AppData\Roaming\Python\Python39\site-packages\glpk-5.0\Library\bin Type: _glpk_shell args: () options: {}

Execution failed after 28.03 seconds

I have tried the call with absolute path. I have tried to move glpsol to other folders. Both did not help.

Upvotes: 0

Views: 54

Answers (0)

Related Questions