How to create the multi Set for CPLEX OPL - opl

I want create the multi of paths (e.g. three paths) that store the link<i,j>, but get the error. Ask everyone what can I modify?
tuple path{
{int} p;
}
{path} ps={1,2,3};
I think that sample way is create the three sets.
{int} p1;
{int} p2;
{int} p3;

tuple path{
{int} p;
}
{path} ps={<{1}>,<{2}>,<{3}>};
execute
{
writeln(ps);
}
works fine

Related

Slim Twig array conversion error while rendering

I am just playing around with the microFramework Slim + Twig template Engine. But actually passing an array in render method.
SomeOne help me to solve the error.
running in my local environment using XAMPP
Below is my code in index.php
<?php
/* Require and initialize Slim and Twig */
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
require 'Views/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$app = new \Slim\Slim(array(
'view' => new \Slim\Extras\Views\Twig(),
'templates.path' => './Templates'
));
/* Application routes */
$app->get('/', function () use($app) {
$pageTitle = 'hello world';
$body = 'sup world';
$app->render('index.php',array('title' => $pageTitle, 'body' => $body));
});
/* Run the application */
$app->run();
and below is the error am getting
Slim Application Error
The application could not run because of the following error:
Details
Type: ErrorException
Code: 4096
Message: Argument 1 passed to Twig_Template::render() must be an array, object given, called in C:\xampp\htdocs\app\Slim\Extras\Views\Twig.php on line 99 and defined
File: C:\xampp\htdocs\app\Views\Twig\lib\Twig\Template.php
Line: 244
Trace
Your resolution is an overhead. Better way is to pass $this->all(), which will return an array of set's key-value data, as an argument for $template->render() method.
public function render($template)
{
$env = $this->getEnvironment();
$template = $env->loadTemplate($template);
return $template->render($this->all());
}
Before April $this->data was an array but now it is a Set so that's why your problem occurred.
Unfortunately i have to answer my question.
I have just did a little hack in the Twig.php(in slim extras) and introduced a new function called ObjectToArray and now it works
function objectToArray( $object ) {
$array=array();
foreach($object as $member=>$data)
{
$array[$member]=$data;
}
return $array;
}
so my custom Twig.php files looks something like this
<?php
/**
* Slim - a micro PHP 5 framework
*
* #author Josh Lockhart
* #link http://www.slimframework.com
* #copyright 2011 Josh Lockhart
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Slim\Extras\Views;
/**
* TwigView
*
* The TwigView is a custom View class that renders templates using the Twig
* template language (http://www.twig-project.org/).
*
* Two fields that you, the developer, will need to change are:
* - twigDirectory
* - twigOptions
*/
class Twig extends \Slim\View
{
/**
* #var string The path to the Twig code directory WITHOUT the trailing slash
*/
public static $twigDirectory = null;
/**
* #var array Paths to directories to attempt to load Twig template from
*/
public static $twigTemplateDirs = array();
/**
* #var array The options for the Twig environment, see
* http://www.twig-project.org/book/03-Twig-for-Developers
*/
public static $twigOptions = array();
/**
* #var TwigExtension The Twig extensions you want to load
*/
public static $twigExtensions = array();
/**
* #var TwigEnvironment The Twig environment for rendering templates.
*/
private $twigEnvironment = null;
/**
* Get a list of template directories
*
* Returns an array of templates defined by self::$twigTemplateDirs, falls
* back to Slim\View's built-in getTemplatesDirectory method.
*
* #return array
**/
private function getTemplateDirs()
{
if (empty(self::$twigTemplateDirs)) {
return array($this->getTemplatesDirectory());
}
return self::$twigTemplateDirs;
}
/**
* Render Twig Template
*
* This method will output the rendered template content
*
* #param string $template The path to the Twig template, relative to the Twig templates directory.
* #return void
*/
public function render($template)
{
$env = $this->getEnvironment();
$template = $env->loadTemplate($template);
return $template->render($this->objectToArray($this->data));
}
/**
* Creates new TwigEnvironment if it doesn't already exist, and returns it.
*
* #return Twig_Environment
*/
public function getEnvironment()
{
if (!$this->twigEnvironment) {
// Check for Composer Package Autoloader class loading
if (!class_exists('\Twig_Autoloader')) {
require_once self::$twigDirectory . '/Autoloader.php';
}
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Filesystem($this->getTemplateDirs());
$this->twigEnvironment = new \Twig_Environment(
$loader,
self::$twigOptions
);
// Check for Composer Package Autoloader class loading
if (!class_exists('\Twig_Extensions_Autoloader')) {
$extension_autoloader = dirname(__FILE__) . '/Extension/TwigAutoloader.php';
if (file_exists($extension_autoloader)) require_once $extension_autoloader;
}
if (class_exists('\Twig_Extensions_Autoloader')) {
\Twig_Extensions_Autoloader::register();
foreach (self::$twigExtensions as $ext) {
$extension = is_object($ext) ? $ext : new $ext;
$this->twigEnvironment->addExtension($extension);
}
}
}
return $this->twigEnvironment;
}
/**
* Converts PHP objects into Array.
*
* #return array
*/
private function objectToArray( $object ) {
$array=array();
foreach($object as $member=>$data)
{
$array[$member]=$data;
}
return $array;
}
}
I think it should work without the above modification.
If you do have find any better solution , let me know
This will solve the problem!
function objectToArray($object){
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object)) {
$object = get_object_vars($object);
}
return array_map('objectToArray', $object);
}

Unable to convert "cocos2d::CCPoint" to "cocos2d::CCPoint"

I am newbie in C++ and cocos2d-x, so i do not understand why it's wrong.
Code
void
MainLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
// Get any touch and convert the touch position to in-game position.
CCTouch* touch = (CCTouch*)pTouches->anyObject();
CCPoint position = touch->locationInView();
position = CCDirector::sharedDirector()->convertToGL(position);
__pShip->setMove(position);
}
This is code of function.
Ship::setMove(CCPoint *newPosition)
{
__move=*newPosition;
}
As you can see it uses CCPoint type as parameter, but it fails with position
Header:
class Ship : public AnimatedObject
{
public:
Ship();
bool init(const char* frameName, CCSpriteBatchNode* pSpriteSheet);
void setMove(CCPoint* newPosition);
void move();
private:
/**
* A CCMoveTo action set in the move() method.
*/
cocos2d::CCFiniteTimeAction* __pMoveAction;
/**
* This value specifies the ship's speed.
*/
float __speed;
/**
* This value specifies position to which the ship should move.
* It's set in touch events callbacks in MainLayer class.
*/
CCPoint __move;
};
What am I doing wrong? Why this code fails to converting CCPoints?
Use:
__pShip->setMove(&position); // Address-of
Or change the function itself:
Ship::setMove(CCPoint newPosition) // better: const CCPoint& newPosition
{
__move = newPosition;
}
If CCPoint is a small class, use by value (as shown), or if it is larger (copy is expensive), use the commented prototype.

Is there an additional runtime cost for using named parameters?

Consider the following struct:
public struct vip
{
string email;
string name;
int category;
public vip(string email, int category, string name = "")
{
this.email = email;
this.name = name;
this.category = category;
}
}
Is there a performance difference between the following two calls?
var e = new vip(email: "foo", name: "bar", category: 32);
var e = new vip("foo", 32, "bar");
Is there a difference if there are no optional parameters defined?
I believe none. It's only a language/compiler feature, call it syntactic sugar if you like. The generated CLR code should be the same.
There's a compile-time cost, but not a runtime one...and the compile time is very, very minute.
Like extension methods or auto-implemented properties, this is just magic the compiler does, but in reality generates the same IL we're all familiar with and have been using for years.
Think about it this way, if you're using all the parameters, the compiler would call the method using all of them, if not, it would generate something like this behind the scenes:
var e = new vip(email: "foo", category: 32); //calling
//generated, this is what it's actually saving you from writing
public vip(string email, int category) : this(email, category, "bar") { }
No it is a compile-time feature only. If you inspect the generated IL you'll see no sign of the named parameters. Likewise, optional parameters is also a compile-time feature.
One thing to keep in mind regarding named parameters is that the names are now part of the signature for calling a method (if used obviously) at compile time. I.e. if names change the calling code must be changed as well if you recompile. A deployed assembly, on the other hand, will not be affected until recompiled, as the names are not present in the IL.
There shouldn't be any. Basically, named parameters and optional parameters are syntactic sugar; the compiler writes the actual values or the default values directly into the call site.
EDIT: Note that because they are a compiler feature, this means that changes to the parameters only get updated if you recompile the "clients". So if you change the default value of an optional parameter, for example, you will need to recompile all "clients", or else they will use the old default value.
Actually, there is cost at x64 CLR
Look at here http://www.dotnetperls.com/named-parameters
I am able to reproduce the result: named call takes 4.43 ns, and normal call takes 3.48 ns
(program runs in x64)
However, in x86, both take around 0.32 ns
The code is attached below, compile and run it yourself to see the difference.
Note that in VS2012 the default targat is AnyCPU x86 prefered, you have to switch to x64 to see the difference.
using System;
using System.Diagnostics;
class Program
{
const int _max = 100000000;
static void Main()
{
Method1();
Method2();
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method1();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method2();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00 ns"));
Console.Read();
}
static void Method1()
{
Method3(flag: true, size: 1, name: "Perl");
}
static void Method2()
{
Method3(1, "Perl", true);
}
static void Method3(int size, string name, bool flag)
{
if (!flag && size != -1 && name != null)
{
throw new Exception();
}
}
}

ambiguous copy constructors vc 2008

I'm trying to recompile older code in latest Visual Studio (2008) and code that worked previously now fails to compile. One of the problems is due to overloaded operators for my class. below there is simplified class to demonstrate the problem. If I remove casting operators for int and char* then it works fine. So one of the ways to fix my issue is to replace them with procedures to_char and to_int and use them instead but it will require a lot of changes in the code (that class is heavily used). There must be some better, smarter way to fix it. any help is greatly appreciated :-)
class test
{
public:
test();
test(char* s2);
test(int num);
test(test &source);
~test();
operator char*();
operator int();
};
test::test() {
}
test::test(char* s2) {
}
test::test(int num) {
}
test::test(test &source) {
}
test::operator char*() {
}
test::operator int() {
}
test test_proc() {
test aa;
return aa;
}
int test_proc2(test aa)
{
return 0;
}
int main()
{
test_proc2(test_proc());
}
//test.cpp(60) : error C2664: 'test_proc2' : cannot convert parameter 1 from 'test' to 'test'
// Cannot copy construct class 'test' due to ambiguous copy constructors or no available copy constructor
Try changing
test(test &source);
to
test(const test &source);
The issue is that the test_proc call returns a temporary test object, which can be passed to a function that accepts a const reference, but not a plain reference.

How to use stdext::hash_map?

I would like to see a simple example of how to override stdext::hash_compare properly, in order to define a new hash function and comparison operator for my own user-defined type. I'm using Visual C++ (2008).
This is how you can do it
class MyClass_Hasher {
const size_t bucket_size = 10; // mean bucket size that the container should try not to exceed
const size_t min_buckets = (1 << 10); // minimum number of buckets, power of 2, >0
MyClass_Hasher() {
// should be default-constructible
}
size_t operator()(const MyClass &key) {
size_t hash_value;
// do fancy stuff here with hash_value
// to create the hash value. There's no specific
// requirement on the value.
return hash_value;
}
bool operator()(const MyClass &left, const MyClass &right) {
// this should implement a total ordering on MyClass, that is
// it should return true if "left" precedes "right" in the ordering
}
};
Then, you can just use
stdext::hash_map my_map<MyClass, MyValue, MyClass_Hasher>
Here you go, example from MSDN
I prefer using a non-member function.
The method expained in the Boost documentation article Extending boost::hash for a custom data type seems to work.

Resources