1/* BEGIN LICENSE BLOCK 2 * Version: CMPL 1.1 3 * 4 * The contents of this file are subject to the Cisco-style Mozilla Public 5 * License Version 1.1 (the "License"); you may not use this file except 6 * in compliance with the License. You may obtain a copy of the License 7 * at www.eclipse-clp.org/license. 8 * 9 * Software distributed under the License is distributed on an "AS IS" 10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11 * the License for the specific language governing rights and limitations 12 * under the License. 13 * 14 * The Original Code is The ECLiPSe Constraint Logic Programming System. 15 * The Initial Developer of the Original Code is Cisco Systems, Inc. 16 * Portions created by the Initial Developer are 17 * Copyright (C) 1997-2006 Cisco Systems, Inc. All Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 * END LICENSE BLOCK */ 22 23/* 24 * ECLiPSe Application Example 25 * 26 * $Id: eg_cc_mip.cc,v 1.2 2012/02/25 13:47:56 jschimpf Exp $ 27 * 28 * IDENTIFICATION: eg_sendmore.c 29 * 30 * AUTHOR: Joachim Schimpf 31 * AUTHOR: Stefano Novello 32 * 33 * DESCRIPTION: 34 * Example of solving a constraint problem from C 35 */ 36 37#include "eclipseclass.h" 38#include <iostream> 39 40 41#define NCOLS 3 42#define NROWS 2 43 44double req[NROWS][NCOLS] = { 45 2.2, 1.8, 1.9, 46 2.4, 2.0, 2.1 47}; 48 49double pc[NCOLS] = { 50 24.7, 22.4, 19.7 51}; 52 53 54 55 56static void bounds(EC_word vars, double lb, double ub) 57{ 58 post_goal(term(EC_functor("::",2), 59 vars, 60 term(EC_functor("..",2), lb, ub))); 61} 62 63static void eq(EC_word lhs, EC_word rhs) 64{ 65 post_goal(term(EC_functor("$=",2), lhs, rhs)); 66} 67 68static void geq(EC_word lhs, EC_word rhs) 69{ 70 post_goal(term(EC_functor("$>=",2), lhs, rhs)); 71} 72 73static void leq(EC_word lhs, EC_word rhs) 74{ 75 post_goal(term(EC_functor("$=<",2), lhs, rhs)); 76} 77 78static void maximize(EC_word obj, EC_word objval) 79{ 80 post_goal(term(EC_functor("optimize",2), 81 term(EC_functor("max",1), obj), objval)); 82} 83 84 85int 86main() 87{ 88 ec_init(); 89 { 90 91 EC_ref Profit; 92 EC_refs Vars(NCOLS); 93 EC_ref VarList; 94 95 post_goal("lib(eplex)"); 96 97 VarList = list(Vars); 98 99 bounds(VarList, 0.0, 1e20); 100 101 leq( VarList * list(NCOLS,req[0]), 8.0); 102 leq( VarList * list(NCOLS,req[1]), 10.0); 103 104 maximize(VarList * list(NCOLS,pc), Profit); 105 106 if (EC_resume() == EC_succeed) /* solve */ 107 { 108 double d; 109 int i; 110 111 if (EC_word(Profit).is_double(&d) == EC_succeed) 112 std::cout << "Profit is " << d << "\n"; 113 else 114 std::cout << "Profit is ?\n"; 115 116 for (i=0; i<NCOLS; i++) 117 { 118 if (Vars[i].is_double(&d) == EC_succeed) 119 std::cout << "X" << i << " = " << d << "\n"; 120 else 121 std::cout << "X" << i << " = ?\n"; 122 } 123 } 124 else std::cout << "No solution\n"; 125 } 126 ec_cleanup(); 127} 128 129