# C++ & Gurobi Programming

**URL:** https://forum.ysang.org/c/programming/5.md

[Latest](https://forum.ysang.org/latest.md) · [Categories](https://forum.ysang.org/categories.md)

---

## [All You Need to Know about C++ and Gurobi Programming](https://forum.ysang.org/t/all-you-need-to-know-about-c-and-gurobi-programming/12)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0

</div>

In this category, let’s share our programming tips when we implement the models. Start Programming: https://forum.ysang.org/t/setting-up-gurobi-with-c-in-the-linux-environment/13/3 https://forum.ysang.org/t/c-gurobi-p…

---

## [What to do if our optimization problem is infeasible](https://forum.ysang.org/t/what-to-do-if-our-optimization-problem-is-infeasible/19)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [June 9, 2020, 11:19pm UTC](https://forum.ysang.org/t/what-to-do-if-our-optimization-problem-is-infeasible/19 "2020-06-09T23:19:10Z")

</div>

When we run our C++ Gurobi code, sometimes we end up getting a result saying the problem is “infeasible”. How do we figure out makes the code infeasible? First thing first, if you are using a reusable code, your code wa…

---

## [Setting up Gurobi with C++ in the Linux environment](https://forum.ysang.org/t/setting-up-gurobi-with-c-in-the-linux-environment/13)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 2\
**Last updated:** [February 3, 2021, 8:00pm UTC](https://forum.ysang.org/t/setting-up-gurobi-with-c-in-the-linux-environment/13 "2021-02-03T20:00:48Z")

</div>

Install Windows Subsystem for Linux and install necessary packages. WSL1 is good enough. No need to update to WSL2. Tutorial: https://docs.microsoft.com/en-us/windows/wsl/install-win10 Open the Microsoft Store app, a…

---

## [How to set the MIP gap](https://forum.ysang.org/t/how-to-set-the-mip-gap/30)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 1\
**Last updated:** [August 18, 2020, 4:05am UTC](https://forum.ysang.org/t/how-to-set-the-mip-gap/30 "2020-08-18T04:05:05Z")

</div>

There is a MIP gap for every mixed-integer problem. The default MIP gap for Gurobi is 1e-4. However, you could do some settings to change this value. It can be any non-negative value. Warning: Most of the time, a MIP ga…

---

## [How to read test system data](https://forum.ysang.org/t/how-to-read-test-system-data/33)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [August 4, 2020, 5:03pm UTC](https://forum.ysang.org/t/how-to-read-test-system-data/33 "2020-08-04T17:03:41Z")

</div>

If the test system just has 3 buses, we could define the system in the C++ code. However, if the system is not that small, it would take a lot of work to define the system in the C++ code. What I do is: Use .txt files…

---

## [How to output the results from Gurobi](https://forum.ysang.org/t/how-to-output-the-results-from-gurobi/31)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [August 4, 2020, 3:42pm UTC](https://forum.ysang.org/t/how-to-output-the-results-from-gurobi/31 "2020-08-04T15:42:26Z")

</div>

Once you run the code MyModel.optimize(); you will be able to solve the problem, and the objective value will be displayed in the command window automatically. However, the values of the variables will not be displayed a…

---

## [How to track the solution time of the optimization problem](https://forum.ysang.org/t/how-to-track-the-solution-time-of-the-optimization-problem/32)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [August 4, 2020, 4:01pm UTC](https://forum.ysang.org/t/how-to-track-the-solution-time-of-the-optimization-problem/32 "2020-08-04T16:01:25Z")

</div>

After the optimization problem is solved, the solution time will be displayed in the command window, shown as follows: You could record the solution time manually, however, there is a method that you could save the so…

---

## [How to get the dual value of a constraint using C++&Gurobi?](https://forum.ysang.org/t/how-to-get-the-dual-value-of-a-constraint-using-c-gurobi/28)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [August 4, 2020, 12:16am UTC](https://forum.ysang.org/t/how-to-get-the-dual-value-of-a-constraint-using-c-gurobi/28 "2020-08-04T00:16:11Z")

</div>

First thing first, if you are solving mixed-integer programs (MIP), including mixed-integer linear programs (MILP), you are out of luck, because Gurobi does not support getting dual values for mixed-integer problems. You…

---

## [Finding the runtime error in your C++ code](https://forum.ysang.org/t/finding-the-runtime-error-in-your-c-code/15)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [June 7, 2020, 11:39pm UTC](https://forum.ysang.org/t/finding-the-runtime-error-in-your-c-code/15 "2020-06-07T23:39:31Z")

</div>

There are two types of errors in the C++ code that you may encounter: (1) Errors that show up when you compile the code (2) Errors that show up when you run the code (runtime error) For error Type (1), it is relativel…

---

## [How to catch the runtime error](https://forum.ysang.org/t/how-to-catch-the-runtime-error/29)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [August 4, 2020, 3:24am UTC](https://forum.ysang.org/t/how-to-catch-the-runtime-error/29 "2020-08-04T03:24:55Z")

</div>

We talked about how to find the runtime error in a previous post (Finding the runtime error in your C++ code). Many times, we could figure out what the error is by checking the code closely once figure out which part of …

---

## [Is 0.8% of error worth your attention?](https://forum.ysang.org/t/is-0-8-of-error-worth-your-attention/27)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [July 20, 2020, 12:18am UTC](https://forum.ysang.org/t/is-0-8-of-error-worth-your-attention/27 "2020-07-20T00:18:08Z")

</div>

Recently, I got the code for a mixed-integer linear programming (MILP) problem implemented using C++&Gurobi. The objective value of this optimization problem is 1.97308e6, while the correct answer that I got from a previ…

---

## [Set up SSH remote access for Windows Subsystem for Linux (WSL)](https://forum.ysang.org/t/set-up-ssh-remote-access-for-windows-subsystem-for-linux-wsl/26)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [July 4, 2020, 11:41pm UTC](https://forum.ysang.org/t/set-up-ssh-remote-access-for-windows-subsystem-for-linux-wsl/26 "2020-07-04T23:41:29Z")

</div>

To access lab computers remotely, you could use TeamViewer in the Windows environment. However, it gets stuck sometimes and the experience is just not perfect. Since we usually run our code in the Linux environment, it w…

---

## [Defining the objective function in Gurobi](https://forum.ysang.org/t/defining-the-objective-function-in-gurobi/25)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [June 11, 2020, 5:37pm UTC](https://forum.ysang.org/t/defining-the-objective-function-in-gurobi/25 "2020-06-11T17:37:14Z")

</div>

There two methods using which you could define objective functions. ===================================================== The first one is to define the objective explicitly: GRBVar x = SimpleExample.addVar(0, 50, 20…

---

## [C++ Gurobi programming for Beginners](https://forum.ysang.org/t/c-gurobi-programming-for-beginners/20)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [June 10, 2020, 12:24am UTC](https://forum.ysang.org/t/c-gurobi-programming-for-beginners/20 "2020-06-10T00:24:38Z")

</div>

Hey! Once you set up your C++ and Gurobi, you might want to quickly start solving your first problem! (If you haven’t set up your C++ and Gurobi under the Linux environment and don’t know how to compile and run a code un…

---

## [Removing null characters](https://forum.ysang.org/t/removing-null-characters/14)

<div class="topic-metadata">

**Author:** [@yuanruisang](https://forum.ysang.org/u/yuanruisang)\
**Replies:** 0\
**Last updated:** [June 7, 2020, 6:17am UTC](https://forum.ysang.org/t/removing-null-characters/14 "2020-06-07T06:17:42Z")

</div>

Once when I was compiling my code under the Linux environment, and bash told me that there were a lot of errors in my code - because my code had a lot of null characters. I used the following command to remove the null c…
