Blogs

The One With

January 2011 - Posts

  • Genetic Algorithms, Traveling Salesman and Charts

         

    I have came across my really old Genetic Algorithm code and thought I’d share it with you. The easiest way to understand GA is to think of it as a search function, a search for a way to get to a solution. Imagine you have a problem and you know the solution to it, but you don’t know how to get to that solution.

    Genetic Algorithms have many applications; designing circuit boards, code breaking, finding the best route to travel etc... The traveling problem is, I guess, the easiest to define and is the most common application of GA.

    Travelling Salesman Problem (TSP) is defined as follows: A salesman has to visit N number cities and the order in which he visits them is such that the total distance traveled is the minimum.

    At the first glance, this is easy to solve. There are N! number of possible combinations, we can just go through them and find the best.

    • 3 cities = 3! = 6 combinations
    • 4 cities = 4! = 24
    • 11 cities = 11! = 39 916 800
    • 12 cities = 12! = 479 001 600
    • 21 cities = 21! = 5.10909422 × 1019

    If you have a CPU capable of analyzing one billion (1,000,000,000) combinations per second, analyzing 21! combinations would take you 21! / 86,400 (1 day = 86 400 seconds) = 5.91330349 × 1014 days. That’s a lot of days my friends. We can surely use GA to get it done :)

    Traveling Salesman Problem

    Download the source from http://machine.codeplex.com/ (requires XtraCharts to compile)

    Let me know if you have any questions about Genetic Algorithms.

    Cheers

    Azret

  • Business Letters and Mail Merge with Rich Text Edit (Part 2)

         

    In part one of Business Letters and Mail Merge with Rich Text Edit, I showed you how to use the INCLUDEPICTURE tag and IUriStreamProvider to merge images into your document.Now, let’s take a closer look at Document.GetHtmlText.

    Imagine a simple document with an image:

    RichEdit

    string html = richEditControl1.Document.GetHtmlText(richEditControl1.Document.Range, null);

    A call to GetHtmlText will produce HTML that has the image embedded in the src as a base64 string.

    <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEAAAAAAAD/7gAOQWRvYmUAZAAAA= ....." />

    This is just a default behavior that we can control by supplying a custom IUriProvider. And that’s exactly what we need if we need to send documents over email. We want send images as attachments and reference them using a "cid:" prefix.

    The implementation of IUriProvider.CreateImageUri

    int imageId;
    public string CreateImageUri(string rootUri, RichEditImage image, string relativeUri) {
        string imageName = String.Format("image{0}", imageId);
        imageId++;
    
        RichEditImageFormat imageFormat = GetActualImageFormat(image.RawFormat);
        Stream stream = new MemoryStream(image.GetImageBytes(imageFormat));
        string mediaContentType = RichEditImage.GetContentType(imageFormat);
        AttachementInfo info = new AttachementInfo(stream, mediaContentType, imageName);
        attachments.Add(info);
    
        return "cid:" + imageName;
    }

    Download Source

    Cheers

    Azret

  • Business Letters and Mail Merge with Rich Text Edit (Part 1)

         

    Thanks you all for attending this morning’s webinar on how to work with Mail Merge features of the Rich Text Editor. There have been some very good questions and I want to answer some of them in this follow up post. The webinar will also be available on the DevExpress Channel very soon.

    First, as promised, here is the source code for the Mailer App that I build during the webinar: Source Code. I have changed things a little bit so that the actual RichEditControl is inside a UserControl (as requested during the webinar). When you do that, make sure to take care of a couple of things:


    editor1.RichEdit.BindingContext = dataNavigator1.BindingContext; 

    IUriStreamProvider

    As I explained in the Webinar, adding data fields to the document is very straight forward. Bind to the data source and you’re done (How to: Perform a Mail Merge). We do however need to do some work if we want to bind to a picture.

    We use a special field INCLUDEPICTURE to include a picture URI.

    {INCLUDEPICTURE "dbimg://{MERGEFIELD EmployeeID}"}

    and this is where our IUriStreamProvider comes in. Because we include a URI as an image source, the RichEditControl will try to look it up using the available IUriStreamProvider.

    Our implementation looks like this:

    public class DBUriStreamProvider : IUriStreamProvider {
        static readonly string prefix = "dbimg://";
        DataTable table;
        string columnName;
    
        public DBUriStreamProvider(DataTable table, string columnName) {
            this.table = table;
            this.columnName = columnName;
        }
    
        public Stream GetStream(string uri) {
            uri = uri.Trim();
            if (!uri.StartsWith(prefix))
                return null;
            string strId = uri.Substring(prefix.Length).Trim();
            int id;
            if (!int.TryParse(strId, out id))
                return null;
            DataRow row = table.Rows.Find(id);
            if (row == null)
                return null;
            byte[] bytes = row[columnName] as byte[];
            if (bytes == null)
                return null;
            return new MemoryStream(bytes);
        }
    }
    and we register it like so:
    IUriStreamService uriService 
            = (IUriStreamService)richEditControl1.GetService(typeof(IUriStreamService));
                    
    if (_provider != null) {
        uriService.UnregisterProvider(_provider);
    }
    
    if (value != null && value is DataTable) {
        _provider = new DBUriStreamProvider((DataTable)value, "Photo");
        uriService.RegisterProvider(_provider);
    }
    
    INCLUDEPICTURE
    
    

    Next we'll learn how to properly generate HTML content to be used in emails and how to resolve images during the HTML export.

    Cheers

    Azret

  • Business Letters and Mail Merge with Rich Text Edit

         

    Friends, join me tomorrow Jan 20th for an hour of fun with the Rich Text Editor.

    Rich Text Editor Webinar

     

    Cheers

    Azret

  • New York City Give Camp Live Stream

         

    Watch Seth Juarez – DevExpress (@SethJuarez), Mehul Harry - DevExpress (@MehulHarry) and Rachel Appel – Microsoft (@RachelAppel) as they broadcast live from the New York City Give Camp.

    Cheers

    Azret

  • DevExpress New York City Mixer 2011

         

    Friends,

    We are inviting you and your colleagues to join us for a chance to meet a part of our team

    at our New York City Mixer 2011 at club La Pomme (http://LaPommeNYC.com)

    On January 13th of 2011 @ 7PM

    clip_image001

    Please RSVP by visiting our Facebook page or by sending an email to RSVP@DevExpress.com

    Hope to see you there!

    Azret Botash - DevExpress

More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.