<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Message</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.6001.18939"></HEAD>
<BODY bgColor=#ffffff text=#000000>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2 face=Arial>Hi
Christian,</FONT></SPAN></DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2 face=Arial>Thank
you for replies. In the mean time I have played around a bit more with issues 2)
and have reached a working version of that "encapsulating" function. It is not
very elegant, though. </FONT></SPAN><FONT face=Arial><FONT size=2><FONT
color=#0000ff><SPAN class=057384309-08112010>In order not need to deal with
destruction of objects instantiated through "new" I am using boost's shared_ptr.
Nevertheless, because such objects get destroyed when last reference to objects
go out of scope, I have to pass back all MP_* related objects back to caller's
scope (even for locally created and used MP_set variables, such as 'abs_x_set'
below). Hence, I have resorted to pass back all the MP_* objects via references
to the caller, although this makes the entire encapsulating thing a bit
questionable.</SPAN></FONT></FONT></FONT></DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial></FONT></SPAN> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial></FONT></SPAN> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial>#include <boost/shared_ptr.hpp><BR>typedef
boost::shared_ptr<MP_set> MP_set_ptr;<BR>typedef
boost::shared_ptr<MP_variable> MP_var_ptr;<BR>typedef
boost::shared_ptr<MP_expression> MP_expr_ptr;<BR>typedef
boost::shared_ptr<MP_constraint> MP_cnstr_ptr;<BR>typedef
boost::shared_ptr<MP_model> MP_model_ptr;</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2 face=Arial>static
MP_expr_ptr<BR>lp_objective_absolute(<BR> MP_variable&
x,<BR> MP_data& weight,<BR> MP_set_ptr&
abs_x_set_ptr,<BR> MP_var_ptr&
abs_x_var_ptr,<BR> vector<MP_cnstr_ptr>&
abs_x_linctr,<BR> MP_model&
lp/*=MP_model::getDefaultModel()*/,<BR> const string&
prefix/*="abs_"*/) {<BR> if (x.size() > 0) {<BR> if
(x.getName().empty()) {<BR> string
message;<BR> message += "decision variable(s) or affine
expression(s) is/are not named";<BR> throw
BmtException(message.c_str());<BR> }<BR> // create
required MP_* objects as automatic variables<BR> MP_set_ptr
abs_x_set(new MP_set(x.size()));<BR> MP_var_ptr abs_x_var(new
MP_variable(*abs_x_set));<BR> MP_cnstr_ptr abs_x_linctr_a(new
MP_constraint(*abs_x_set));<BR> MP_cnstr_ptr abs_x_linctr_b(new
MP_constraint(*abs_x_set));</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> // set-up names<BR> string abs_x_name(prefix +
x.getName());<BR> abs_x_set->setName(abs_x_name +
"_set");<BR> (*abs_x_var).setName(abs_x_name);<BR> abs_x_linctr_a->setName(abs_x_name
+ "_cnstr_a");<BR> abs_x_linctr_b->setName(abs_x_name +
"_cnstr_b");</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> // set-up bounds and
constraints<BR> (*abs_x_var).lowerLimit(*abs_x_set) =
0.0;<BR> (*abs_x_linctr_a)(*abs_x_set) = +1.0*x(*abs_x_set) <=
(*abs_x_var)(*abs_x_set);<BR> (*abs_x_linctr_b)(*abs_x_set) =
-1.0*x(*abs_x_set) <= (*abs_x_var)(*abs_x_set);</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> // add constraints to
model<BR> lp.add(*abs_x_linctr_a);<BR> lp.add(*abs_x_linctr_b);</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> // assign automatic variables to out
parameters<BR> // in order to prevent destruction of MP_*
objects.<BR> abs_x_set_ptr = abs_x_set;<BR> abs_x_var_ptr
=
abs_x_var;<BR> abs_x_linctr.push_back(abs_x_linctr_a);<BR> abs_x_linctr.push_back(abs_x_linctr_b);</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> MP_expr_ptr objective(new MP_expression(sum(*abs_x_set,
weight(*abs_x_set)*(*abs_x_var)(*abs_x_set))));</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff size=2 face=Arial></FONT> </DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2
face=Arial> return objective;<BR> } else {<BR> return
MP_expr_ptr();<BR> }<BR>}<BR></FONT></SPAN></DIV>
<DIV><SPAN class=057384309-08112010><FONT color=#0000ff size=2 face=Arial><FONT
color=#0000ff size=2><FONT color=#0000ff size=2>
<P></FONT></FONT></FONT></SPAN><FONT size=2 face=Tahoma>-----Original
Message-----<BR><B>From:</B> Christian Wolf [mailto:kalmar@uni-paderborn.de]
<BR><B>Sent:</B> 06 November 2010 18:34<BR><B>To:</B> flopcpp@list.coin-or.org;
Roth, Marc (RWE Trading)<BR><B>Subject:</B> Re: [FlopCpp] Using "symbolic"
variable names.<BR><BR></FONT></P></DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">Hello Marc,<BR><BR>Am 05.11.2010 17:55,
schrieb <A class=moz-txt-link-abbreviated
href="mailto:marc.roth@rwe.com:">marc.roth@rwe.com:</A>
<BLOCKQUOTE
cite=mid:E7FDCCA07104474A92CCEE0903653F0B0995A404@s080a0022.group.rwe.com
type="cite">
<META name=Generator
content="MS Exchange Server version 6.5.7654.12"><!-- Converted from text/rtf format -->
<P><FONT size=2 face=Arial>Hi,</FONT> </P>
<P><FONT size=2 face=Arial>I am developing a toy problem in order to figure
out how FlopC++ has to be used. I consulted the example problems in the
examples section to get started but have a couple of questions:</FONT></P>
<P><FONT size=2 face=Arial>1) Use of symbolic names and LP file output: I
would like to use the setName method on MP_variable, lets say, such
as</FONT> </P>
<P> <FONT size=2
face=Arial>MP_model lp(new OsiCbcSolverInterface) ;</FONT>
<BR> <FONT size=2
face=Arial>MP_set T(123);</FONT>
<BR> <FONT size=2
face=Arial>MP_variable dispatch_period_volume(T);</FONT>
<BR> <FONT size=2
face=Arial>x.setName("dp_volume_");</FONT> </P>
<P><FONT size=2 face=Arial>Once the model has been set up, I write the
problem to a file, such as</FONT> </P>
<P> <FONT size=2
face=Arial>lp.attach();
</FONT><BR> <FONT size=2
face=Arial>lp.Solver->writeLp("c:\\temp\\bmt);</FONT> </P>
<P><FONT size=2 face=Arial>What I expect to see in the file bmt.lp is that
the variable names are printed like dp_volume_1, dp_volume_2, …,
dp_volume_124, but what I get is only x1, …, x124. Am I doing something
wrong or is this feature not supported (in this case what is the
MP_variable::setNames() method for?).</FONT></P></BLOCKQUOTE>At the moment the
feature is not yet supported, but it will be in the near future, at least in
the stochastic branch of FlopC++. <BR><BR>
<BLOCKQUOTE
cite=mid:E7FDCCA07104474A92CCEE0903653F0B0995A404@s080a0022.group.rwe.com
type="cite">
<P><FONT size=2 face=Arial>2) In the objective function in question some of
the decision variables x are modelled by abs(x) (the absolute value of x).
Obviously this is not a linear construct, but can be modelled as:</FONT></P>
<P> <FONT size=2 face=Arial>x
<= abs_x</FONT> <BR> <FONT
size=2 face=Arial>-1.0*x <= abs_x</FONT>
<BR> <FONT size=2 face=Arial>0
<= abs_x</FONT> </P>
<P><FONT size=2 face=Arial>Because I require various variables to be
modelled as abs(.) I would like to implement a function that;</FONT>
<BR><FONT size=2 face=Arial>A) creates the auxiliary variable(s) abs_x for
each such x,</FONT> <BR><FONT size=2 face=Arial>B) adds above
constraints/bounds to the model, and</FONT> <BR><FONT size=2 face=Arial>C)
returns a MP_expression that can be added to the objective function
expression in terms of the abs_x variables.</FONT> </P>
<P><FONT size=2 face=Arial>I.e., I would like to delegate/encapsulate all
issues to model the absolute value of a decision variable to a
function.</FONT> </P>
<P><FONT size=2 face=Arial>My initial approach, listed below, failed
miserably because of the issue of automatic variables and copy constructors,
etc., I guess boiling down to that FlopC++ elements are not "shallow" copied
and reference counted on the one had and that MP_variables are not added to
the model implicitly but only through their use in constraints and objective
function. What is the proper way to accomplish
this?</FONT></P></BLOCKQUOTE><BR>In my opinion this could work, if you work
with pointers instead of objects in your helper function. Create the
MP_constraints and MP_variables in the function with new, otherwise they
should be lost when the function goes out of scope. This is only a first
thought about the topic. I can look into it someday next week.<BR><BR>Hope
this helps,<BR>Christian<BR>
<BLOCKQUOTE
cite=mid:E7FDCCA07104474A92CCEE0903653F0B0995A404@s080a0022.group.rwe.com
type="cite"><BR>
<P><FONT size=2 face=Arial>static MP_expression</FONT> <BR><FONT size=2
face=Arial>lp_objective_absolute(</FONT>
<BR>
<FONT size=2
face=Arial>MP_variable& x,</FONT>
<BR>
<FONT size=2
face=Arial>MP_data& weight,</FONT>
<BR>
<FONT size=2
face=Arial>MP_model& lp/*=MP_model::getDefaultModel()*/,</FONT>
<BR>
<FONT size=2 face=Arial>const
string& prefix/*="abs_"*/) {</FONT>
<BR> <FONT size=2 face=Arial>if
(x.size() > 0) {</FONT> <BR>
<FONT size=2 face=Arial>if
(x.getName().empty()) {</FONT>
<BR>
<FONT size=2 face=Arial>string
message;</FONT> <BR>
<FONT size=2 face=Arial>message
+= "decision variable(s) or affine expression(s) is/are not named";</FONT>
<BR>
<FONT size=2 face=Arial>throw
BmtException(message.c_str());</FONT>
<BR>
<FONT size=2 face=Arial>}</FONT>
<BR>
<FONT size=2 face=Arial>MP_set
D(x.size());</FONT> <BR>
<FONT size=2
face=Arial>MP_variable abs_x(D);</FONT>
<BR>
<FONT size=2
face=Arial>abs_x.setName(prefix + x.getName());</FONT>
<BR>
<FONT size=2
face=Arial>abs_x.lowerLimit(D) = 0.0;</FONT>
<BR>
<FONT size=2
face=Arial>MP_constraint abs_x_linctr_a(D);</FONT>
<BR>
<FONT size=2
face=Arial>MP_constraint abs_x_linctr_b(D);</FONT>
<BR>
<FONT size=2
face=Arial>abs_x_linctr_a(D) = x(D) <= abs_x(D);</FONT>
<BR>
<FONT size=2
face=Arial>abs_x_linctr_b(D) = -1.0*x(D) <= abs_x(D);</FONT> </P>
<P>
<FONT size=2
face=Arial>lp.add(abs_x_linctr_a);</FONT>
<BR>
<FONT size=2
face=Arial>lp.add(abs_x_linctr_b);</FONT> </P>
<P>
<FONT size=2 face=Arial>return
sum(D, weight(D)*abs_x(D));</FONT>
<BR> <FONT size=2 face=Arial>}
else {</FONT> <BR>
<FONT size=2 face=Arial>return
MP_expression();</FONT> <BR> <FONT
size=2 face=Arial>}</FONT> <BR><FONT size=2 face=Arial>}</FONT> </P>
<P><FONT size=2 face=Arial>And would be used such as</FONT> </P>
<P> <FONT size=2
face=Arial>MP_expression objective;</FONT>
<BR> <FONT size=2
face=Arial>MP_data UNIT_WEIGHT_X(T);</FONT>
<BR> <FONT size=2
face=Arial>UNIT_WEIGHT_X(T) = 1.0;</FONT>
<BR> <FONT size=2
face=Arial>objective = lp_objective_absolute(dispatch_period_volume,
UNIT_WEIGHT_X, lp);</FONT> </P>
<P><FONT size=2 face=Arial>Best Regards/Mit freundlichen Grüßen</FONT>
<BR><B><FONT size=2 face=Arial>Marc Roth</FONT></B><BR><FONT size=2
face=Arial>External Consultant,<BR>Structuring & Valuation<BR>Tel.: +41
55 534 44 82<BR>Mob: +44 7795 276 136<BR>Fax: +41 55 534 44 82<BR></FONT><A
href="mailto:marc.roth@rwe.com" moz-do-not-send="true"><U><FONT
color=#0000ff size=2
face=Arial>mailto:marc.roth@rwe.com</FONT></U></A><BR><FONT size=2
face=Arial>yahoo:nerdyanorak</FONT> <BR><FONT size=1 face=Arial>RWE Supply
& Trading GmbH<BR>London Branch<BR>130 Wood Street<BR>London EC2V
6DL<BR>United Kingdom<BR><BR>Advisory Board:<BR>Dr. Ulrich Jobs
(Chairman)</FONT> </P>
<P><FONT size=1 face=Arial>Board of Directors:<BR>Stefan Judisch
(CEO)<BR>Dr. Bernhard Günther<BR>Dr. Peter Kreuzberg<BR>Richard Lewis</FONT>
<BR><FONT size=1 face=Arial>Alan Robinson</FONT> </P>
<P><FONT size=1 face=Arial>Head Office: Essen, Germany </FONT><BR><FONT
size=1 face=Arial>Registered at the County Court of Essen<BR>Commercial
Registry No.: HRB 14327<BR>Sales Tax Identification No. DE 8130 22
070</FONT> </P>
<P><BR></P><PRE wrap=""><FIELDSET class=mimeAttachmentHeader></FIELDSET>
_______________________________________________
FlopCpp mailing list
<A class=moz-txt-link-abbreviated href="mailto:FlopCpp@list.coin-or.org">FlopCpp@list.coin-or.org</A>
<A class=moz-txt-link-freetext href="http://list.coin-or.org/mailman/listinfo/flopcpp">http://list.coin-or.org/mailman/listinfo/flopcpp</A>
</PRE></BLOCKQUOTE><BR></BLOCKQUOTE></BODY></HTML>
<p>
<br>
</p>