1

I solved a problem with ampl model software. I need to convert the code to java or c# to print out the result. How can I represent the ampl model code in java. Is there any libraries? How can I represent objective function and maximize function in java and c#? Please help me on this.

My AMPL code:

param i;    #Supply
param j;    #Demand
param k;    #Time

var x{1..i,1..j,1..k} binary;

maximize z_flow : sum{a in 1..k} (x[1,1,a]+x[1,5,a] +  x[2,1,a]+x[2,3,a] + x[3,2,a]+x[3,3,a] + x[4,1,a]+ x[4,2,a] + x[4,4,a] + x[5,2,a]+x[5,3,a]);

subject to supply1cons{a in 1..k} : (x[1,1,a] + x[1,5,a]) <= 1;
subject to supply2cons{a in 1..k} : (x[2,1,a] + x[2,3,a]) <= 1;
subject to supply3cons{a in 1..k} : (x[3,2,a] + x[3,3,a]) <= 1;
subject to supply4cons{a in 1..k} : (x[4,1,a] + x[4,2,a] + x[4,4,a]) <= 1;
subject to supply5cons{a in 1..k} : (x[5,2,a] + x[5,3,a]) <= 1;

subject to demand1cons{a in 1..k} : (x[1,1,a] + x[2,1,a] + x[4,1,a]) <= 1;
subject to demand2cons{a in 1..k} : (x[3,2,a] + x[4,2,a] + x[5,2,a]) <= 1;
subject to demand3cons{a in 1..k} : (x[2,3,a] + x[3,3,a] + x[5,3,a]) <= 1;
subject to demand4cons{a in 1..k} : (x[4,4,a]) <= 1;
subject to demand5cons{a in 1..k} : (x[1,5,a]) <=1;

subject to cap1 : sum{a in 1..k}(x[1,1,a]) = 2;
subject to cap2 : sum{a in 1..k}(x[1,5,a]) = 8;
subject to cap3 : sum{a in 1..k}(x[2,1,a]) = 3;
subject to cap4 : sum{a in 1..k}(x[2,3,a]) = 4;
subject to cap5 : sum{a in 1..k}(x[3,2,a]) = 1;
subject to cap6 : sum{a in 1..k}(x[3,3,a]) = 7;
subject to cap7 : sum{a in 1..k}(x[4,1,a]) = 5;
subject to cap8 : sum{a in 1..k}(x[4,2,a]) = 2;
subject to cap9 : sum{a in 1..k}(x[4,4,a]) = 6;
subject to cap10 : sum{a in 1..k}(x[5,2,a]) = 4;
subject to cap11 : sum{a in 1..k}(x[5,3,a]) = 3;
8
  • Unless your model is very simple, you cannot do it, it is a highly nontrivial task. Commented Dec 11, 2012 at 20:24
  • Ali..It's a simple model..I need to find the minimum time slot to send all packets in a netwrok with some contraints Commented Dec 11, 2012 at 20:31
  • Then there is some hope. Could you post your AMPL model? Commented Dec 11, 2012 at 20:36
  • Do you have any licencing restriction? Restricition on third parties? Commented Dec 11, 2012 at 20:37
  • @ali.. no...no restriction with third parties untill its free.. and please find my ampl code in the post..I redited. Commented Dec 11, 2012 at 21:15

3 Answers 3

4

AMPL is a modeling language, you can create models with reasonable efforts.

Then, the AMPL envionment calls a solver (backend if you wish) that actually solves your problem. It isn't AMPL that solves your problem.

Since your problem is fortunately simple, you can give up the modeling language part. Nevertheless, you still need a solver that solves the problem for you.

Your problem is a binary integer programming problem. Any linear programming solver that can handle integer variables is a candidate. Solvers are typically written in C or C++, so you need one that has a Java or C# interface.

Finally, you will code / build your problem using that API the solver comes with.


Candidates:

See also

Sign up to request clarification or add additional context in comments.

1 Comment

I'd add also or-tools. It has a nice .net wrapper to GLPK, CBC and other solvers (CBC is one of the best free solvers IMHO).
0

There are two parts to your question: modeling and calling from a (C#) program. As others have noted, AMPL is a modeling language. If you like using a modeling language like AMPL, you will need to call it from your program. Currently, AMPL does not have an API, but you can call AMPL via scripting. AIMMS and MPL both have APIs that allow you to call them from other programs, but they will require you to rewrite your model in their respective modeling languages.

The other option is to call a solver API directly. A number of solvers, including CPLEX and Gurobi, have .NET APIs that allow you to build and solve a model directly from a C# program.

(Disclaimer: I currently work for Gurobi Optimization and formerly worked for ILOG, which provided CPLEX).

Comments

0

You might try

Optimization Framework

It's a free project where you can do modelling and also you can read AMPL Dat and Mod files into your model. Solving is possible via CPLEX, Gurobi, MOPS and others.

Optimization Framework does not use AMPL, it uses GNU Mathprog for building model instances. MathProg is a subset of AMPL, so 80%-90% of your AMPL models will run unchanged.

Have in mind, that it's an academic project, so don't expect it to be stable or ripe for commercial use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.