SCIPsolve fails for the simple optimization program - scip

I'm trying to solve simple optimization problem using SCIP but the method SCIPsolve returns exception.
The problem is: Minimize x + y + z
subject to: xyz = 1 where x, y and z are integers
The source code is given below. Does anybody have idea what's wrong with this code?
I tried this code in Visual Studio 2010 and 2012. I used the latest version of SCIP source code and libraries from http://scip.zib.de/
// 1. Initializing the SCIP environment
SCIP* scip;
SCIP_CALL(SCIPcreate(&scip));
// 2. Load all desired plugins
SCIP_CALL(SCIPincludeDefaultPlugins(scip));
// 3. Creating a problem
SCIP_CALL(SCIPcreateProb(scip, "Example", NULL, NULL, NULL, NULL, NULL, NULL, NULL));
// 4. Creating variables
SCIP_VAR *x, *y, *z;
SCIP_CALL(SCIPcreateVar(scip, &x, "x", -1, 1, 1.0, SCIP_VARTYPE_INTEGER, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL));
SCIP_CALL(SCIPcreateVar(scip, &y, "y", -1, 1, 1.0, SCIP_VARTYPE_INTEGER, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL));
SCIP_CALL(SCIPcreateVar(scip, &z, "z", -1, 1, 1.0, SCIP_VARTYPE_INTEGER, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL));
// 5. Creating constraint
SCIP_EXPR* exprx;
SCIP_EXPR* expry;
SCIP_EXPR* exprz;
SCIP_CALL(SCIPexprCreate(SCIPblkmem(scip), &exprx, SCIP_EXPR_VARIDX, 0));
SCIP_CALL(SCIPexprCreate(SCIPblkmem(scip), &expry, SCIP_EXPR_VARIDX, 1));
SCIP_CALL(SCIPexprCreate(SCIPblkmem(scip), &exprz, SCIP_EXPR_VARIDX, 2));
/* setup monomial for x*y*z */
SCIP_Real exponents[3] = { 1.0, 1.0, 1.0 };
SCIP_EXPRDATA_MONOMIAL* monomial;
SCIP_Real one = 1.0;
SCIP_CALL(SCIPexprCreateMonomial(SCIPblkmem(scip), &monomial, one, 3, NULL, exponents));
/* create polynomial expression x*y*z */
SCIP_EXPR* expr;
SCIP_EXPR* children[3] = {exprx, expry, exprz};
SCIP_CALL(SCIPexprCreatePolynomial(SCIPblkmem(scip), &expr, 3, children, 1, &monomial, 0.0, FALSE));
/* setup expression tree with expr as root expression, the tree is defined w.r.t. 3 variables */
SCIP_EXPRTREE* exprtree;
SCIP_CALL(SCIPexprtreeCreate(SCIPblkmem(scip), &exprtree, expr, 3, 0, NULL));
/* assign SCIP variables to tree */
SCIP_VAR* vars[3] = { x, y, z };
SCIP_CALL(SCIPexprtreeSetVars(exprtree, 3, vars));
/* create and add nonlinear constraint x*y*z = 1 */
SCIP_CONS* constraint;
SCIP_CALL(SCIPcreateConsBasicNonlinear(scip, &constraint, "xyz", 0, NULL, NULL, 1, &exprtree, &one, 1.0, 1.0));
SCIP_CALL(SCIPaddCons(scip, constraint));
// 6. Solving the problem
SCIP_CALL(SCIPsolve(scip));

In Step 4, you miss the addition of the variables to SCIP (SCIPaddVar).

Related

Supply different families of priors as a parameter in the bugs/stan model

This is the classic eight school example in Bayesian data analysis by Andrew Gelman. Please see the stan file and R code below. I use a cauchy prior with paratmer A for the hyperparamter tau in the stan file. I am trying to supply the R function "school" with different priors not within cauchy family, for example, uniform(0,1000) prior, so that I do not have to create different stans file for the new priors. Is this possible within stan or bugs?
schools.stan:
`
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
real<lower=0> A;
}
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
}
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
}
model {
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
tau ~ cauchy(0,A);
}
`
`
school <- function(A=100){
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
A=A)
fit <- stan(file = "schools.stan", data = schools_dat,iter = 20)
print(fit)
}
school()
`
I tried the following but have no idea how to change the stan file correspondingly.
`
school <- function(prior="dunif(0,1000"){
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
prior=prior)
fit <- stan(file = "schools.stan", data = schools_dat,iter = 20)
print(fit)
}
school()
`
It's possible to pre-specify more than one distribution in the Stan code, and then specify which distribution you want in the input data. Stan isn't really intended to be used this way, but it can be done!
Here's an example. I've added a new data variable, tau_prior; it's an integer that specifies which prior you want to use for tau. 1 = Cauchy, 2 = uniform, 3 = exponential. In addition, for each type of prior, there's a data variable that sets a hyperparameter. (Hyperparameters for the distributions that aren't chosen have no effect.)
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
int<lower=1,upper=3> tau_prior;
real<lower=0> cauchy_sigma;
real<lower=0> uniform_beta;
real<lower=0> exponential_beta;
}
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
}
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
}
model {
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
if(tau_prior == 1) {
tau ~ cauchy(0, cauchy_sigma);
} else if(tau_prior == 2) {
tau ~ uniform(0, uniform_beta);
} else if(tau_prior == 3) {
tau ~ exponential(exponential_beta);
}
}
I've also modified the R function so that it provides default values for each hyperparameter, on a scale similar to the one you've used already.
school <- function(tau_prior = 1,
cauchy_sigma = 100,
uniform_beta = 1000,
exponential_beta = 0.01) {
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
tau_prior = tau_prior,
cauchy_sigma = cauchy_sigma,
uniform_beta = uniform_beta,
exponential_beta = exponential_beta)
fit <- stan(file = "schools.stan", data = schools_dat, iter = 20)
print(fit)
}
# The default: use a Cauchy prior with scale 100.
school()
# Use a uniform prior with the default upper limit (1000).
school(tau_prior = 2)
# Use an exponential prior with a non-default rate (1).
school(tau_prior = 3, exponential_beta = 1)

Invalid object name of stored procedure error

I have a stored procedure such as below :
CREATE PROCEDURE [dbo].[sp_InsertPumpsStatus] (
#Frequency1 FLOAT,
#Power1 FLOAT,
#Db1 FLOAT,
#Efficiency1 FLOAT,
#Frequency2 FLOAT,
#Power2 FLOAT,
#Db2 FLOAT,
#Efficiency2 FLOAT,
#Frequency3 FLOAT = NULL,
#Power3 FLOAT = NULL,
#Db3 FLOAT = NULL,
#Efficiency3 FLOAT = NULL,
#Frequency4 FLOAT = NULL,
#Power4 FLOAT = NULL,
#Db4 FLOAT = NULL,
#Efficiency4 FLOAT = NULL,
#Frequency5 FLOAT = NULL,
#Power5 FLOAT = NULL,
#Db5 FLOAT = NULL,
#Efficiency5 FLOAT = NULL,
#Frequency6 FLOAT = NULL,
#Power6 FLOAT = NULL,
#Db6 FLOAT = NULL,
#Efficiency6 FLOAT = NULL,
#Frequency7 FLOAT = NULL,
#Power7 FLOAT = NULL,
#Db7 FLOAT = NULL,
#Efficiency7 FLOAT = NULL,
#Frequency8 FLOAT = NULL,
#Power8 FLOAT = NULL,
#Db8 FLOAT = NULL,
#Efficiency8 FLOAT = NULL,
#Frequency9 FLOAT = NULL,
#Power9 FLOAT = NULL,
#Db9 FLOAT = NULL,
#Efficiency9 FLOAT = NULL,
#Frequency10 FLOAT = NULL,
#Power10 FLOAT = NULL,
#Db10 FLOAT = NULL,
#Efficiency10 FLOAT = NULL,
#SolarDateTime NVARCHAR(20),
#Date DATETIME
)
AS
BEGIN
INSERT INTO [dbo].sp_InsertPumpsStatus
(
[Frequency1],
[Power1],
[Db1],
[Efficiency1],
[Frequency2],
[Power2],
[Db2],
[Efficiency2],
[Frequency3],
[Power3],
[Db3],
[Efficiency3],
[Frequency4],
[Power4],
[Db4],
[Efficiency4],
[Frequency5],
[Power5],
[Db5],
[Efficiency5],
[Frequency6],
[Power6],
[Db6],
[Efficiency6],
[Frequency7],
[Power7],
[Db7],
[Efficiency7],
[Frequency8],
[Power8],
[Db8],
[Efficiency8],
[Frequency9],
[Power9],
[Db9],
[Efficiency9],
[Frequency10],
[Power10],
[Db10],
[Efficiency10],
[SolarDateTime],
[Date]
)
VALUES
(
#Frequency1,
#Power1,
#Db1,
#Efficiency1,
#Frequency2,
#Power2,
#Db2,
#Efficiency2,
#Frequency3,
#Power3,
#Db3,
#Efficiency3,
#Frequency4,
#Power4,
#Db4,
#Efficiency4,
#Frequency5,
#Power5,
#Db5,
#Efficiency5,
#Frequency6,
#Power6,
#Db6,
#Efficiency6,
#Frequency7,
#Power7,
#Db7,
#Efficiency7,
#Frequency8,
#Power8,
#Db8,
#Efficiency8,
#Frequency9,
#Power9,
#Db9,
#Efficiency9,
#Frequency10,
#Power10,
#Db10,
#Efficiency10,
#SolarDateTime,
#DATE
)
END
when I execute this procedure, I get an error:
Invalid object name 'dbo.sp_InsertPumpsStatus'
What is problem?
Invalid object name 'dbo.sp_InsertPumpsStatus'.
In your script, It can be clearly seen that, You are giving same name to the stored procedure as the table in statement. Make sure the defined table on Insert statement is correct. If it is correct, Give another name to the stored procedure.

GLSL variables not storing?

I am learning GLSL through Unity and I recently came across a problem involving the storing of variables.
Shader "Shader" {
Properties{
_Hole_Position("hole_Position", Vector) = (0., 0., 0., 1.0)
_Hole_EventHorizonDistance("hole_EventHorizonDistance", Float) = 1.0
_DebugValue("debugValue", Float) = 0.0
}
SubShader{
Pass{
GLSLPROGRAM
uniform mat4 _Object2World;
//Variables
varying float debugValue;
varying vec4 pos;
varying vec4 hole_Position;
varying float hole_EventHorizonDistance = 1;
#ifdef VERTEX
void main()
{
pos = _Object2World * gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
void main()
{
float dist = distance(vec4(pos.x, 0.0,pos.z, 1.0), vec4(hole_Position.x, 0.0, hole_Position.z, 1.0));
debugValue = dist;
if (dist < hole_EventHorizonDistance)
{
gl_FragColor = vec4(0.3, 0.3, 0.3, 1.0);
}
else
{
gl_FragColor = vec4(0.4, 0.6, 1.0, 1.0);
}
//gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);
}
#endif
ENDGLSL
}
}
}
Now Hole_Position and EventHorizonDistance are changed from an outside C#-script with:
g.GetComponent<Renderer>().sharedMaterial.SetVector("_Hole_Position", new Vector4(transform.position.x, transform.position.y, transform.position.z, 1));
g.GetComponent<Renderer>().sharedMaterial.SetFloat("_Hole_EventHorizonDistance", 2);
this does not work as I intend it too (by changing the fragments color if its position is within 2 units from Hole_Position. However debugging with:
gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);
seemingly suggests that EventHorizon is 0 at all times (the mesh tested on remains completely black), however debugging by getting and printing the variable from an outside (via
print(g.GetComponent<Renderer>().sharedMaterial.GetFloat("_Hole_EventHorizonDistance"));
) tells me EventHorizonDistance = 2. I cannot wrap my head around why this is the case, why is it so?

Groovy not returning a list of lists after for loop

I am learning Groovy and I am trying to return a list of lists but when I do my for loop in counter() function, it automatically returns just giving me the first iteration and doesn't continue with the rest of the words.
I found the issue is in the for loop of counter(), it looks like Groovy shares the i variable in the loops. Coming from Python each for loop holds its own variable i. Is there something like this in Groovy?
lista = ["apple","banana","orange","melon","watermelon"]
def copaa(a_list_of_things){
lista_to_return = []
for (i = 0; i < a_list_of_things.size(); i++) {
lis = counter(a_list_of_things[i])
lista_to_return.add(lis)
}
return lista_to_return
}
def counter(word){
list_of_times = []
//return "bla"
for (i = 0; i < word.length(); i++) {
list_of_times.add(i)
}
return list_of_times
}
ls = copaa(lista)
println(ls)
Avoid global scope:
prefix the i variable declarations with the implicit type def (actually Object) or an appropriate explicit type (e.g. int or Integer) to make the scope local to the loop. Otherwise these variables are placed (as a single one i) in the bindings of the script (practically it's treated as a global variable).
Modify the relevant lines of your code like this:
// with def...
for (def i = 0; i < a_list_of_things.size(); i++) {
// ...
for (def i = 0; i < word.length(); i++) {
// ...OR with an explicit type (e.g. int) the scope is limited
// to the for loop as expected
for (int i = 0; i < a_list_of_things.size(); i++) {
// ...
for (int i = 0; i < word.length(); i++) {
Output
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
The Groovy Way
To gives you some extra hints I reimplemented your algorithm using some of the cool features groovy provides (collect, closure, numeric ranges):
wordList = ["apple","watermelon"]
// collect process each word (the implicit variable it) and returns a new list
// each field of the new list is a range from 0 till it.size() (not included)
outList = wordList.collect { (0 ..< it.size()).toArray() }
assert outList == [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

Int arrays in Objective-C

So I have this:
int a[4] = { 0, 1, 2, 3 };
And then I want to make a new int array:
int b[4];
What is the easiest way to make b[] = a[]?
memcpy(b, a, sizeof(int) * 4);