Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smetana addConstr() error while using gurobi solver #51

Open
BananaJumper96 opened this issue Nov 22, 2024 · 1 comment
Open

Smetana addConstr() error while using gurobi solver #51

BananaJumper96 opened this issue Nov 22, 2024 · 1 comment

Comments

@BananaJumper96
Copy link

This is my smetana command:

rule smetana:
    input:
        gatherbinmodels
    output:
        detailed_tsv = os.path.join(outpath, "SMETANA", "{binID}_detailed.tsv")
    params:
        smetana_media = "M8",  # Replace with your media
        smetana_solver = "gurobi",    # Replace with your solver
        smetana_flavor = "fbc2",
        media_db = os.path.join(databasesDir, "media_db.tsv"),
        binID = lambda wildcards: wildcards.binID  # Extract bin ID from the input filename
    conda:
        os.path.join(envDir, "env_gurobi.yaml")
    log:
        os.path.join(outpath, "logs", "{binID}_smetana.log")
    shell:
        """
        tmpdir=$(mktemp -d)
        trap "rm -rf $tmpdir" EXIT

        mkdir -p "$(dirname "{output.detailed_tsv}")"

        echo -e "\\nRunning SMETANA for sample {wildcards.binID} ... " >> "{log}"

        cp "{params.media_db}" "$tmpdir/" >> "{log}" 2>&1
        cp {input:q} "$tmpdir/" >> "{log}" 2>&1

        cd "$tmpdir"
        smetana -o "{wildcards.binID}" \
            --flavor "{params.smetana_flavor}" \
            --mediadb "$(basename "{params.media_db}")" \
            -m "{params.smetana_media}" \
            --detailed \
            --solver "{params.smetana_solver}" \
            -v *.xml >> "{log}" 2>&1

        cp "{wildcards.binID}_detailed.tsv" "{output.detailed_tsv}" >> "{log}" 2>&1

        echo "SMETANA analysis for sample {wildcards.binID} completed." >> "{log}"
        """
        
    It is running a snakemake file. However when using the gurobi solver, I get the following error:
Running SMETANA for sample bin.122 ... 
Loading community: all
Running SCS for community all on medium M8...
Restricted license - for non-production use only - expires 2026-11-23
Traceback (most recent call last):
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/bin/smetana", line 89, in <module>
    main(
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/smetana/interface.py", line 345, in main
    entries = run_detailed(comm_id, community, medium_id, excluded_mets, env, verbose, min_mol_weight,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/smetana/interface.py", line 174, in run_detailed
    scs = sc_score(community, environment=env, verbose=verbose)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/smetana/smetana.py", line 38, in sc_score
    solver = solver_instance(community.merged)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/reframed/solvers/__init__.py", line 104, in solver_instance
    return solvers[solver](model)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/reframed/solvers/gurobi_solver.py", line 57, in __init__
    self.build_problem(model)
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/reframed/solvers/solver.py", line 99, in build_problem
    self.update()
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/reframed/solvers/solver.py", line 80, in update
    self.add_constraints(self._cached_constrs)
  File "/mnt/lscratch/users/rparise/Thesis/Envs/ece8ca1e4744f5192f12549dd720d25d_/lib/python3.11/site-packages/reframed/solvers/gurobi_solver.py", line 72, in add_constraints
    self.problem.addConstr(expr, sense_mapping[sense], rhs, constr_id)
  File "src/gurobipy/_model.pyx", line 3894, in gurobipy._model.Model.addConstr
TypeError: addConstr() takes at most 3 positional arguments (5 given)

Could you help me find out what is causing this error and how I can fix that addConstr() error? Thank you

@dingxia97
Copy link

dingxia97 commented Dec 5, 2024

I have the same problem.

This is my smetana command:
smetana --flavor fbc2 --mediadb media_db.tsv -m M1,M2,M3,M4,M5,M7,M8,M9,M10,M11,M13,M14,M15A,M15B,M16 --detailed --solver gurobi -v *.xml

Loading community: all
Running SCS for community all on medium M1...
Set parameter LicenseID to value 2590555
Traceback (most recent call last):
  File "/Users/dingxia/mambaforge/envs/metagem/bin/smetana", line 89, in <module>
    main(
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/smetana/interface.py", line 345, in main
    entries = run_detailed(comm_id, community, medium_id, excluded_mets, env, verbose, min_mol_weight,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/smetana/interface.py", line 174, in run_detailed
    scs = sc_score(community, environment=env, verbose=verbose)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/smetana/smetana.py", line 38, in sc_score
    solver = solver_instance(community.merged)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/reframed/solvers/__init__.py", line 104, in solver_instance
    return solvers[solver](model)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/reframed/solvers/gurobi_solver.py", line 57, in __init__
    self.build_problem(model)
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/reframed/solvers/solver.py", line 99, in build_problem
    self.update()
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/reframed/solvers/solver.py", line 80, in update
    self.add_constraints(self._cached_constrs)
  File "/Users/dingxia/mambaforge/envs/metagem/lib/python3.12/site-packages/reframed/solvers/gurobi_solver.py", line 72, in add_constraints
    self.problem.addConstr(expr, sense_mapping[sense], rhs, constr_id)
  File "src/gurobipy/_model.pyx", line 3894, in gurobipy._model.Model.addConstr
TypeError: addConstr() takes at most 3 positional arguments (5 given)

When I run the example in the gurobi installation directory examples/python/mip1.py successfully, it's work:


base$ python /Library/gurobi1200/macos_universal2/examples/python/mip1.py
Set parameter LicenseID to value 2590555
Gurobi Optimizer version 12.0.0 build v12.0.0rc1 (mac64[arm] - Darwin 22.6.0 22G74)

CPU model: Apple M1
Thread count: 8 physical cores, 8 logical processors, using up to 8 threads

Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
  Matrix range     [1e+00, 3e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 8 available processors)

Solution count 2: 3 2 

Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1
y 0
z 1
Obj: 3

my python version is 3.12, gurobi v12.0.0, smetana v1.2.0.
I tried different versions of gurobi, but the result is always the same.
Could you help me find out what is causing this error and how I can fix that addConstr() error? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants