Good C++ alternative to MATLAB's "fminunc"? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to convert some code written in MATLAB to C++. I'm having some (or actually quite a lot of) trouble finding an alternative to the "fminunc" function which is used in the MATLAB code that I can replace and use in the C++ code. I've been looking at the "dlib"-library because I've heard it could be a function there I can use, but I'm not sure what function to use.
This is how the "fminunc" is used in the MATLAB code I want to convert:
[theta, cost] = ...
fminunc(#(t)(costFunction(t, X, y)), initial_theta, options);
Does anyone know any good optimizing functions like this in C++?

I believe what you are looking for is Google's Ceres Solver, an open source C++ library for modeling and solving large, complicated optimization problems. The code is designed to handle two classes of problems:
Non-linear Least Squares problems with bounds constraints.
General unconstrained optimization problems.
Automatic Differentiation is also supported.
Several cool example applications can be found here.

There are a bunch of optimizers in dlib, some that use gradients and others than just work on black-box functions. You can see some examples here http://dlib.net/optimization_ex.cpp.html and more generally here http://dlib.net/optimization.html.

Related

How do I compare two functions' speed and performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two functions performing same process but with different techniques and I need to know on a large scale which technique is faster than the other of maybe in the future will be more techniques available. So my question is, how can I do that in c++ specially? Is there a specific method and header to be used to perform this task?
More details:
For example the isLargest() uses three parameters and it has two versions, one uses a nested if technique and the other uses initializers and less if statements. So if I need to know which one is faster, how can I do that?
Try your code in the real world and measure
There is a tool called a profiler that is meant to solve this problem. Broadly speaking, there are two kinds (note: some are a mix between the two):
Sampling profilers.
Instrumenting profilers.
It's worth learning about what each does and their pros/cons, but if you don't know what to use go with a sampling profiler.
There are many sampling profilers, but support depends on your platform. If you're on Windows, Visual Studio comes with a really nice sampling profiler and I recommend you start there!
If you go down this route, it's important to make sure you use your functions as you would "for real" when you're profiling them, as there are many subtle factors that can affect the result.
An alternative
If you don't want to try your code running in a real program, perhaps if you're just trying to understand general characteristics of the function, there are libraries to help you do this such as Google Benchmark.
Benchmarking code can be surprisingly difficult to get right, so I would strongly recommend using existing benchmarking tools where like Google Benchmark wherever possible.

Implementation of C string functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to know how the string manipulation functions are implemented internally so I can figure out their performance. Is there a way to find this out?
I'm talking about null-terminated c strings (arrays of chars) and the related functions (strcat and such) in C++, if that has anything to do with it.
Is there a way to find this out?
Use the source, Luke
The sources for GNU libc string functions are easily viewable in the Git repository
You can also look at other free software or open source C libraries, such as newlib, FreeBSD, NetBSD, OpenBSD, OpenSolaris etc.
if your whole purpose is to figure out the performance I don't think you need to know how it is implemented. You can feed in different data and come up with a graph and compare how the functions performed.
But if you need to study how those functions are implemented, then there is always the source code, which you can get from the internet for different C++ compilers( Not all compilers though).

AI for time table generator software [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop a time table generator software for my college. Obviously it requires a great deal of constraint satisfaction i.e. I need to satisfy a lot of rules in order to generate a bunch of time tables where classes do not clash. After doing some research and reading this article, I feel I need to use some AI in it. Now, I am a complete newbie to AI. Can anyone tell me which algorithm will work best in my case?
The simplest algorithm that you can use for this problem is genetic algorithm (or any other evolutionary algorithm). Solving this problem using GA is very simple but yet effective. There are lots of papers and codes that have used this approach for this problem.
If you have few rules and constraints, you may want to use exact straightforward techniques like backtracking with CSP heuristics to speed it up, but if there are lots of classes and constraints, I suggest Genetic Algorithm.
Well, not a trivial task indeed. Problems like this one are VERY hard to solve.
Here I can recommend you two things:
Use an existing CSP/COP solver and describe your constraints in its language. These solvers are very good, fast and tuned, being developed for years.
Educate yourself in the area of Discrete Optimization (there was a course at coursera.org with the same name which was great). Only after you grasp the basics of how these things work can you try to write your own solver. But let you be warned! Discrete optimization is pain and suffering :-).
This is by no means a suitable place to just tell you how CSP/COP works. It is a very broad and difficult field.
I wish you good luck!

Plotting lines in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have code to solve 2D truss structures implemented in C++ (as a console application).
I would like to add some simple graphics to it in order to visualize the initial structure and the deformed form. Example: http://people.rit.edu/pnveme/VenkatCOMSOL42/COMSOLTruss2D/ExtractingInfoinMATLAB_01.png
But I have no idea of how to add graphics in any way. How can I implement this kind of graphics?
Cross platform (preferred) or windows.
I doubt this is the answer you want, but to be honest, implementing plotting functionality isn't trivial, even if you're not doing it from scratch: you'll have to review a number of libraries, choose one, and get to grips with it.
Unless it's essential that the plotting functionality be integrated with the solver (e.g. for a product), I think you ought to consider simply exporting your results to, say, a simple ASCII matrix format that can be easily imported into a variety of environments with extensive and flexible plotting capabilities, one or more of which you probably already know (e.g. Matlab, R, Octave, etc.)
One option would be to generate a file which can be shown by another program. For example, generating SVG and using the browser to display it sounds like it would be good for your case, you can even easily include it in a report.
Check out SDL: http://www.libsdl.org/ It's cross-platform and has a ton of features. It maybe a bit overwhelming for your task, but I thought it was very easy to grasp when I was a newcomer.
There is a derivative of SDL that lets you draw basic shapes such as lines and points and is incredibly easy to use:
http://sdl-draw.sourceforge.net/

How to approach a C++ parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wanting to have a go at a C++ parser for a formatter I am making.
You can obviously open a file and use getline(..) or get(), is this reasonable way of starting things off and then working out a system using vector arrays and hence creating loads of arrays and somehow structuring out and processing what you are doing from there. For example say I wanted to find ever function in a source file, all functions have the common syntax, "(){" once whitespace has been removed, so do you just look for common delimeters to parse out the sections into arrays. I suppose I will learn as I go.
Or I also assume there are tried and tested ways of doing this, and I would likley just be reinventing the wheel as they say.
C++ is a language that is quite hard to parse in the first place. So if you want anything other that really trivial C++ code to be "understood" by your parser, you are definitely better off starting with an existing product.
The Clang frontend library would perhaps be a good starting point.
There are also a number of "source to source" conversion examples based on clang. Here's one of them: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/