How to represent a variable in pyomo which takes a single value from three specified values - abstract

I want to declare a variable in PYOMO which must take any of the three values.
For example, the variable must take a value from these 0.037364, 0.03174, 0.03797.
Model.cost =Var(within=NonNegativeReals)??

model.A=Set(initialize=[0.037364, 0.03174, 0.03797])
model.Y=Var(within=model.A)

Related

How to have dynamic value within contentful?

I am looking to be able to hold a value in a variable and use that variable throughout contentful, is this possible? Let’s say the variable is a number 12345, this number may change a month from now. If I write 12345 inside each content area like 60 times, I have to edit 60 times (not even mentioning locales). Is there a way I can create a label and put this value in that and use that label throughout my content?
in your case I would suggest creating a separate content type that holds this value. You can then refer to this value in any other content types. This way, if you have to change the value, you only do it once, and all the other content types have the new value.

JMeter: How do I inject specific range of values in 2 different parameters of http requests

We have a scenario where user has to enter numeric data in 2 fields. If user enters 1.11 in first field, he has to enter 1.20 in second field as it's the range he has to enter. Basically, whatever is the number entered in first field, next field should contain +0.11. That way we have 2 parameters in http request.
If you're trying to generate pairs of random values where 2nd value should be larger than first one by 0.11 you can do this as follows (the examples assume __groovy() function):
Create a random Double value using RandomUtils.nextDouble() function, truncate it to 2 decimal points and save it into first JMeter Variable
${__groovy(org.apache.commons.lang3.RandomUtils.nextDouble(1\,10).trunc(2),first)}
here I limit the range to be from 1 to 10, however you may use different one
Add 0.11 to the first variable and truncate the result to 2 decimal points
${__groovy(((vars.get('first') as double) + 0.11).trunc(2),)}
Demo:
More information: Mathematical Functions and Converting Data Types in Groovy

Return variable from its name in MATLAB

Say I have a variable var=1 and a string str='var'.
How can I obtain the value of var from str?. I tried using str2num(str), but it didn't work.
Also, if I had 2 strings str1='some letters' and str2='str1', can I obtaing the phase 'some letters' from str2?
I want to do this because I have many matrices (quite big) and I want to separate them in some groups, so I thought about making cells with the names of each of the matrices that belong to a group (a matrix can belong to more than one group, so making cells with the matrices is not very good).
You can use eval:
x = eval( str ) ;
But it's not recommended.
Though it can easily be achieved with an eval as #Shai mentioned, you probably don't really want to do this. Using eval hinders your debugging and depending on the name of variables seriously limits the flexibility of your code. If you want to name something, you may be better off using a struct with a data field and a name field instead.
Judging from your description, I wonder about the following:
1. Why do you have many matrices?
For each variable that you have, you depend on a name. Depending on a lot of names is typically undesirable. Hence my suggestion:
Use a (cell) array containing these matrices
2. What way do you exactly want them to be in a group
It is not clear to me how you want the grouping to work, but think of this:
If you want to use names, create a struct or array of structs with a nameField, but
otherwise just use a cell array and have each matrix get a number.
You can now handle the matrices more easily and things like 'selecting 10 random matrices' or 'selecting all matrices whose nameField contains 'abc'' can be done easily and efficiently.
You can now also have a field with your data specifying in which groups it is, or you can define groups as simple lists of numbers.

How to convert a string containing non-numeric values into numeric values?

I have several variables of the form:
1 gdppercap
2 19786,97
3 20713,737
4 20793,163
5 23070,398
6 5639,175
I have copy-pasted the data into Stata, and it thinks they are strings. So far I have tried:
destring gdppercap, generate(gdppercap_n)
but get
gdppercap contains nonnumeric characters; no generate
And:
encode gdppercap, gen(gdppercap_n)
but get a variable numbered from 1 to 1055 regardless of the previous value.
Also I've tried:
gen gdppercap_n = real(gdppercap)
But get:
(1052 missing values generated)
Can you help me? As far as I can tell, Stata do not like the fact that the variable contains fraction numbers.
If I understand you correctly, the interpretation as string arises from one and possibly two facts:
The variable name may be echoed in the first observation. If so, that's text and it's inconsistent with a numeric variable. The root problem there is likely to be a copy-and-paste operation that copied too much. Stata typically gives you a choice when importing by copy-and-paste of whether the first row of what you copied is to be treated as variable names or as data, and you need the first choice, so that column headers become variable names, not data. It may be best to go back and do the copy-and-paste correctly. However, Stata can struggle with multiple header lines in a spreadsheet. Alternatively, use import excel, not a copy-and-paste. Alternatively, drop in 1 to remove the first observation, provided that it consistently is superfluous.
Commas indicate decimal places. destring can easily cope with this: see the help for its dpcomma option. Stata has no objection to fractions; that would be absurd. The problem is that you need to flag your use of commas.
Note that
destring is a wrapper for real(), so real() is not a way round this.
encode is for mapping genuine categorical variables to integers, as you discovered, and as its help does explain. It is not for fixing data input errors.
You can write a for loop to convert a comma to a period. I don't quite know your variables but imagine you have a variable gdppercap with information like 1234,343 and you want that to be 1234.343 before you do the destring.
For example:
forvalues x = 1(1)10 {
replace gdppercap = substr(gdppercap, 1, `x'-1) + "." + substr(gdppercap, `x'+1, .)
if substr(gdppercap, `x', 1) == ","
}

matlab "from workspace" block has error

If have an array (5000x1 double) in matlab workspace. I put the 'from workspace' block in simulink window for input of another block, But when run the program this error occurred:
Invalid matrix-format variable specified as workspace input in 'new_net_pattern_recog/From Workspace'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values. Matrix values cannot be Inf or NaN.
What can I do?
I believe that you are getting this error because the From Workspace block is expecting your data to be in the form of a time series. According to the documentation for this block,
In the Data parameter of the block, enter a MATLAB expression that specifies the workspace data. The expression must evaluate to one of the following:
A MATLAB timeseries object
A structure of MATLAB timeseries objects
An array or structure containing an array of simulation times and corresponding signal values
It sounds like your 5000x1 element array does not change over time, and these values are intended to remain constant throughout the entirety of the simulation. If this is true, then you should just use a Constant block. To use a variable from the workspace as the output of this block, simply set the "Constant Value" parameter of the constant block to the name of your variable. Refer to this doc for more info on the Constant block.

Resources