运筹优化学习13:C#中Gurobi的配置及测试

简介: 运筹优化学习13:C#中Gurobi的配置及测试

1 C#中的Gurobi的配置

1.1 添加引用

  • 新建C#应用程序
  • 添加Gurobi的dll引用,方法为找到gurobi的安装目录的bin目录下找到

20191026220236454.png

  • 在cs文件中引用 Gurobi

20191026220627137.png

1.2 将编译环境配置为64位

20191026220802324.png

2 官方代码测试及结果

官方的C#示例存放在gurobi安装路径的:examples\C#中,本次演示的示例是workforce1_cs.cs

workforce1_cs.cs

/* Copyright 2019, Gurobi Optimization, LLC */
/* Assign workers to shifts; each worker may or may not be available on a
   particular day. If the problem cannot be solved, use IIS to find a set of
   conflicting constraints. Note that there may be additional conflicts
   besides what is reported via IIS. */
using System;
using Gurobi;
class workforce1_cs
{
    static void Main()
    {
        try
        {
            // Sample data
            // Sets of days and workers
            string[] Shifts =
                new string[] { "Mon1", "Tue2", "Wed3", "Thu4", "Fri5", "Sat6",
              "Sun7", "Mon8", "Tue9", "Wed10", "Thu11", "Fri12", "Sat13",
              "Sun14" };
            string[] Workers =
                new string[] { "Amy", "Bob", "Cathy", "Dan", "Ed", "Fred", "Gu" };
            int nShifts = Shifts.Length;
            int nWorkers = Workers.Length;
            // Number of workers required for each shift
            double[] shiftRequirements =
                new double[] { 3, 2, 4, 4, 5, 6, 5, 2, 2, 3, 4, 6, 7, 5 };
            // Amount each worker is paid to work one shift
            double[] pay = new double[] { 10, 12, 10, 8, 8, 9, 11 };
            // Worker availability: 0 if the worker is unavailable for a shift
            double[,] availability =
                new double[,] { { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1 },
              { 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 },
              { 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
              { 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
              { 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1 },
              { 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1 },
              { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
            // Model
            GRBEnv env = new GRBEnv();
            GRBModel model = new GRBModel(env);
            model.ModelName = "assignment";
            // Assignment variables: x[w][s] == 1 if worker w is assigned
            // to shift s. Since an assignment model always produces integer
            // solutions, we use continuous variables and solve as an LP.
            GRBVar[,] x = new GRBVar[nWorkers, nShifts];
            for (int w = 0; w < nWorkers; ++w)
            {
                for (int s = 0; s < nShifts; ++s)
                {
                    x[w, s] =
                        model.AddVar(0, availability[w, s], pay[w], GRB.CONTINUOUS,
                                     Workers[w] + "." + Shifts[s]);
                }
            }
            // The objective is to minimize the total pay costs
            model.ModelSense = GRB.MINIMIZE;
            // Constraint: assign exactly shiftRequirements[s] workers
            // to each shift s
            for (int s = 0; s < nShifts; ++s)
            {
                GRBLinExpr lhs = 0.0;
                for (int w = 0; w < nWorkers; ++w)
                    lhs.AddTerm(1.0, x[w, s]);
                model.AddConstr(lhs == shiftRequirements[s], Shifts[s]);
            }
            // Optimize
            model.Optimize();
            int status = model.Status;
            if (status == GRB.Status.UNBOUNDED)
            {
                Console.WriteLine("The model cannot be solved "
                    + "because it is unbounded");
                return;
            }
            if (status == GRB.Status.OPTIMAL)
            {
                Console.WriteLine("The optimal objective is " + model.ObjVal);
                return;
            }
            if ((status != GRB.Status.INF_OR_UNBD) &&
                (status != GRB.Status.INFEASIBLE))
            {
                Console.WriteLine("Optimization was stopped with status " + status);
                return;
            }
            // Do IIS
            Console.WriteLine("The model is infeasible; computing IIS");
            model.ComputeIIS();
            Console.WriteLine("\nThe following constraint(s) "
                + "cannot be satisfied:");
            foreach (GRBConstr c in model.GetConstrs())
            {
                if (c.IISConstr == 1)
                {
                    Console.WriteLine(c.ConstrName);
                }
            }
            // Dispose of model and env
            model.Dispose();
            env.Dispose();
        }
        catch (GRBException e)
        {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " +
                e.Message);
        }
    }
}

示例结果:

2019102622114493.png


相关文章
|
2月前
|
人工智能 搜索推荐 数据管理
探索软件测试中的自动化测试框架选择与优化策略
本文深入探讨了在现代软件开发流程中,如何根据项目特性、团队技能和长期维护需求,精准选择合适的自动化测试框架。
127 8
|
14天前
|
运维 关系型数据库 MySQL
os-copilot安装_配置_功能测试全集
我是一位中级运维工程师,我平时工作会涉及到 各类服务器的 数据库 与 java环境配置 操作。 我顺利使用了OS Copilot的 -t -f | 功能,我的疑惑是不能在自动操作过程中直接给与脚本运行权限,必须需要自己运行一下 chmod 这个既然有了最高的权限,为什么就不能直接给与运行权限呢。 我认为 -t 功能有用,能解决后台运行基础命令操作。 我认为 -f 功能有用,可以通过task文件中撰写连续任务操作。 我认为 | 对文件理解上有很直接的解读,可以在理解新程序上有很大帮助。
159 84
|
13天前
|
人工智能 Ubuntu Linux
os-copilot使用之全面配置与使用测试
作为一名个人开发者,我主要从事云服务器架设工作。近期,我成功使用了OS Copilot的 `-t -f |` 功能,解决了执行语句、连续提问及快速理解文件的问题。我发现这些功能非常实用,特别是在使用Workbench时能快速调用AI助手。此外,建议将AI功能与xShell工具联动,进一步提升效率。文中详细记录了购买服务器、远程连接、安装配置OS Copilot以及具体命令测试的过程,展示了如何通过快捷键和命令行操作实现高效开发。
112 67
|
16天前
|
Dart 前端开发 Android开发
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
2月前
|
域名解析 弹性计算 监控
slb测试基本配置检查
slb测试基本配置检查
114 60
|
23天前
|
消息中间件 监控 小程序
电竞陪玩系统架构优化设计,陪玩app如何提升系统稳定性,陪玩小程序平台的测试与监控
电竞陪玩系统架构涵盖前端(React/Vue)、后端(Spring Boot/php)、数据库(MySQL/MongoDB)、实时通信(WebSocket)及其他组件(Redis、RabbitMQ、Nginx)。通过模块化设计、微服务架构和云计算技术优化,提升系统性能与可靠性。同时,加强全面测试、实时监控及故障管理,确保系统稳定运行。
|
2月前
|
开发框架 .NET Java
C#集合数据去重的5种方式及其性能对比测试分析
C#集合数据去重的5种方式及其性能对比测试分析
36 11
|
2月前
|
缓存 API C#
C# 一分钟浅谈:GraphQL 优化与性能提升
本文介绍了 GraphQL API 的常见性能问题及优化方法,包括解决 N+1 查询问题、避免过度取数据、合理使用缓存及优化解析器性能,提供了 C# 实现示例。
93 33
|
2月前
|
开发框架 .NET Java
C#集合数据去重的5种方式及其性能对比测试分析
C#集合数据去重的5种方式及其性能对比测试分析
52 10
|
2月前
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
141 13