我需要优化具有最高总体影响的投资选择。约束条件是总投资最多只能达到8.0。另外一个约束条件是每个类别只能评估两次
我面临的问题是,最大投资额的约束不起作用。我也找不到如何实现Category约束。也欢迎链接到相关表单。
我的代码附在下面
非常感谢
--
int NumberofProjects = 12;
range Project = 1..NumberofProjects; // 12 different projects
//Declaration of Parameters
float Impact[Project] = [0.1, 0.03, 0.07, 0.13, 0.01, 0.04, 0.07, 0.08, 0.15, 0.12, 0.02, 0.04]; // Impact to maximize
float Investment[Project] = [2.0, 1.6, 2.5, 12.0, 10.3, 0.6, 1.2, 1.9, 4.0, 5.0, 0.2, 0.5]; // costs --> can be max 8.0
string Category[Project] = [1,1,1,1,1,2,2,2,3,3,3,3]
float Budget = 8.0;
// Declaration of Parameters
//Expression of Decision
dexpr float Reduction = sum(p in Project) Impact[p];
//Objective Function
maximize Reduction;
//Constraints
constraint ctCapResources[Project];
subject to {
forall (p in Project)
ctCapResources[p]: sum(p in Project)
Investment[p] <= Budget;
}发布于 2021-11-03 07:22:48
主要问题是您忘记添加决策变量。
int NumberofProjects = 12;
range Project = 1..NumberofProjects; // 12 different projects
//Declaration of Parameters
float Impact[Project] = [0.1, 0.03, 0.07, 0.13, 0.01, 0.04, 0.07, 0.08, 0.15, 0.12, 0.02, 0.04]; // Impact to maximize
float Investment[Project] = [2.0, 1.6, 2.5, 12.0, 10.3, 0.6, 1.2, 1.9, 4.0, 5.0, 0.2, 0.5]; // costs --> can be max 8.0
int Category[Project] = [1,1,1,1,1,2,2,2,3,3,3,3];
float Budget = 8.0;
// Declaration of Parameters
// decision variable
dvar boolean invest[Project];
//Expression of Decision
dexpr float Reduction = sum(p in Project) Impact[p]*invest[p];
//Objective Function
maximize Reduction;
//Constraints
constraint ctCapResources[Project];
subject to {
forall (p in Project)
ctCapResources[p]: sum(p in Project)
Investment[p]*invest[p] <= Budget;
// each Category can only be assessed twice
forall(c in 1..3) sum(p in Project:Category[p]==c) invest[p]<=2;
}将会工作得更好
对于你的第二个问题,你可以改变
maximize Reduction;转到
maximize staticLex(Reduction,-Cost);发布于 2021-11-03 16:19:24
@alexfleischer -非常感谢Alex。非常感谢。我还有一个后续问题。我已经为两种制造方法(即内部和外部)添加了一个索引,并希望添加以下限制:·以下三个项目中最多一个可以在内部完成:项目1、项目4、项目5。另外,我注意到,虽然实现了最佳结果(最高影响),但它没有考虑成本(有两个答案产生相同的影响,但成本不同),而我希望合并为次要(不太重要)的目标(应该可以使用CPLEX 12.9?)
int NumberofProduction = 2;
range Production = 1..NumberofProduction; // Internal vs External production
int NumberofProjects = 12;
range Project = 1..NumberofProjects; // 12 different projects
//Declaration of Parameters
float Impact[Project] = [0.1, 0.03, 0.07, 0.13, 0.01, 0.04, 0.07, 0.08, 0.15, 0.12, 0.02, 0.04]; // Impact to maximize
//float Investment[Project] = [2.3, 1.7, 2.9, 2.3, 0.4, 0.7, 1.4, 2.2, 4.5, 5.5, 0.3, 0.6]; // costs --> can be max 8.0
// float Setupcosts[Project] = [0.3, 0.1, 0.4, 0.3, 0.1, 0.1, 0.2, 0.3, 0.5, 0.5, 0.1, 0.1];
// float ExInvestment[Project] = [2.5, 1.7, 2.0, 2.5, 0.4, 0.7, 1.8, 2.2, 4.5, 1.0, 0.4, 0.5];
float InExInvestment[Production][Project] = [[2.3, 1.7, 2.9, 2.3, 0.4, 0.7, 1.4, 2.2, 4.5, 5.5, 0.3, 0.6],[2.5, 1.7, 2.0, 2.5, 0.4, 0.7, 1.8, 2.2, 4.5, 1.0, 0.4, 0.5]];
int Category[Project] = [1,1,1,1,1,2,2,2,3,3,3,3];
float Budget = 8.0;
// decision variable
dvar boolean invest[Production][Project];
//Expression of Decision
dexpr float Reduction = sum(q in Production, p in Project) Impact[p]*invest[q][p];
// dexpr float Costs = sum(q in Production, p in Project) -1*InExInvestment[q][p]; // Second decision to mimimize costs if two options for Reduction with same outcome
//Constraints
constraint ctCapResources[Project];
constraint ctProject[Project];
//Objective Function
maximize Reduction;
subject to {
// each Category can only be assessed twice
forall (p in Project)
ctProject[p]: sum(q in Production)invest[q][p] <=1;
forall(c in 1..3) sum(p in Project:Category[p]==c, q in Production) invest[q][p]<=2;
forall (p in Project)
ctCapResources[p]: sum(q in Production, p in Project)
InExInvestment[q][p]*invest[q][p] <= Budget;
}https://stackoverflow.com/questions/69811086
复制相似问题